How to Install Docker on Ubuntu 22.04 and 24.04

By
Marko Aleksic
Published:
February 6, 2025

Docker is a platform that allows users to package applications and their dependencies into lightweight, portable, and isolated environments called containers. Multiple containers can simultaneously run on a single machine to promote efficient use of resources.

This article will show you how to install Docker on Ubuntu 22.04 and 24.04.

How to install Docker on Ubuntu 22.04 and 24.04.

Prerequisites

Note: The following tutorial uses Ubuntu 24.04 to illustrate the steps. However, the same procedure works on Ubuntu 22.04.

How to Install Docker on Ubuntu

To install Docker on Ubuntu 22.04 or 24.04, add the official Docker repository, install the required packages, and complete the post-installation configuration. The following steps provide a detailed guide.

Step 1: Add Docker's GPG key

Add Docker's GPG key to verify package authenticity and ensure software integrity. Take the following steps to install the key:

1. Refresh the package list:

sudo apt update

2. Install the necessary packages:

sudo apt install ca-certificates curl

When prompted, type Y and press Enter to begin the installation.

Installing the ca-certificates and curl packages on Ubuntu.

3. Set the necessary permissions for the /etc/apt/keyrings directory:

sudo install -m 0755 -d /etc/apt/keyrings

4. Use the curl command to download the GPG key:

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

5. Change permissions of the key file:

sudo chmod a+r /etc/apt/keyrings/docker.asc

Step 2: Add Docker Repo to APT Sources

Add the official Docker repository to the APT sources list on the system by using the following procedure:

1. Execute the command below:

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

The command uses the GPG key to authenticate and add the repository to the system.

2. Refresh the package list:

sudo apt update

The Docker repository appears in the output.

The official Docker repo in the APT sources list.

Step 3: Install Docker Packages

Enter the command below to install Docker Engine and the necessary dependencies:

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

The command installs the engine, Docker CLI, containerd.io runtime, Docker Compose, and the buildx plugin.

Step 4: Enable Non-Root User Access

Once APT installs Docker on the system, use the following steps to enable Docker CLI commands for a non-root user:

Note: Skipping this step does not prevent non-root users from using Docker. However, each command needs to be preceded by the sudo command.

1. Create the docker group if it does not already exist:

sudo groupadd -f docker

2. Add the current user to the docker group:

sudo usermod -aG docker $USER

To add a user other than the current user, type the relevant username:

sudo usermod -aG docker [username]

3. Apply the group membership changes:

newgrp docker

4. Type a docker command to test that it can run without sudo. For example, list the running containers:

docker ps

The empty list appears.

An empty list of running containers.

How to Use Docker on Ubuntu

The following sections provide usage tips and cover essential Docker commands for managing images, containers, and volumes on Ubuntu.

How to Run Docker Commands

Note: Read our Docker Commands Cheat Sheet and download the PDF resource with the essential Docker commands.

Use Docker CLI to issue commands to the Docker daemon. The basic syntax is as follows:

docker [subcommand] [options] [arguments]

Combining subcommands, options, and arguments allows the creation of efficient Docker commands to streamline complex workflows. For example, the docker run command below deploys a container based on the latest Nginx image:

docker run -d -p 8080:80 -v /my/data:/var/www/html --name test_server -e "TEST_VAR=hello" nginx:latest

The command creates and runs a detached container with a specific name, port mapping, volume mount, and environment variable.

For optimal Docker command execution, consider the following tips:

  • Use Docker Compose for multi-container applications. Docker Compose simplifies defining and managing complex apps by using a YAML file to describe the services, networks, and volumes.
  • Clean up regularly. Remove unused images, containers, and volumes to free up disk space. Use commands like docker system prune to remove dangling resources.
  • Control container resources. Use resource constraints (e.g., --cpus, --memory) with docker run to limit the resources available to a container and prevent it from hogging all system resources.
  • Check Docker logs. Use docker logs to troubleshoot issues with your containers.
  • Run containers as a non-root user. Running as root reduces isolation and exposes the system to privilege escalation risks.
  • Use a minimal base image. Large base images increase the attack surface.

How to Work with Docker Images

Docker images are the read-only templates used to create and run containers. Docker CLI includes many subcommands for manipulating images. Some of the essential ones include:

  • Find pre-built images in a public registry, such as Docker Hub:
docker search [search_term]
  • Pull an image from a registry to make it available locally:
docker pull [repository]/[image]
  • See a list of locally available images:
docker images
  • Push an image to a registry:
docker push [registry]/[username]/[image]:[tag]
docker build -t [image]:[tag] .

How to Work with Docker Containers

Working with Docker containers involves managing their lifecycle, from creation to removal. Below are the commands for creating, running, and interacting with Docker containers:

docker run [image]
docker ps
  • View detailed information about a container, including its configuration, network settings, and more:
docker inspect [container_id_or_name]
  • Execute a command inside a running container:
docker exec [container_id_or_name] [command]
  • Remove a stopped container:
docker rm [container_id_or_name]

How to Work with Docker Volumes

Docker volumes are a way to store data used by Docker containers. They are important for applications that need data persistence, like databases or file servers. The following list presents the commands for working with volumes:

  • Create a volume before starting a container:
docker volume create [volume_name]
  • Create a volume implicitly at runtime or mount an existing volume:
docker run -v [volume_name]:[path_inside_container] [image]
  • Inspect a volume:
docker volume inspect [volume_name]
  • Remove volumes not being used by any container:
docker volume prune

Conclusion

This tutorial explained how to install Docker on Ubuntu 22.04 and 24.04. It provided tips for running Docker commands and briefly introduced you to working with the essential Docker objects.

If you are still looking for the best containerization solution for your system, read our Podman tutorial and learn about a Docker alternative. If you want to know how the two solutions compare, read the Podman vs. Docker article.

Was this article helpful?
YesNo