Skip to contentcloudvix
cloudvix
GuideServers & hosting

Install Docker on Ubuntu 24.04 and run your first container

Denys DunaievskyiFounder, Cloudvix
  • 13 min read
  • Published
  • LevelIntermediate
Guides
On this page

Key takeaways

  • The commands come from the Docker documentation for Ubuntu and install the packages from Docker’s repository, not Ubuntu’s older docker.io package.
  • By default Docker keeps every log line of a container without a limit, and a chatty program can fill the disk.
  • A container started with -p 8080:80 is open to the whole internet even if ufw denies everything.

Docker runs each program in its own container, so you can start, update and remove software without touching the rest of the server. This guide installs Docker Engine from Docker’s own repository, starts one container and ends with three settings worth changing right away.

Before you start

You need an Ubuntu 24.04 server and a user with sudo. If you have only a root login so far, do the first-hour setup first; it takes a few minutes and the rest of this guide assumes it. The commands come from the Docker documentation for Ubuntu and install the packages from Docker’s repository, not Ubuntu’s older docker.io package.

The steps

Add Docker’s repository

This downloads Docker’s signing key and registers the repository for your Ubuntu release. The last line reloads the package list.

On the serverbash
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
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt update

Install Docker Engine and Compose

One package list gives you the engine, the command line, the build tool and the Compose plugin. Compose is then called as docker compose, with a space.

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

Check that it works

The service must be active, and the hello-world image must print a short welcome text and exit. --rm removes the finished container.

On the serverbash
sudo systemctl is-active docker
sudo docker run --rm hello-world

Run a first container

This starts a web server in the background. The port is published on 127.0.0.1 only, so it works from the server itself and nobody on the internet can reach it. That detail matters, see the last step. --restart unless-stopped brings the container back after a reboot.

On the serverbash
sudo docker run -d --name web --restart unless-stopped -p 127.0.0.1:8080:80 nginx:stable
sudo docker ps
curl -I http://127.0.0.1:8080
sudo docker logs web

Stop and remove it

A removed container is gone, but its image stays on the disk for the next start.

On the serverbash
sudo docker stop web
sudo docker rm web

The same thing with Compose

Compose keeps the settings in a file you can commit and read again in a year. Save this as compose.yaml in an empty folder, then run the second block from that folder.

On the server · compose.yamlyaml
services:
  web:
    image: nginx:stable
    ports:
      - "127.0.0.1:8080:80"
    restart: unless-stopped
On the serverbash
docker compose version
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs web
sudo docker compose down

Optional: run docker without sudo

Members of the group docker can control Docker without sudo.

On the serverbash
sudo usermod -aG docker $USER
# log out and back in, then:
docker ps
Warning

That is convenient and also equal to root access: anyone in the group can start a container that mounts the whole disk. Add only yourself, and never a user that an automated tool or an assistant logs in as.

Limit the size of container logs

By default Docker keeps every log line of a container without a limit, and a chatty program can fill the disk. This sets a rotation for containers created afterwards; recreate existing containers to apply it to them.

On the server · /etc/docker/daemon.jsonjson
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

Start at boot

On Ubuntu the package enables the service for you; the first command only confirms it. The second is harmless if it is already on.

On the serverbash
systemctl is-enabled docker
sudo systemctl enable docker

Know which ports are open

Docker adds its own firewall rules for published ports and these run before the rules that ufw manages. Publish on 127.0.0.1 unless the port is meant to be public, put a reverse proxy in front for web traffic, and list what is really listening with the command below.

On the serverbash
sudo ss -ltnp
Warning

A container started with -p 8080:80 is open to the whole internet even if ufw denies everything.

If something goes wrong

apt cannot find docker-ce

The repository file is missing or the key download failed. Run the repository step again and read the first error of apt update.

permission denied while connecting to the Docker socket

You ran docker without sudo and you are not in the group docker. Use sudo, or log out and in again after adding yourself.

port is already allocated

Another program or container already uses that port. Pick a different host port, for example 127.0.0.1:8081.

The container exits at once

Read its log with sudo docker logs web before you change anything.

Where to go next

With Docker in place, you can run a ready-made application such as n8n, or let an assistant deploy your own project over a restricted SSH key. Both guides are linked at the end of this page.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server