Skip to content

Docker & Docker Compose — Installation

Docker is a container runtime that packages applications and their dependencies into isolated units called containers. Docker Compose is a companion tool that manages multi-container setups — a database, a backend, and a reverse proxy can all be defined in a single compose.yaml and started with one command.

This guide uses the official Docker apt repository, not the Ubuntu snap package or the convenience install script. The apt method gives you the current stable Docker Engine, the ability to pin versions, and clean upgrades through the normal apt upgrade workflow.

Security: the docker group grants root-level access

Any user in the docker group can mount the host filesystem, modify running containers, and escalate to root. Only add users you fully trust.


Prerequisites

  • Ubuntu 22.04 LTS (Jammy) or 24.04 LTS (Noble) — amd64 or arm64
  • Root or sudo access
  • A user account that will run Docker (created in this guide if it does not exist yet)

Manual Installation

1 — Add Docker's apt Repository

Install the required tools and import Docker's GPG signing key:

sudo apt update
sudo apt install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Add the Docker repository in deb822 format:

. /etc/os-release
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: ${UBUNTU_CODENAME:-$VERSION_CODENAME}
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt update

The . /etc/os-release line reads your Ubuntu version so the correct repository suite is set automatically — no manual substitution needed.

2 — Install Docker Engine and Compose

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

This installs Docker Engine (docker-ce), the CLI, containerd as the container runtime, the Buildx builder plugin, and Docker Compose as a CLI plugin. After this, Docker Compose is invoked as docker compose (with a space), not the legacy docker-compose.

3 — Enable Docker on Boot

sudo systemctl enable --now docker

4 — Create the User

Skip this step if the user already exists. Create a new system user with a home directory and a login shell:

sudo useradd -m -s /bin/bash myuser
sudo passwd myuser

Replace myuser with the actual username.

5 — Add the User to the Docker Group

sudo usermod -aG docker myuser

The group change takes effect at the next login. To activate it immediately in the current terminal session:

newgrp docker

6 — Verify

Switch to the user and run the Docker test image:

su - myuser
docker run hello-world
docker compose version

Both commands should succeed without sudo. The hello-world container prints a confirmation message and exits.


Automated Script

The script below performs the complete installation in one pass: repository setup, Docker Engine install, user creation (if needed), and docker group assignment. Run it as root or with sudo.

#!/bin/bash
set -euo pipefail

DOCKER_USER="${1:?Usage: sudo bash install-docker.sh <username>}"

apt-get update -qq
apt-get install -y ca-certificates curl

install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc

. /etc/os-release
tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: ${UBUNTU_CODENAME:-$VERSION_CODENAME}
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

apt-get update -qq
apt-get install -y \
  docker-ce \
  docker-ce-cli \
  containerd.io \
  docker-buildx-plugin \
  docker-compose-plugin

systemctl enable --now docker

if ! id "$DOCKER_USER" &>/dev/null; then
  useradd -m -s /bin/bash "$DOCKER_USER"
  passwd "$DOCKER_USER"
fi

usermod -aG docker "$DOCKER_USER"

echo ""
echo "Done. $DOCKER_USER has been added to the docker group."
echo "Log in as $DOCKER_USER and run: docker run hello-world"

Save the file as install-docker.sh and run it:

sudo bash install-docker.sh myuser

The script exits immediately on any error (set -euo pipefail). If it fails partway through, fix the reported error and run it again — apt installs are idempotent.

After it finishes, log in as the target user and verify:

su - myuser
docker run hello-world
docker compose version

Running a First Container

A quick example to confirm everything works end-to-end. This runs an nginx web server and maps it to port 8080 on the host:

docker run -d --name nginx-test -p 8080:80 nginx

Test it:

curl http://localhost:8080

You should see the nginx welcome page HTML. Stop and remove the container:

docker stop nginx-test
docker rm nginx-test

Uninstall

Remove Docker packages and all locally stored images, containers, and volumes:

sudo apt purge -y \
  docker-ce \
  docker-ce-cli \
  containerd.io \
  docker-buildx-plugin \
  docker-compose-plugin
sudo rm -rf /var/lib/docker /var/lib/containerd
sudo rm -f /etc/apt/sources.list.d/docker.sources
sudo rm -f /etc/apt/keyrings/docker.asc

The user and docker group are not removed automatically — clean them up manually if needed:

sudo userdel -r myuser
sudo groupdel docker