This guide shows you how to get started with Docker by installing it on Ubuntu Linux 20.04 (Focal Fossa), the latest Long Term Support (LTS) version of Ubuntu at the time of this writing.

Advantages of Docker

Docker is an open-source platform that allows you to automate and deploy applications as stand-alone packages known as containers. Docker uses operating system virtualization technology but unlike virtual machines, it is much lighter on the system resources.

Some of the major advantages of Docker include:

Relatively easy to use and learn Provides a consistent and easy way of deploying software Compatible with microservice architecture Very light on resource usage

Setting Up the Docker Software Repository

There are several methods of installing Docker, and this guide will show you how to install Docker from the Docker repositories using the apt command utility. Installing Docker in this manner allows you to easily upgrade the Docker package in the future, and is also the recommended approach by the Docker team.

The first step in the installation is to add the Docker software repository to your list of software sources. You will use the Docker software repository over HTTPS, and then install the required software using the command below.

As good practice, first update your list of available software packages.

Then, download all the required dependencies for the installation using apt install.

The Docker software uses GnuPG, also known as GPG, for securing communication when downloading software packages from its repository. GPG is an implementation standard of PGP (Pretty Good Privacy) that is used for encrypting messages or data.

To add the official Docker GPG key to your local keyrings use the following command.

Docker has three main software release versions in their software repositories: stable version, test version, and the nightly release version. This guide will talk about the stable release version of Docker.

Run the following command to use the stable repository release version of Docker.

Note: The aforementioned command assumes that you are using the AMD architecture. If you happen to be using the ARM architecture you can replace the word arch=amd64  in the command above with arch=arm64, or arch=armhf if you are using arm hard float.

Installing the Docker Engine

Now that you have the Docker software repository setup, you can proceed to install the Docker Engine, which is at the core of managing and running Docker containers. Other important components that make up the Docker Engine include Docker Client, containerd, runc, and the Docker daemon.

Make sure to update your package sources using the command below, because you have recently added the Docker repository to your list of software sources.

To install the Docker Engine, run the following command. The command will by default install the latest stable version of Docker Engine.

If you wish to install some specific version of Docker, you can first check the list of available versions using the command below.

You can then install the specific version of Docker using the following command. For example, to install 5:20.10.6~3-0~ubuntu-focal:

Confirming the installation

To check if Docker has been installed successfully, you can run the following command and it will output the version number of the installed Docker Engine.

In Ubuntu Linux and most Debian-based distros, the Docker service will automatically start when your system boots.

You can try to run the hello-world Docker image to test the installation. Since the image is not available locally on your computer, the system will download it from the Docker Hub, a library of container images. The next time you run the image again it will use the local copy that is on your PC.

Running Docker As a Non-Root User

At the moment you can only run Docker containers as a superuser, that is why sudo is used in the command above. The Docker daemon binds to a Unix socket which is by default owned by the root user and non-root users can only access it via sudo.

To be able to run Docker containers and other important commands without being a superuser, you first need to create a user group named docker and then add your user to the docker group on your machine. The groupadd command is responsible for managing user groups in Linux.

Use the command below to activate group changes.

Note: Remember to log out and back in again so that the system recognizes the newly created group membership. You can use the following command to log out.

In some cases, it might be necessary to restart your machine if you are still unable to execute the Docker command as a non-root user.

Uninstalling Docker

To remove or uninstall the Docker Engine from Ubuntu Linux, simply run the following command.

Although the Docker Engine has been removed, other files associated with Docker such as images, containers, volumes, or custom configuration files are not automatically removed. You can delete these files using the rm utility.

Docker in Practise

This guide shows you how to install and configure Docker on Ubuntu Linux. In addition, it shows you how to run a simple docker image fetched from the Docker Hub.

Docker is a very versatile tool and it has many use cases in software engineering. If you are a software engineer or work in DevOps, Docker will simplify the way you deploy software in different environments and is useful for testing and prototyping software.