How To Install Docker On Ubuntu?

Docker is a container technology very popular in Linux distributions. The docker provides the ability to run single or more commands and applications in an isolated secure environment like a VM but with few resources. As a popular Linux technology, Ubuntu supports docker containers. There are different ways to install Docker on Ubuntu. But two most popular ways are using the Ubuntu official repository or using the Docker-provided installation script via the official site. In this tutorial, we examine both installation methods.

Install Ubuntu Provided Docker

Ubuntu provides the Docker package to install via the package manager like apt, apt-get, or GUI package managers.

Show Docker Package Information

Before installing the Docker we can display the docker package information. First of all the Docker package is named as docker.io . The docker package is related to a tiny GUI tool and does not confuse it with the docker.io which is the real Docker package. We can display the Docker package docker.io information with the apt show like below.

$ apt show docker.io
Show Docker Package Information

Install Docker Package

The Docker package named docker.io can be installed with the sudo apt install command.

$ sudo apt install docker.io

Install Docker From Official Site

Docker can be also installed via the official site using provided installation script. I highly recommend the Docker Official Site installation because it provides a more up-to-date version as the Ubuntu provided Docker comes older versions and is not updated in a regular fashion.

First, we update the apt repository to get the latest package information.

$ sudo apt update

First, we install the required prerequisites

$ sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Docker provides the Ubuntu and deb repository about installation. First we add GPG keys using the curl command.

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
 OK

If everything is OK the output is like above. Now we add the Docker official apt repository to our repository list with the add-apt-repository command like below.

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Now we have added the Docker official Ubuntu repository and we can install packages with the following command.

$ sudo apt install docker-ce docker-ce-cli containerd.io

Disable Docker Update

Docker is a popular and dynamic project which is updated in short intervals. Some features may become absolute and some new features are added. This may create some unreliability to update Docker regularly. So we can disable the docker package update which makes the usage of Docker more reliable. The Docker package update can be disabled with the followint apt-mark command.

$ sudo apt-mark hold docker-ce

Check Docker Service Status

The Docker is installed and used as a service. So in order to use Docker, the Docker services should be running. We can check the Docker service with the following systemct command.

$ systemctl status docker

From the output we can see that the Docker service is active and running .

Start Docker Service

If the Docker service is not running we can start the Docker service with the following command. The service management requires the root privileges so we provide the “sudo” command too.

$ sudo systemctl start docker

Start Docker Server Automatically

In some cases, the Docker service may not start automatically during boot. This requires starting the Docker service manually after every boot. We can enable the Docker service to start automatically after boot.

$ sudo systemctl enable docker

Execute Docker Commands As Non-Root User (Regular User)

By default, Docker requires root privileges in order to manage it like creating containers, etc. During the Docker installation, a new group called docker this group has permission to use docker without any root privileges. So adding non-root or regular users to this Docker group makes it run docker without root privileges.

$ sudo usermod -aG docker $USER

Run Hello World Container with Docker

Now let’s make a test run with the newly installed Docker. As we have configured the current user to use Docker without root privileges we just run the docker command to use it. We will run the Docker contains named hello-world which is created for testing purposes.

$ docker container run hello-world
Run Hello World Container with Docker
See also  How To Copy, Cut, and Paste In Vi/Vim?

Leave a Comment