DEV Community

Cover image for How to install Docker and Docker Compose on Ubuntu 24.04
Fernando Chagas for Nuvits

Posted on Originally published at nuvits.net

How to install Docker and Docker Compose on Ubuntu 24.04

This tutorial installs Docker Engine 29.10.1 and the Docker Compose v5.8.1 plugin on an Ubuntu 24.04 VPS from Docker's official apt repository, with the version pinned so that an apt upgrade does not restart your containers at a bad time. After the install it covers running Docker without sudo, capping log size, a first Compose project (Postgres and Adminer), what happens to published ports, and the upgrade to the next version. Everything was tested on 1 December 2026 on a clean Ubuntu 24.04.5 LTS machine.

Tutorial diagram: on the left, Docker Engine 29.10.1 installed on Ubuntu 24.04 from the official apt repository, with a pinned version, apt-mark hold and logs capped at 3 files of 10 MB; on the right, the first Docker Compose v5.8.1 project, with Postgres 18.7 on a volume, Adminer 6.3.1 published only on 127.0.0.1:8080 and reached through an SSH tunnel

Versions used

Component Version Source
Docker Engine 29.10.1 (24 Nov 2026) Moby releases
Docker Compose v5.8.1 (27 Nov 2026) Compose releases
Docker Buildx v0.39.1 (19 Nov 2026) Buildx releases
containerd 2.4.2 (package 2.4.2-4) containerd releases
Postgres 18.7 official image
Adminer 6.3.1 official image
System Ubuntu 24.04.5 LTS, amd64

Ubuntu ships its own package, docker.io, which was at version 29.1.3 on the day of the test. This guide uses Docker's repository instead: it gets new versions first and provides Compose and Buildx as plugins.

What you need

Docker itself uses very little. In the test, the whole example project sat at 65 MiB of RAM when idle (37 MiB for Postgres and 28 MiB for Adminer), and the two images took about 900 MB of disk.

1. Remove conflicting packages

The Docker documentation for Ubuntu asks you to remove the unofficial packages first (docker.io, docker-compose, podman-docker and others), along with any standalone containerd and runc:

sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc | cut -f1)
Enter fullscreen mode Exit fullscreen mode

A freshly created VPS has nothing to remove. dpkg answers no packages found matching for each name and apt ends with 0 to remove.

2. Add Docker's official repository

These commands download Docker's key and write the repository in the docker.sources format:

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 <<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
Enter fullscreen mode Exit fullscreen mode

tee prints the file it wrote. Check that the Suites line says noble and Architectures says amd64.

3. Install a pinned version

List the versions the repository offers:

apt-cache madison docker-ce | head -n 5
apt-cache policy containerd.io docker-buildx-plugin docker-compose-plugin | grep -E '^[a-z]|Candidate'
Enter fullscreen mode Exit fullscreen mode

On the day of the test, the first two docker-ce lines were:

 docker-ce | 5:29.10.1-1~ubuntu.24.04~noble | https://download.docker.com/linux/ubuntu noble/stable amd64 Packages
 docker-ce | 5:29.10.0-1~ubuntu.24.04~noble | https://download.docker.com/linux/ubuntu noble/stable amd64 Packages
Enter fullscreen mode Exit fullscreen mode

The second column is the version string apt expects. The second command shows, on each Candidate line, the newest version of the other three packages. Install with an explicit version for every package:

VERSION_STRING=5:29.10.1-1~ubuntu.24.04~noble
sudo apt install -y docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING docker-ce-rootless-extras=$VERSION_STRING containerd.io=2.4.2-4~ubuntu.24.04~noble docker-buildx-plugin=0.39.1-1~ubuntu.24.04~noble docker-compose-plugin=5.8.1-1~ubuntu.24.04~noble
Enter fullscreen mode Exit fullscreen mode

If the repository already has newer versions when you read this, the ones above are still available. To use the newer ones, replace the numbers with what apt-cache madison shows.

The service starts on its own and is enabled at boot:

systemctl is-active docker containerd
systemctl is-enabled docker containerd
Enter fullscreen mode Exit fullscreen mode

You should see active twice and enabled twice.

4. Hold the version

