Cyberithub

How to Install and Use Docker on Ubuntu 20.04 LTS [Step by Step]

Advertisements

In this article, I will take you through the steps to install and use docker on Ubuntu 20.04. Before the rise of Docker Containers, the concepts of virtual machine were quite popular to run multiple applications in different virtual machines, all running on a single host machine. But over the time, developers realized that if the end goal is to deploy and run an application then why wasting resources in creating OS and its processes which might not helping the end application.

This has revolutionized the entire thinking and led to the creation of something called containers in which application can run independently without having the need to create OS Infrastructure. Docker is known to be the Container Management Solution which effectively create, manage and destroy containers. More on Docker Official website.

What is Docker Hub

Docker Hub is a public repository where latest docker images of various applications are stored. By default, docker always pulls images from Docker Hub Repo.

How to Install and Use Docker on Ubuntu 20.04 LTS [Step by Step]

How to Install and Use Docker on Ubuntu 20.04 LTS

Also Read: How to Install and Use Docker on Debian 11 [Easy Steps]

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your System.

d) You should also have curl, gpg and tee utility installed in your System.

 

Step 2: Uninstall Older Versions

If you have any older version of docker engine then it is always recommended to first uninstall that version and its related components using apt-get remove docker docker-engine docker.io containerd runc command.

root@localhost:~# apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
Package 'docker' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'runc' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
...........................................

 

Step 3: Update Your Server

It is essential to run System update using apt update or apt-get update command to let the System know if there are any updates or a new package available in the repo.

root@localhost:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:3 http://ppa.launchpad.net/hluk/copyq/ubuntu focal InRelease
Hit:4 https://download.docker.com/linux/ubuntu focal InRelease
Ign:5 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Hit:7 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 Release
Get:8 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:9 https://artifacts.elastic.co/packages/oss-7.x/apt stable InRelease
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [278 kB]

 

Step 4: Set Up Dependencies

There are certain set of packages which is going to be required for the installation of docker needs to be installed first. These includes ca-certificates, curl, gnupg and lsb-release package. You can install all these packages by using apt-get install ca-certificates curl gnupg lsb-release command as shown below.

root@localhost:~# apt-get install ca-certificates curl gnupg lsb-release
Reading package lists... Done
Building dependency tree
Reading state information... Done
lsb-release is already the newest version (11.1.0ubuntu2).
lsb-release set to manually installed.
ca-certificates is already the newest version (20210119~20.04.2).
curl is already the newest version (7.68.0-1ubuntu2.7).
gnupg is already the newest version (2.2.19-3ubuntu2.1).
gnupg set to manually installed.

 

Step 5: Add Docker GPG Key

Next step is to add the Docker official GPG public Key using below command. This is required to validate the checksum and integrity of the image.

root@localhost:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

 

Step 6: Setup Stable Repo

You can setup different kinds of repositories like nightly, test and stable to download the docker package depending on your requirement. Here we are setting up the stable repository using below command.

root@localhost:~# 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

 

Step 7: Install Docker

Then to download and install docker and other required packages, you need to run apt-get install docker-ce docker-ce-cli containerd.io command as shown below.

root@localhost:~# apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
i965-va-driver intel-media-va-driver kwayland-data kwayland-integration libaacs0 libaom0 libass9 libavcodec58 libavdevice58 libavfilter7 libavformat58
libavresample4 libavutil56 libbdplus0 libbluray2 libbs2b0 libchromaprint1 libcodec2-0.9 libdbusmenu-qt5-2 libdc1394-22 libfam0 libfftw3-double3 libflite1
mesa-vdpau-drivers va-driver-all vdpau-driver-all
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
docker-scan-plugin pigz
Suggested packages:
aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
containerd.io docker-ce docker-ce-cli docker-scan-plugin pigz
0 upgraded, 5 newly installed, 0 to remove and 43 not upgraded.
Need to get 3,575 kB/89.2 MB of archives.
After this operation, 388 MB of additional disk space will be used.
.................................................

 

Step 8: Check Docker Version

After successful installation, you can verify the current installed version by using docker --version command as shown below.

root@localhost:~# docker --version
Docker version 20.10.12, build e91ed57

 

Step 9: Pull a Docker Image

Now you can pull docker images from Docker Hub Image library using docker pull command. Here we are downloading latest ubuntu image using docker pull ubuntu command as shown below.

NOTE:

It is important to note here that if you don't specify any tag with the image then by default it will take the latest tag as you might have noticed from the below shown output.
root@localhost:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

 

Step 10: List Docker Images

You can list all the images by using docker images command as shown below.

root@localhost:~# docker images
REPOSITORY                          TAG    IMAGE ID       CREATED    SIZE
goharbor/harbor-exporter            v2.4.1 0ee361b5454a   2 days ago 82.7MB
goharbor/chartmuseum-photon         v2.4.1 337a9d03b197   2 days ago 174MB
goharbor/redis-photon               v2.4.1 2bbe19b8cc5d   2 days ago 156MB
goharbor/trivy-adapter-photon       v2.4.1 d39fd42c93f0   2 days ago 151MB

 

Step 11: Run Ubuntu Container

It is now time to run a container using docker image. Here we are using ubuntu image to run our container. You just need to use docker run -it ubuntu bash command as shown below.

root@localhost:~# docker run -it ubuntu bash
root@a670087faaa0:/#

 

Step 12: Remove Docker Engine

Once you are done with docker images and containers, you can remove all its components by using apt-get remove docker-ce docker-ce-cli containerd.io command as shown below.

root@localhost:~# apt-get remove docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libsndio7.0 libsord-0-0 libsratom-0-0 libssh-gcrypt-4 libswresample3 libswscale5 libtie-ixhash-perl libva-drm2 libva-x11-2 libva2 libvdpau1 libvidstab1.1
libx264-155 libx265-179 libxml-xpathengine-perl libxvidcore4 libzmq5 libzvbi-common libzvbi0 linux-headers-5.11.0-40-generic
linux-hwe-5.11-headers-5.11.0-40 linux-image-5.11.0-40-generic linux-modules-5.11.0-40-generic linux-modules-extra-5.11.0-40-generic mesa-va-drivers
mesa-vdpau-drivers va-driver-all vdpau-driver-all
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
containerd.io docker-ce docker-ce-cli
0 upgraded, 0 newly installed, 3 to remove and 43 not upgraded.
After this operation, 375 MB disk space will be freed.
Do you want to continue? [Y/n] Y

Leave a Comment