Docker is an open source containerization platform that allows you to quickly build, test, and use applications as portable containers that can run almost anywhere. Containers represent runtime for one application and include everything that the software needs to run.
Docker is an integral part of modern software development and the continuous integration of DevOps and the deployment pipeline.
This tutorial covers how to install Docker on an Ubuntu 20.04 machine.
Docker is available for installation from the standard Ubuntu 20.04 repository, but may not always be the latest version. We will install the latest Docker package from the official Docker repository.
Installing Docker on Ubuntu 20.04
Installing Docker on Ubuntu is quite easy. We will activate the Docker repository, import the GPG key repository, and install the package.
First, update the package index and install the dependencies needed to add a new HTTPS repository:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Import the GPG repository key using the following curl command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker APT repository to your system:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Now that the Docker repository is activated, you can install whatever version of Docker is available in the repository.
1. To install the latest version of Docker, run the command below. If you want to install a specific Docker version, skip this step and go to the next one.
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
2. To install a particular version, first list all the versions available in the Docker repository :
sudo apt update
apt list -a docker-ce
Available versions of Docker are printed in the second column. At the time of writing this article, there is only one version of Docker (5: 19.03.9 ~ 3-0 ~ ubuntu-focal) available in the official Docker repository.
docker-ce/focal 5:19.03.9~3-0~ubuntu-focal amd64
Install a particular version by adding = after the package name:
sudo apt install docker-ce=<VERSION> docker-ce-cli=<VERSION> containerd.io
After the installation is complete, the Docker service will start automatically. You can verify it by typing:
sudo systemctl status docker
The output will look like this:
docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-05-21 14:47:34 UTC; 42s ago
When a new version of Docker is released, you can update packages using the standard sudo apt update && sudo apt upgrade procedure.
If you want to prevent the Docker package from being updated, mark it as on hold:
sudo apt-mark hold docker-ce
Run Docker Commands as Non-Root Users
By default, only root and users with sudo rights can run the Docker command.
To run the Docker command as a non-root user, you must add the user to the docker group created during the Docker CE package installation. To do this, type:
sudo usermod -aG docker $USER
$ USER is an environment variable that holds your username.
Logout and log in again so that group membership is refreshed.
Verifying the Installation
To verify that Docker has been installed successfully and that you can run docker commands without adding sudo, we will run a test container:
docker container run hello-world
The command will download the test image, if not found locally, run it in the container, print the “Hello from Docker” message, and exit. The output will look like this:
The container will stop printing after the message because it does not have a long running process.
By default, Docker draws images from the Docker Hub. This is a cloud-based registry service that, among other functions, stores Docker images in public or private repositories.
Before removing Docker, you should delete all containers, images, volumes and networks.
Run the following command to stop all running containers and delete all docker objects:
docker container stop $(docker container ls -aq)
docker system prune -a --volumes
You can now uninstall Docker as another package installed with apt:
sudo apt purge docker-ce
sudo apt autoremove
We have shown you how to install Docker on an Ubuntu 20.04 machine. To learn more about Docker, see the official Docker documentation.
If you have questions, please leave a comment below.