How to Install Docker on Ubuntu

Updated on

4 min read

Install Docker on Ubuntu 22.04

Docker is a highly versatile open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run anywhere, from local machines to cloud-based servers.

A container represents a runtime for a single application that includes all the necessary software components and dependencies for the application to run smoothly, isolated from the host operating system.

Docker is an integral part of modern software development and DevOps continuous integration and deployment pipelines.

This tutorial explains how to install Docker on Ubuntu.

Docker is available for installation from the standard Ubuntu repositories, but it may not always be the latest version. We’ll install the latest Docker package from the official Docker’s repositories. At the time of writing, the Docker repository includes packages for the following Ubuntu versions:

  • Ubuntu 23.10
  • Ubuntu 22.04
  • Ubuntu 20.04

Installing Docker on Ubuntu

Installing Docker on Ubuntu is relatively straightforward. We’ll enable the Docker repository, import the repository GPG key, and install the package.

First, update the packages index and install the dependencies necessary to add a new HTTPS repository :

sudo apt updatesudo apt install ca-certificates curl gnupg

Next, import the Docker repository’s GPG key to your system:

sudo mkdir -p /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

Add the Docker APT repository to your system:

echo \  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \  sudo tee /etc/apt/sources.list.d/docker.list

$(lsb_release -cs) will print the Ubuntu codename. For example, if you have Ubuntu version 22.04 the command will print jammy.

Now that the Docker repository is enabled, you can install any Docker version that is available in the repositories.

To install the latest version of Docker, run the commands below. If you want to install a specific Docker version, skip this step and go to the next one.

sudo apt updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Installing Specific version

If you want to install a specific version, first list all the available versions in the Docker repository:

sudo apt updateapt list -a docker-ce

The available Docker versions are printed in the second column:

docker-ce/jammy 5:26.0.0-1~ubuntu.22.04~jammy amd64
docker-ce/jammy 5:25.0.5-1~ubuntu.22.04~jammy amd64
docker-ce/jammy 5:25.0.4-1~ubuntu.22.04~jammy amd64
docker-ce/jammy 5:25.0.3-1~ubuntu.22.04~jammy amd64
docker-ce/jammy 5:25.0.2-1~ubuntu.22.04~jammy amd64
docker-ce/jammy 5:25.0.1-1~ubuntu.22.04~jammy amd64
...

Install a specific version by adding =<VERSION> after the package name. For example to install version “5:24.0.7-1~ubuntu.23.10~mantic” you would type:

DOCKER_VERSION=5:24.0.7-1~ubuntu.23.10~manticsudo apt-get install docker-ce=$DOCKER_VERSION docker-ce-cli=$DOCKER_VERSION containerd.io docker-buildx-plugin docker-compose-plugin

Once the installation is completed, the Docker service will start automatically. You can verify it by typing:

sudo systemctl status docker

The output will look something like this:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-04-02 20:27:00 UTC; 41s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
...

When a new version of Docker is released, you can update the 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 held back:

sudo apt-mark hold docker-ce

Executing Docker Commands as a Non-Root User

By default, only root and user with sudo privileges can execute Docker commands.

To execute Docker commands as a non-root user, you’ll need to add your user to the docker group that is created during the installation of the Docker CE package. To do that, type in:

sudo usermod -aG docker $USER

$USER is an environment variable that holds the currently logged-in username. If you want to execute commands with other users, run the command above replacing, $USER with the username.

Log out and log back in so that the group membership is refreshed.

Verifying the Installation

To verify that Docker has been successfully installed and that you can execute the docker command without prepending sudo , we’ll run a test container:

docker container run hello-world

If not found locally, the command will download the test image, run it in a container, print a “Hello from Docker” message, and exit. The output should look like the following:

Docker Hello World

The container will stop after printing the message because it has nolong-running process.

By default, Docker pulls images from the Docker Hub. It is a cloud-based registry service which among other functionalities, stores the Docker images in public or private repositories.

Uninstalling Docker

Before uninstalling Docker it is a good idea to remove all containers, images, volumes, and networks .

Run the following commands to stop all running containers and remove all docker objects:

docker container stop $(docker container ls -aq)docker system prune -a --volumes

You can now uninstall Docker as any other package installed with apt:

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extrassudo apt autoremove

To completely remove Docker from your system, you can also delete the directories that were created during the installation process:

sudo rm -rf /var/lib/{docker,containerd}

Conclusion

We’ve shown you how to install Docker on your Ubuntu machine. To learn more about Docker, check out the official Docker documentation .

If you have any questions, please leave a comment below.