Linuxhit

Install Docker on Ubuntu 20.04

Docker Logo

Overview – Install Docker on Ubuntu 20.04

Docker allows you to run container images and is quickly becoming the standard way to deploy applications. In this tutorial we will walk through how to install Docker on Ubuntu 20.04. And how to start a container image. If you are new to Linux, Ubuntu or Docker, this is a great tutorial to get you up and running.

This tutorial to install Docker on Ubuntu 20.04 has only been tested on 20.04. However it should be applicable to other version of Ubuntu with zero or minimal changes.

Step 1 – Find the Docker package in Ubuntu’s apt repository

The name of the Ubuntu package for Docker is docker.io. But just to confirm it hasn’t changed lets use the apt search command to find the package.

apt search docker.io

This command searches the Ubuntu package repository cache for packages containing docker.io in the name and description. You will see out put similar to this:

Sorting… Done
Full Text Search… Done
docker-doc/focal-updates,focal-security 19.03.8-0ubuntu1.20.04.1 all
Linux container runtime -- documentation

docker.io/focal-updates,focal-security 19.03.8-0ubuntu1.20.04.1 amd64
Linux container runtime

python3-docker/focal 4.1.0-1 all
Python 3 wrapper to access docker.io's control socket

ruby-docker-api/focal 1.22.2-1 all
Ruby gem to interact with docker.io remote API

We are looking for the second entry docker.io, Linux Container Runtime.

Step 2 – Install the Docker on Ubuntu 20.04

To install the Docker package on Ubuntu 20.04 we will use the apt install command:

sudo apt install docker.io

You will see output indicating all the packages that will be installed as a result. These packages are dependencies of the Docker.io package. Additionally you will be prompted to confirm the installation. The output will look similar to this:

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
libgd3 libxpm4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base libidn11 pigz runc ubuntu-fan
Suggested packages:
ifupdown aufs-tools debootstrap docker-doc rinse zfs-fuse | zfsutils
The following NEW packages will be installed:
bridge-utils cgroupfs-mount containerd dns-root-data dnsmasq-base docker.io libidn11 pigz runc ubuntu-fan
0 upgraded, 10 newly installed, 0 to remove and 35 not upgraded.
Need to get 69.7 MB of archives.
After this operation, 334 MB of additional disk space will be used.

Package installation can take some time. Hence, if your connection is slow you might be waiting minutes or longer for all the packages to download.

Step 3 – Start and enable the Docker service

Unlike some package installations, the Docker service will not be running or enable to start at boot. As a result we must start it manually and configure it start on boot. We can do that with the systemctl command.

First let’s enable Docker to start on boot:

sudo systemctl enable docker

After running systemctl enable docker you should see output similar to this:

Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.

Docker will start on your next reboot. Next let’s start the Docker service so we can explore it without rebooting. Again we will use systemctl:

sudo systemctl start docker

If this command executes without error congrats! Docker should be running. But lets confirm just to be sure by looking at the process table. Use a combination of the ps and grep commands. The ps command lists the process table. We pipe the output of ps to the grep command so we can search for the string docker. Run the command pipeline as follows:

ps aux | grep docker

You should see output similar to the out from process table. In my case the docker process is running with a PID of 31130:

ps aux | grep docker
root 31130 0.3 8.1 861288 81660 ? Ssl 22:39 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Step 4 – Pull a Docker container image

Before we boot our first Docker container we must download an image. We will use the NGINX webserver image. This image runs the popular NGINX server and is based on Alpine Linux. Alpine is a distribution commonly used in container images. Because of its small size and optimizations it makes a great base distribution for building container images. Additionally its small size means we won’t be waiting very long for our download.

To get the image we will use the docker pull command:

sudo docker pull nginx

The result of the command should be similar to this:

Using default tag: latest
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete 5a9f1c0027a7: Pull complete b5c20b2b484f: Pull complete
166a2418f7e8: Pull complete
1966ea362d23: Pull complete
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest

The image digest hash might be different because the image gets updated frequently. Now let’s use the docker image command to confirm the image has been pulled and installed to our local Docker registry:

sudo docker image ls

The output should be similar to this:

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 11 days ago 133MB

The NGINX image has been pulled down to our Docker host. As you can see the image size is only 133MB.

Step 5 – Run a Docker Container

The moment you have been waiting for, running your first container! To start a container we tell Docker daemon to create a new container based on the NGINX image we installed. We do this using the docker container create command:

sudo docker container create nginx

This command will return the ID of the container created. Next we use the docker ps command to view running containers:

sudo docker ps

It should return an empty list that looks like this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Why is the list empty? Because we created the container but we have not started it yet. However if we pass docker ps the -a flag it will show us all containers. Even stopped containers:

sudo docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea48a441d83b nginx "/bin/sh" 7 minutes ago Exited (0) 54 seconds ago vibrant_boyd

To start the container we use the docker start command:

sudo docker start ea48a441d83b

You will need to replace the container id (ea48a441d83b) with the ID of your container. Which you can get from the docker ps -a command.

Check the docker ps command again. The container should be running now:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2f842b42f99 nginx "/docker-entrypoint.…" 17 seconds ago Up 2 seconds 80/tcp romantic_ishizaka

Step 6 – Run a command in the container

Your NGINX container is running. Let’s run a command inside the container and confirm we see the NGINX process. To do this let’s use the docker exec command:

sudo docker exec -it e2f842b42f99 ls

Breaking down this command, -it are flags telling docker to run in interactive mode and to allocate a tty. Then we pass in the container ID and finally the ls command which runs in the container.

Learn more about Docker

Looking to increase your Docker skill set? We highly recommend the following books on Docker:

Install Docker on Ubuntu 20.04 – Conclusion

You now have a basic NGINX container running on your Linux system. But this is just the tip of the iceberg. We will be following up with more tutorials that explain how to customize the NGINX image to include your app or website. And additionally how to expose it on the network.