Updated 2 min read

Install Docker and Compose on Ubuntu 24.04

Install Docker Engine and the Compose plugin from the official repository, run it as a normal user and avoid the UFW bypass.

Install Docker from Docker's own apt repository, not from the docker.io package in Ubuntu: it is newer and includes Compose.

Install#

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
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Test it:

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

Use it without sudo#

sudo usermod -aG docker $USER

Log out and back in.

Warning Membership of the docker group is the same as root: anyone in it can take over the server. Add only users you would give root to.

Docker ignores your firewall#

This catches almost everyone. Docker publishes ports with its own firewall rules, so a container started with -p 8080:80 is reachable from the whole Internet even if UFW denies the port. Publish on the loopback address instead, and put a reverse proxy in front:

docker run -d --name web -p 127.0.0.1:8080:80 nginx

In Compose, write "127.0.0.1:8080:80" under ports. Check what is really open from your own computer with nmap <ip>, not from inside the server.

Stop logs from filling the disk#

Container logs grow without limit by default, and your SSD is small. Cap them:

sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}
EOF
sudo systemctl restart docker

Existing containers need to be recreated to pick this up.

Clean up#

docker system df
docker system prune -a

prune -a removes every image that no container uses. Read the prompt before you accept.

Related reading (in English): hosting multiple websites on one VPS with a reverse proxy.

Next: set up the UFW firewall.

More in this section