How to Install Docker on Ubuntu 20.04 LTS

Introduction

Docker is a very popular open-source containerization platform that allows creating containers of applications and then deploying them. Docker containers are very similar to virtual machines, but they are dependent on the kernel of the host operating system. Whereas Virtual Machines are having their own kernel.

Docker containers are portable and can run on any operating system if the docker engine is installed on it.

In this tutorial, We will install the Latest Version of Docker Community Edition (CE) on Ubuntu 20.04 LTS Focal Fossa.

Prerequisites

In order to follow this tutorial, you will need the following things:

  • Ubuntu 20.04 LTS Focal Fossa Installed
  • A sudo non-root user
  • A Docker Hub account if you want to create your own repository for custom docker images

Uninstall older versions

If you already have an older version of Docker then you need to uninstall it in order to install the latest version. Uninstall the old versions using the below command:

sudo apt-get remove docker docker-engine docker.io containerd runc

Installation methods for Docker on Ubuntu 20.04 LTS

There are several methods available to Install Docker on Ubuntu 20.04.

You will get the same functionality of docker from all the above methods however the best practice is to install the Docker from its Official Repository. Because the Ubuntu Official repository may not have the latest version of docker.

This tutorial will install Docker from its official repository for the Ubuntu 20.04 Distro.

Follow the below steps to install Docker on Ubuntu 20.04.

Step 1: Set up the Official Docker Repository

At the very first step we will update our existing package list by using the below command:

sudo apt update

Now, we will install some packages to allow apt to use HTTPS repository:

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

Then add the Docker official repository GPG Key to our system:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

After that, set up the stable Docker Official repository for ubuntu 20.04 focal fossa to APT Source by using this command:

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

Now we are ready to install Docker on Ubuntu 20.04.

Step 2: Install Docker on Ubuntu 20.04

First, we have to update our package list because we added Docker repository:

sudo apt update

Finally, Install the Docker by using this command:

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

After this, Run the below command to verify that to Docker daemon is running:

sudo systemctl status docker

You will see the below output like this but the version of the Docker may differ.

Output:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-01-25 13:00:34 IST; 1h 33min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 1487 (dockerd)
      Tasks: 43
     Memory: 86.5M
     CGroup: /system.slice/docker.service
             ├─1487 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
             ├─2182 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.23.0.2 -container-port 80
             └─2191 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8080 -container-ip 172.23.0.2 -container-port 80

You can check the docker version by running the below command:

sudo docker -v
Output:

Docker version 20.10.12, build e91ed57

Step 3: Run Docker as a non-root user

By default, The docker command can be run by only the root user or by a user who is in the docker group. Docker group is automatically created during the installation of docker.

In order to run the docker command with non-root users or without sudo you need to add the existing user to the docker group.

Add your existing user to the docker group by using the below command:

sudo usermod -aG docker $USER

The user is added to the docker group, run this command when login with the desired user profile to verify:

groups

You will get the below-like output and can see the docker group in the output.

Output:

void adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

Conclusion

In this tutorial, we install the latest version of Docker on Ubuntu 20.04 LTS Focal Fossa. Please post your suggestions if something is missing in this post.

Leave a Comment