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.
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 updateInstall 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.
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginCheck 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.
sudo systemctl is-active docker
sudo docker run --rm hello-worldRun 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.
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 webStop and remove it
A removed container is gone, but its image stays on the disk for the next start.
sudo docker stop web
sudo docker rm webThe 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.
services:
web:
image: nginx:stable
ports:
- "127.0.0.1:8080:80"
restart: unless-stoppeddocker compose version
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs web
sudo docker compose downOptional: run docker without sudo
Members of the group docker can control Docker without sudo.
sudo usermod -aG docker $USER
# log out and back in, then:
docker psThat 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.
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 dockerStart 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.
systemctl is-enabled docker
sudo systemctl enable dockerKnow 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.
sudo ss -ltnpA container started with -p 8080:80 is open to the whole internet even if ufw denies everything.
Let an assistant do it, with a plan first
If you use an AI assistant that can run SSH commands, paste the prompt below. It works with SSH access as a user with sudo. If the assistant should also order the server, use a Cloudvix API key, which has limits (up to €500 a month and 25 servers), instead of your account login.
The prompt makes the assistant show a plan before it acts, check each step and stop when a check fails. It is told to keep your user out of the docker group and to publish ports on 127.0.0.1 only.
You are helping me install Docker Engine on an Ubuntu 24.04 server at SERVER_IP and run a first container. Connect over SSH as the user ops, which has sudo.
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
Plan to propose: add Docker's official apt repository for Ubuntu following docs.docker.com, install docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin and docker-compose-plugin; confirm with systemctl is-active docker and docker run --rm hello-world; start an nginx:stable container named web that publishes its port only on 127.0.0.1:8080 and check it with curl; stop and remove it; limit container log size with a daemon.json (max-size 10m, max-file 3).
Do not add ops to the docker group unless I ask: that group is equivalent to root. Do not publish any container port on 0.0.0.0, because Docker bypasses the ufw firewall. At the end, list what you changed and how to undo each change.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.
Runs Cloudvix and is responsible for the guides published here.