Install Docker and Docker Compose on Ubuntu

4 min read

DevOpsHomeLabDockerDocker ComposeLinuxUbuntu

Install Docker Engine and Compose from the official repository on Ubuntu, and the traps I learned over time: the firewall, unbounded logs and endless restarts.

Contents

This guide installs Docker Engine and the Docker Compose plugin from Docker’s official repository on Ubuntu. It is preferable to the distribution packages because it receives new versions and security fixes sooner. It is the same method I use in my homelab, and at the end I describe the problems it gave me and how to avoid them.

In short, once the official repository is added:

sudo apt install docker-ce docker-compose-plugin
sudo usermod -aG docker $USER
docker compose up -d

Requirements

  • Ubuntu Server on a supported LTS version (22.04, 24.04 or 26.04), 64-bit.
  • A user with sudo permissions.
  • No unofficial packages such as docker.io, containerd or runc installed; they conflict with the official ones.
sudo apt remove docker.io containerd runc

Uninstalling does not delete your images, containers or volumes in /var/lib/docker/.

Add the official repository

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

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

sudo apt update

The docker.sources file is APT’s current format. In many older guides you will see a single deb ... line inside docker.list; it works the same, but the official documentation now uses the new format.

Install the packages

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

Check the installation

sudo systemctl status docker
sudo docker run --rm hello-world
docker compose version

Use Docker without sudo

sudo usermod -aG docker $USER
newgrp docker

newgrp docker only applies the change in the current terminal; to make it hold everywhere, log out and back in. Being in the docker group is equivalent to having administrator access on the machine. Do it only on servers you control, or consider rootless mode.

If you later get a permissions error on ~/.docker, it is usually because you ran something with sudo earlier; fix it by taking ownership back with sudo chown "$USER":"$USER" ~/.docker -R.

First compose.yaml

An example with the three things I always add:

services:
  web:
    image: nginx:stable-alpine
    ports:
      - "127.0.0.1:8080:80"
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1/"]
      interval: 30s
      timeout: 5s
      retries: 3
    logging:
      driver: json-file
      options:
        max-size: "50m"
        max-file: "3"
docker compose up -d
curl -I http://127.0.0.1:8080
docker compose ps
docker compose down

What each one does and why:

  • 127.0.0.1:8080:80: publishes the port on localhost only. It is what you want when a reverse proxy sits in front.
  • healthcheck: Docker checks every 30 seconds that the service answers and marks it healthy or unhealthy.
  • logging: caps the log size, covered in the next section.

The traps I ran into

Published ports bypass ufw

When you publish a container port, Docker writes its own iptables rules, and those ports bypass ufw or firewalld rules. The official documentation warns about it and says your own rules go in the DOCKER-USER chain. In practice: a ufw deny does not protect a port published with -p 8080:80. Two simple defences: publish on 127.0.0.1 when you only need local access, and check with sudo ss -tlnp what listens on 0.0.0.0.

Logs do not rotate by default

Without a /etc/docker/daemon.json or a logging block in each service, a container’s log grows without limit. One of my containers stuck in a loop reached 1.2 GB of log. Besides the per-service logging block, you can set it for the whole daemon:

{
  "log-driver": "json-file",
  "log-opts": { "max-size": "50m", "max-file": "3" }
}

Restarting Docker with sudo systemctl restart docker restarts every container, so do it at a quiet time.

restart: always and endless restarts

With a restart policy, if a service fails on startup Docker relaunches it forever. It happened to me with a server that restarted more than 8,000 times in three days, burning CPU, and from outside it looked like it was working because another process kept the port open. Two defences: add a healthcheck and look at the restart counter from time to time:

docker inspect --format '{{.RestartCount}}' NAME

(docker ps --format does not support .RestartCount: it returns empty without warning.)

Careful with docker volume prune

It removes volumes that no container uses (with -a, every unused one), and a volume without a container may still hold data you need. For important data I prefer a bind mount (a folder on the server) instead of a volume: you see it with ls, you copy it with any backup tool, and docker volume prune does not touch it.

Next steps

With Docker ready, the usual next step is to put a reverse proxy in front of the containers to serve them over HTTPS. I cover that in Nginx as a reverse proxy with HTTPS and Certbot.

Copyright: CC BY-NC-ND 4.0 · José Ángel Alejo Sillero

Contents