Without a hold, the next sudo apt upgrade installs the new Docker release together with the rest of the system and restarts the service in the middle of the day. Mark the packages as held:

sudo apt-mark hold docker-ce docker-ce-cli docker-ce-rootless-extras containerd.io docker-buildx-plugin docker-compose-plugin
apt-mark showhold
Enter fullscreen mode Exit fullscreen mode

In the test, on a machine with 29.10.0 held and 29.10.1 already in the repository, a simulated apt upgrade (sudo apt-get -s upgrade) listed the six packages under The following packages have been kept back and went on with the rest of the system. Upgrading Docker becomes your decision, described in Upgrading Docker.

5. Test the installation

sudo docker version --format 'server {{.Server.Version}}, client {{.Client.Version}}'
docker compose version
docker buildx version
sudo docker run --rm hello-world
Enter fullscreen mode Exit fullscreen mode

The first three lines show 29.10.1, v5.8.1 and v0.39.1. hello-world pulls a tiny image, runs it and prints:

Hello from Docker!
This message shows that your installation appears to be working correctly.
Enter fullscreen mode Exit fullscreen mode

6. Run Docker without sudo

Without sudo the command fails, because the Docker socket belongs to root:

permission denied while trying to connect to the docker API at unix:///var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode

The install creates an empty docker group. Add your user to it, then log out of the SSH session and back in:

sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

After logging back in:

id -nG
docker run --rm hello-world
Enter fullscreen mode Exit fullscreen mode

id -nG has to list docker. In the session where you ran usermod the group does not apply yet, and the error stays.

The docker group is equivalent to root. The Docker documentation warns that the group grants root-level privileges. Add only the people who already administer the VPS.

One example: only root can read /etc/shadow, but a member of the group can mount the file into a container and read it without sudo:

docker run --rm -v /etc/shadow:/host-shadow:ro alpine:3.24.4 cut -d: -f1 /host-shadow | head -n 3
Enter fullscreen mode Exit fullscreen mode

The command lists the first three user names in the file (root, daemon and bin) without showing the password hashes. A different cut would show them.

7. Cap the log size

Docker stores each container's output on disk. The default driver, json-file, does not rotate: a chatty container fills the VPS disk. The logging documentation recommends the local driver, which rotates and compresses old files.

Create /etc/docker/daemon.json (for example with sudo nano /etc/docker/daemon.json) with this content:

{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
Enter fullscreen mode Exit fullscreen mode

Values in log-opts are always strings, in quotes. Validate the file before restarting the service:

sudo dockerd --validate --config-file /etc/docker/daemon.json
sudo systemctl restart docker
docker info --format '{{.LoggingDriver}}'
Enter fullscreen mode Exit fullscreen mode

The first command answers configuration OK and the last one, local. Each container now keeps at most 3 files of 10 MB. In the test, a container that wrote non-stop levelled off at three files (container.log, container.log.1.gz and container.log.2.gz) and 22 MB in total, and docker logs kept working as before.

The setting applies to containers created after the restart. Existing ones keep the old rule until they are recreated.

8. A first project with Docker Compose

Compose describes a set of containers in one file. The example starts Postgres with its data on a volume and Adminer, a web panel for the database. It uses what almost every project needs: a pinned tag, a password kept out of the file, a volume, a healthcheck, start order and automatic restart.

Create the folder and a .env with a freshly generated password:

mkdir -p ~/apps/demo && cd ~/apps/demo
printf 'POSTGRES_PASSWORD=%s\n' "$(openssl rand -hex 24)" > .env
chmod 600 .env
Enter fullscreen mode Exit fullscreen mode

Compose reads .env from the project folder and substitutes ${POSTGRES_PASSWORD} in the file below. The :? makes the command fail when the variable is empty, instead of creating a database with no password:

name: demo

services:
  db:
    image: postgres:18.7
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?}
    volumes:
      - db_data:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  adminer:
    image: adminer:6.3.1
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:8080:8080"

volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

