How To Install Docker Engine on Ubuntu 20.04 LTS

How To Install Docker Engine on Ubuntu 20.04 LTS

It provides all the steps required to install the most recent version of Docker Engine on Ubuntu 20.04 LTS. It also provides the steps to run the first image i.e. Hello World to verify the Docker Engine on Ubuntu.

September 26, 2021

Docker is a tool that can be used to set up different environments having its own set of applications in a container without installing the applications on the main system. This makes it easy to develop multiple projects on the same system without installing the project-specific applications on the system. Docker can be used to run applications in lightweight containers without interfering with the system configurations and software installed on the system.

This tutorial provides all the steps required to install the most recent version of Docker Engine on Ubuntu 20.04 LTS. It also provides the steps to remove the existing installation of Docker and run the first image i.e. Hello World to verify that Docker Engine is successfully installed on Ubuntu.

Prerequisites

The Ubuntu versions on which we can install Docker Engine include Ubuntu 21.04, Ubuntu 20.04 LTS, and Ubuntu 18.04 LTS. Also, make sure that you have installed Ubuntu on machines having x86_64 architecture. The system architectures supported by Docker Engine include x86_64 (or amd64), armhf, arm64, and s390x. This tutorial provides the steps to install Docker Engine on x86_64 architecture.

Uninstall Existing Docker Engine

In case you have an existing version of Docker Engine (docker, docker.io, or docker-engine) installed on your system, you can remove it using the command as shown below.

# Remove existing version of Docker among docker, docker.io, or docker-engine
sudo apt-get remove docker docker-engine docker.io containerd runc

In case none of the packages are installed, you might get the output as shown below.

# Output
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package docker-engine

Install Docker Engine using Docker Repository

We can install Docker Engine using the Docker Repository to have ease of upgrades in the future. Also, this is the recommended approach to install Docker Engine on Ubuntu.

# Refresh the repositories
sudo apt-get update

# Install packages required to allow apt to use repository over HTTPS
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release

# Auto remove unused packages
sudo apt autoremove

Now import the Docker's official GPG key using the curl command as shown below.

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

Setup the stable repository using the command as shown below.

echo "deb [arch=amd64 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

Now install the most recent version of Docker Engine using the commands as shown below.

# Refresh the repositories
sudo apt-get update

# Install most recent version of Docker Engine
sudo apt-get install docker-ce docker-ce-cli containerd.io

We can also install a specific version of Docker Engine using the commands as shown below.

# List available versions
apt-cache madison docker-ce

# Output
docker-ce | 5:20.10.8~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.7~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.6~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.5~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

# Command to install the selected version
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

# Example
sudo apt-get install docker-ce=5:20.10.6~3-0~ubuntu-focal docker-ce-cli=5:20.10.6~3-0~ubuntu-focal containerd.io

Now check the status of Docker Engined as shown below.

# Docker Engine status
sudo systemctl status docker

# Output
docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-09-25 22:28:50 PDT; 4min 7s ago TriggeredBy: docker.socket Docs: https://docs.docker.com Main PID: 189931 (dockerd) Tasks: 11 Memory: 33.6M CGroup: /system.slice/docker.service └─189931 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Hello World

In this step, we will verify whether Docker Engine is installed successfully by running the Hello World Image. We can run the Hello World image using the command as shown below.

# Run Hello World Image
sudo docker run hello-world

# Output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

----
----

For more examples and ideas, visit:
https://docs.docker.com/get-started/

The above-mentioned command will download the Hello World Image and runs it in a container. The download will be done on the first run and it might take some time. The subsequent runs will be faster. It will simply print a message and exit.

This completes the installation and verification of the Docker Engine on Ubuntu systems.

Change the Data Path

We might be required to change the path to store the images and containers on a drive different than the root drive. It can be done by updating /etc/default/docker as shown below.

# Stop Docker Service
sudo service docker stop

# Make sure that Docker is not running on your system
ps faux

# Open file using nano editor
sudo nano /etc/default/docker

# Update the File
# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /data2/docker"

# Restart Docker
sudo service docker restart

In my case, I have specified the data path as /data2/docker. It might be different on your system based on your selection. Now press Ctrl + O to write the changes and Ctrl + X to exit the nano editor.

The above-mentioned approach might not work out in case Docker has already generated data in the /var/lib/docker directory. In such cases, we can move the data from the /var/lib/docker directory to another directory and add a symlink to the new directory as shown below. Also, undo the changes made in the previous steps by updating /etc/default/docker.

Make a note of the directory structure of /var/lib/docker.

# Stop Docker Service
sudo service docker stop

# Make sure that Docker is not running on your system
ps faux

# Backup the existing data in /var/lib/docker
sudo tar -zcC /var/lib docker > /data2/var_lib_docker-backup-$(date +%s).tar.gz

# Move /var/lib/docker to new directory
sudo mv /var/lib/docker /data2

# Create Symlink
sudo ln -s /data2/docker /var/lib/docker

# Restart Docker Service
sudo service docker restart

# Run Hello World
sudo docker run hello-world

Now try to open the new Docker data directory using the symbolic link /var/lib/docker. The directory structure should be the same before taking the backup and creating the symbolic link.

Commands to add/remove Images and Containers

This section lists the commands required to add or remove the images and containers.

# Add and run Image
sudo docker run <image>

# Example
sudo docker run hello-world

# List Containers
sudo docker container ls -a

# Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92f8d1c64068 hello-world "/hello" 9 minutes ago Exited (0) 9 minutes ago reverent_lumiere

# Remove Container
sudo docker container rm <container id>

# Example
sudo docker container rm 92f8d1c64068

# Remove Image
sudo docker image rm <image>

# Example
sudo docker image rm hello-world

We can remove everything including stopped containers, networks not used by at least one container, unused images, and build cache using the prune command as shown below.

# Stop Docker
sudo service docker stop

# Remove everything
sudo docker system prune -a

# Output
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N] y Deleted Containers: 4b62b996278d74b1fc064a50acb41573b51e1a72c619f865613f10f2b09751db Deleted Images: untagged: hello-world:latest untagged: hello-world@sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0 deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359 Total reclaimed space: 13.26kB

# Start Docker
sudo service docker start

Uninstall Docker Engine

We can uninstall and completely remove Docker Engine from Ubuntu using the command as shown below.

# Uninstall and remove Docker Engine from Ubuntu
sudo apt-get purge docker-ce docker-ce-cli containerd.io

Start Docker Rootless

In several case, we might not be required to run Docker using the root user and access it using sudo. In such cases, we can create a separate group and add users to it to run Docker without root privileges.

# Stop Docker
sudo service docker stop

# Create Docker Group
sudo groupadd docker

# Add User - Replace username and password with actual one
sudo useradd -m -s /bin/bash username

# Set Password
sudo passwd username

# Output
New password:
Retype new password:
passwd: password updated successfully

# Add User to Docker group
sudo usermod -aG docker username

Now logout from the system and login back using the newly created user. Also, use the below mentioned commands to test the new user.

# Switch User
su - username

# Run Docker image without sudo
docker run hello-world

# Output

Hello from Docker!
This message shows that your installation appears to be working correctly.

You must be able to run the Docker images without the sudo prefix.

Summary

This tutorial provided the steps required to install the most recent version of Docker Engine on Ubuntu 20.04 LTS. It also provided the steps to confirm the installing by installing the Hello World Image.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS