PixelCove

How to Install Docker on Ubuntu

docker
ubuntu

Prerequisites

To follow this tutorial, you will need the following:

Installing Docker

We are going to install docker from the official Docker repository as the official Ubuntu repo might not be the latest version. To do that we need to add a new package source, add the GPG key from Docker to ensure the downloads are valid, and finally install Docker.

First, we will update our system:

sudo apt update

Next, install a few prerequisite packages which let apt use packages over HTTPS:

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

Then add the GPG key for the official Docker repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Now add the Docker repository to APT sources:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update your existing list of packages again for the addition to be recognized:

sudo apt update

Ensure you are installing from the Docker repo rather than the Ubuntu repo (Optional):

apt-cache policy docker-ce

You’ll see output like this, although the exact version number may be different:

Output of apt-cahce policy docker-ce
docker-ce:
  Installed: (none)
  Candidate: 5:20.10.14~3-0~ubuntu-jammy
  Version table:
     5:20.10.14~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.13~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

Notice docker-ce is not installed, but the candidate for installation is from the official Docker repository.

Finally, install Docker:

sudo apt install docker-ce

Docker should now be installed, the daemon started, and the process enabled to start on boot. To verify it is running:

sudo systemctl status docker

The output should be similar to the following:

Output
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-04-01 21:30:25 UTC; 22s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 7854 (dockerd)
      Tasks: 7
     Memory: 38.3M
        CPU: 340ms
     CGroup: /system.slice/docker.service
             └─7854 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Executing the Docker Command Without Sudo (Optional)

By default, the docker command can only be run by the root user or by a user in the docker group. To avoid having to type sudo every time you use the docker command, add your username to the docker group and apply the new group membership:

sudo usermod -aG docker ${USER} && su - ${USER}

If you need to add a user to the docker group that you’re not logged in as, declare the username explicitly using:

sudo usermod -aG docker username

In conclusion, you’ve installed Docker on your Ubuntu server, enabling efficient application management with lightweight containers. Docker’s flexibility and scalability enhance productivity across various projects and tasks.