In this tutorial
- Docker app image or a manual install: which one?
- What are the steps to set up Docker Engine for Debian or Ubuntu?
- How do you confirm the engine works?
- How do you stop container logs from filling the disk?
- How do you write a compose stack?
- Why publish ports only on 127.0.0.1?
- How do you update and clean up?
- How do you back up container data?
Docker app image or a manual install: which one?
Use the Docker app image when you want containers from the first login with no setup. It is Ubuntu 24.04 with Docker Engine installed, so docker version works straight away and the rest of this guide from the log rotation step onward still applies.
Install by hand when you started from another image, such as Debian 12 or Debian 13, or when you want to choose the moment Docker arrives. Avoid the docker.io package from the distribution for production work; it often lags behind and lacks the Compose plugin.
Size the plan for the containers, not for Docker itself. The engine uses little memory; your databases and app images use the rest. Two gigabytes is a practical floor for a stack with a database.
What are the steps to set up Docker Engine for Debian or Ubuntu?
Add Docker's signing key and repository, then install the engine with the Compose and Buildx plugins. On Ubuntu:
sudo apt-get update
sudo apt-get 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
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-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginDebian users swap ubuntu for debian in the two repository URLs. On AlmaLinux and Rocky add the CentOS repository with sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo and install the same package names with dnf.
How do you confirm the engine works?
Run the test container and check versions.
sudo systemctl enable --now docker
sudo docker run --rm hello-world
docker compose version
sudo docker info | grep -E 'Server Version|Storage Driver|Cgroup Version'The storage driver should be overlay2 and the cgroup version 2 on current images. To run docker without sudo, add your user to the group: sudo usermod -aG docker deploy, then log out and in. Be aware this group is effectively root on the host, so only add accounts you would trust with root.
How do you stop container logs from filling the disk?
Set a size cap for the default json-file driver before you start long-running containers. Without it, a chatty service can write gigabytes into /var/lib/docker/containers.
sudo tee /etc/docker/daemon.json >/dev/null <<'EOF'
{
"log-driver": "json-file",
"log-opts": { "max-size": "20m", "max-file": "3" },
"live-restore": true
}
EOF
sudo systemctl restart dockerThe cap applies to containers created after the change, so recreate existing ones with docker compose up -d --force-recreate. live-restore keeps containers running while the daemon restarts during upgrades.
How do you write a compose stack?
Keep each stack in its own folder under /srv with one compose.yaml. A small app with PostgreSQL:
sudo mkdir -p /srv/app && cd /srv/app
sudo tee compose.yaml >/dev/null <<'EOF'
services:
db:
image: postgres:16
restart: unless-stopped
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/dbpass
secrets: [dbpass]
volumes: [dbdata:/var/lib/postgresql/data]
web:
image: ghcr.io/example/app:1.4.2
restart: unless-stopped
depends_on: [db]
ports: ["127.0.0.1:3000:3000"]
secrets:
dbpass:
file: ./dbpass.txt
volumes:
dbdata:
EOF
openssl rand -hex 24 | sudo tee dbpass.txt >/dev/null && sudo chmod 600 dbpass.txt
sudo docker compose up -d
sudo docker compose psThe database publishes no port at all; the web service reaches it by the name db on the stack network. Pin image tags to versions instead of latest so an update happens when you choose.
Why publish ports only on 127.0.0.1?
Because Docker inserts its own packet filter rules, and a port published as 3000:3000 is reachable from the internet even when your nftables input chain drops it. Publishing on 127.0.0.1:3000:3000 keeps it local, and a reverse proxy on the host serves it over HTTPS:
app.example.net {
reverse_proxy 127.0.0.1:3000
}That is a Caddyfile block from the Caddy tutorial. Check from another machine that only 80 and 443 answer: nmap -Pn -p 1-10000 198.51.100.24. If a container must be public, such as a game server on UDP, publish just that port on purpose.
How do you update and clean up?
Pull new images, recreate, then prune what is no longer used.
cd /srv/app
sudo docker compose pull
sudo docker compose up -d
sudo docker image prune -f
sudo docker system dfdocker system df shows how much space images, containers and volumes take. Run sudo docker builder prune -f on hosts where you build images, because build cache grows quietly. Never run docker volume prune without reading the list first; named volumes hold your data.
How do you back up container data?
Dump databases from inside their containers and copy named volumes to your backup target.
sudo docker compose exec -T db pg_dumpall -U postgres | gzip > /var/backups/db/app-$(date +%F).sql.gz
sudo docker run --rm -v app_dbdata:/data:ro -v /var/backups:/out alpine \
tar czf /out/app_dbdata.tgz -C /data .The SQL dump is the copy to restore from; the volume tarball is a fallback. Add /srv and /var/backups to the restic job in the backups tutorial, and see what is running at a glance with sudo docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'.
Questions about this tutorial
Is the Docker image different from installing Docker myself?
The Docker image is Ubuntu 24.04 with Docker Engine already installed. The result is the same as following this guide on a plain Ubuntu 24.04 instance.
Can I run Docker on Alpine or FreeBSD?
Alpine runs Docker from its community repository with apk add docker docker-cli-compose. FreeBSD does not run Linux containers; use jails there.
Is Kubernetes available?
Not as a managed service. You can run a lightweight distribution yourself on your instances, but plan memory carefully on small plans.
Why does my firewall not block a published port?
Docker routes published ports through its own rules before your input chain sees them. Publish on 127.0.0.1 and use a reverse proxy instead.
How much disk do Docker images use?
Often more than expected. Check with docker system df and prune unused images after updates.