What each part does:

  • image with a pinned tag: the project always starts with the same version. With latest, a pull changes the version without you asking.
  • restart: unless-stopped: the container comes back after Docker or the VPS restarts, unless you stopped it yourself.
  • volumes: the database files live in the demo_db_data volume, outside the container. From Postgres 18 on, the official image expects the volume at /var/lib/postgresql, no longer at /var/lib/postgresql/data.
  • healthcheck and depends_on: Adminer starts only after pg_isready answers.
  • ports with 127.0.0.1: the panel is reachable only from inside the VPS. Postgres publishes no port at all; Adminer reaches it over the project's internal network, by the name db.

The file can be called compose.yaml or docker-compose.yml. Compose accepts both.

Validate and start:

docker compose config -q
docker compose up -d
docker compose ps
Enter fullscreen mode Exit fullscreen mode

config -q prints nothing when the file is valid. up -d pulls the images, creates the demo_default network and the volume, waits for the database to become Healthy and only then starts Adminer. ps should show db as (healthy):

NAME             IMAGE           COMMAND                  SERVICE   CREATED         STATUS                   PORTS
demo-adminer-1   adminer:6.3.1   "entrypoint.sh docke…"   adminer   8 seconds ago   Up Less than a second    127.0.0.1:8080->8080/tcp
demo-db-1        postgres:18.7   "docker-entrypoint.s…"   db        8 seconds ago   Up 7 seconds (healthy)   5432/tcp
Enter fullscreen mode Exit fullscreen mode

Check the database and the panel from inside the VPS:

docker compose exec db psql -U postgres -c 'select version();'
curl -sS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/
Enter fullscreen mode Exit fullscreen mode

psql shows PostgreSQL 18.7 and curl answers 200.

Open Adminer through an SSH tunnel

Port 8080 exists only on the VPS's localhost, so you reach it from your computer through an SSH tunnel. On your computer:

ssh -L 8080:127.0.0.1:8080 deploy@VPS_IP
Enter fullscreen mode Exit fullscreen mode

With the tunnel open, go to http://localhost:8080. On the login screen, pick PostgreSQL under System and fill in Server with db, Username with postgres, Password with the password from .env and Database with postgres.

The data outlives the container

Create a table, take the project down and bring it back up:

docker compose exec db psql -U postgres -c 'create table visits (id serial primary key, at timestamptz default now()); insert into visits default values;'
docker compose down
docker compose up -d
docker compose exec db psql -U postgres -tc 'select count(*) from visits;'
Enter fullscreen mode Exit fullscreen mode

down removes the containers and the network but keeps the volume, and the count comes back as 1.

docker compose down -v deletes the volumes. With -v, the database goes with them. Use it only when you really want to start from scratch.

In the test, both containers also came back on their own after sudo systemctl restart docker and after a machine restart, with the table intact.

9. Published ports and ufw

Docker writes its own firewall rules, and they apply before ufw's. A port published with -p 8081:80 or "8081:80" is open to the internet even with ufw active and no rule for it. The Docker documentation describes this behaviour.

In the test, with ufw active and allowing only port 22:

  • a container published on 8081:80 answered 200 to another machine;
  • an ordinary service outside Docker, on port 8082, was blocked;
  • Adminer, on 127.0.0.1:8080, did not answer from outside.

Put 127.0.0.1: in front of everything that does not need to be public, and publish no port at all for services that only talk to other containers, like the database in the example. The guide The Docker port stays open with ufw covers the diagnosis and the other ways out.

Day-to-day commands

Inside the project folder:

docker compose ps
docker compose logs --tail=50 db
docker compose exec db psql -U postgres
docker compose restart adminer
docker compose down
Enter fullscreen mode Exit fullscreen mode

With -f, logs follows the output live. exec runs a command inside the running container; leave psql with \q.

To change the version of an image, edit the tag in compose.yaml, pull and start again. The example goes from Adminer 6.3.0 to 6.3.1; use the version you have and the one you are installing:

sed -i 's|adminer:6.3.0|adminer:6.3.1|' compose.yaml
docker compose pull
docker compose up -d
Enter fullscreen mode Exit fullscreen mode

up -d recreates only the service that changed. In the test, Adminer was recreated and Postgres did not even restart.

To see what is using disk and to clean up images no container uses:

docker system df
docker image prune -a
Enter fullscreen mode Exit fullscreen mode

prune -a asks for confirmation and deletes every image without a container, including those of projects you stopped with down.

