Latest

Installing Docker Engine and Docker Compose on Ubuntu Server from the official repository

Second guide in the Docker series: remove unofficial packages, add Docker's repository, install Docker Engine, Buildx and the Compose plugin, test with hello-world, decide whether to use the docker group, limit logs and run a first test web container.

By 5 min read
Installing Docker Engine and Compose on Ubuntu

With the Ubuntu server ready (if you have not prepared it yet, start with Ubuntu Server 26.04 LTS for Docker), we install Docker Engine, which downloads images and runs containers, and the Docker Compose plugin, which lets you describe a service in a file and start it with one command. We use Docker’s official repository, as its documentation recommends: Ubuntu’s own packages (docker.io) and the snap exist, but they lag behind and can cause conflicts.

In short: a few commands for the repository, one to install, one to test. At the end you will have Docker Engine 29 with Compose v5, a test container reachable from the browser and the basics to read what it does.

Step 1: remove any unofficial packages

If Docker was never installed on the server you can skip this, but the command is harmless: it only removes conflicting packages if they exist.

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

If apt reports that none of these packages are installed, that is fine. Images and volumes already in /var/lib/docker are not deleted.

Step 2: add Docker’s repository

# Docker's official GPG key
sudo apt update
sudo apt install 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

# repository file, in .sources format
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
Line Meaning
docker.asc The key Docker signs its packages with: apt rejects packages not signed with it.
Suites The Ubuntu codename (resolute for 26.04), read from /etc/os-release.
Architectures amd64 or arm64, detected from the system.
Signed-By Ties the repository to that one key instead of trusting every key on the system.

The final apt update should show a download.docker.com line with no errors. If you get a signature error, download the key again.

Step 3: install Docker

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Package Purpose
docker-ce The Docker service (the daemon), which runs with root privileges
docker-ce-cli The docker command
containerd.io The component that actually runs containers
docker-buildx-plugin Image builds (docker build)
docker-compose-plugin The docker compose command (with a space, not the old docker-compose)

On Ubuntu the service starts automatically and on every boot. Check:

sudo systemctl status docker --no-pager
sudo docker run hello-world
docker compose version
sudo docker run hello-world downloads the test image and prints Hello from Docker
hello-world downloads a tiny image, starts a container that prints a message and exits. Reconstructed screenshot, indicative versions.

hello-world checks in one go that the service works, that the server reaches Docker Hub and that containers start.

Step 4: Docker without sudo? Only if you know what it means

After installation, docker commands need sudo. To avoid it you add your user to the docker group:

sudo usermod -aG docker $USER
# log out and back into the SSH session, then:
docker ps

Docker’s documentation is clear: the docker group grants root-level privileges. Anyone who can start containers can mount any folder on the system. On a personal server where you are the only user it is an acceptable convenience; on a shared server, stick with sudo. There is also rootless mode, which runs Docker without root but has limitations: we do not use it for a first lab.

Step 5: stop logs from filling the disk

By default Docker stores container output in JSON files that are not rotated: a very chatty container can fill the disk in a few months. Docker’s documentation recommends the local driver, which compresses and rotates logs automatically.

sudo nano /etc/docker/daemon.json
{
  "log-driver": "local"
}
sudo systemctl restart docker
docker info --format '{{.LoggingDriver}}'

The new driver applies to containers created from now on, which is why it is worth setting before you create services. Restarting Docker stops running containers for a few seconds.

Step 6: a first real container, a test web site

A small end-to-end simulation: we download the nginx web server image, run it, view it in the browser and remove it.

docker run -d --name test -p 8080:80 nginx:alpine
docker ps
docker run starts nginx:alpine on port 8080, docker ps shows it running, then docker stop and docker rm remove it
The test container publishes nginx’s port 80 on the server’s port 8080. Reconstructed screenshot, example IDs.
Part of the command Meaning
-d Runs the container in the background
--name test A readable name instead of a random one
-p 8080:80 Server port : port inside the container
nginx:alpine The nginx image, alpine variant (smaller)

Now open http://192.168.1.50:8080 from your PC (with your server’s address): the Welcome to nginx! page appears. Look at the requests and then remove everything:

docker logs test
docker stop test && docker rm test
docker image ls

The nginx:alpine image stays on disk until you delete it (docker image rm nginx:alpine): next time the container starts without downloading it again.

Commands to remember

Command What it does
docker ps / docker ps -a Running containers / all, including stopped ones
docker logs -f NAME Follows the container output (Ctrl+C to exit)
docker exec -it NAME sh Opens a shell inside the container
docker stop / docker start / docker restart Stop, start, restart
docker rm NAME Deletes the container (not the image)
docker image ls / docker pull Lists / downloads images

In the next guide we move from docker run to the compose.yaml file and explain images, volumes, networks and ports: it is the format we will then use in Portainer.

Sources