Upgrading Docker

Read the release notes and check what the repository offers:

sudo apt update
apt-cache madison docker-ce | head -n 3
Enter fullscreen mode Exit fullscreen mode

Then lift the hold, install the version you chose and hold it again. The example is the one from the test, 29.10.0 to 29.10.1:

sudo apt-mark unhold docker-ce docker-ce-cli docker-ce-rootless-extras containerd.io docker-buildx-plugin docker-compose-plugin
VERSION_STRING=5:29.10.1-1~ubuntu.24.04~noble
sudo apt install -y docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING docker-ce-rootless-extras=$VERSION_STRING containerd.io=2.4.2-4~ubuntu.24.04~noble docker-buildx-plugin=0.39.1-1~ubuntu.24.04~noble docker-compose-plugin=5.8.1-1~ubuntu.24.04~noble
sudo apt-mark hold docker-ce docker-ce-cli docker-ce-rootless-extras containerd.io docker-buildx-plugin docker-compose-plugin
docker version --format 'server {{.Server.Version}}, client {{.Client.Version}}'
docker compose ps
Enter fullscreen mode Exit fullscreen mode

The install restarts the Docker service, and the containers restart with it. In the test, Adminer stopped answering for 8 seconds, both containers came back on their own thanks to restart: unless-stopped, and the data stayed on the volume. Do the upgrade at a quiet time.

Keep containers running during the upgrade

The live-restore option keeps containers running while the Docker service is down. To use it, add the line to the daemon.json from step 7:

{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "live-restore": true
}
Enter fullscreen mode Exit fullscreen mode

This option takes effect without restarting anything:

sudo dockerd --validate --config-file /etc/docker/daemon.json
sudo systemctl reload docker
docker info --format '{{.LiveRestoreEnabled}}'
Enter fullscreen mode Exit fullscreen mode

With live-restore, the same upgrade from 29.10.0 to 29.10.1 restarted no container. The published port still stopped answering for 3 seconds while the service changed version.

The option has two limits. The documentation only supports live-restore across patch releases, such as 29.10.0 to 29.10.1; for a bigger jump, expect the restart. And it does not work with Swarm mode, which panels like Dokploy use: with it enabled, docker swarm init answers --live-restore daemon configuration is incompatible with swarm mode.

Common problems

  • permission denied while trying to connect to the docker API at unix:///var/run/docker.sock: the user is not in the docker group, or the session is older than the usermod. Log out and back in, then check with id -nG.
  • Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?: the service did not start. After a change to daemon.json, the reason shows up in sudo journalctl -u docker -n 30 --no-pager. A trailing comma in the file produces unable to configure the Docker daemon with file /etc/docker/daemon.json: invalid JSON: invalid character '}' looking for beginning of object key string. Fix it, validate with dockerd --validate and run sudo systemctl restart docker.
  • docker-compose: command not found: the hyphenated command is the old Compose, which is no longer installed. The current plugin is called with a space: docker compose.
  • no configuration file provided: not found: you are outside the project folder. Go to ~/apps/demo or pass the file with -f.
  • required variable POSTGRES_PASSWORD is missing a value: the .env does not exist or is in another folder.
  • Bind for 127.0.0.1:8080 failed: port is already allocated: another container already uses the port. Find it with docker ps --format '{{.Names}}\t{{.Ports}}' or change the left-hand port under ports.
  • failed to resolve reference "docker.io/library/postgres:18.99": ... not found: the tag does not exist. Check the image's tag list on Docker Hub.
  • The following packages have been kept back listing the Docker packages: that is the hold from step 4 doing its job.

What was not tested

The test ran on a virtual machine with no public IP. Ports were checked from another machine on the same local network, not over the internet. The SSH tunnel was opened with local port 18080 instead of 8080, and the Adminer login was done in a browser through that tunnel. Rootless mode, building images with Buildx, upgrades that are not patch releases and editing daemon.json with nano (the file was copied in ready-made) were left out. The test machine did not come with ufw, which was installed for the port test. The memory figures and the seconds come from that machine and are an order of magnitude.

Next steps


Originally published at https://nuvits.net/en/blog/install-docker-compose-ubuntu-24-04

Top comments (0)