Docker on a VPS is the quickest way to run self-hosted apps: one compose.yaml per app, one command to start it,
and nothing installed directly on the host. It also has two traps that catch almost everyone on a fresh server:
published ports skip your ufw firewall, and container logs grow until the disk is full.

This guide installs Docker the way the Docker documentation recommends on Ubuntu 24.04 and Debian 12, fixes both
traps, and runs a small Compose stack behind a reverse proxy.

0. Harden the server first

Do the first-hour basics before you install anything: updates, SSH keys, a firewall, fail2ban. The open-source
vps-first-steps script does that without locking you out:

curl -fsSLO https://raw.githubusercontent.com/phantomvps/vps-first-steps/main/vps-first-steps.sh less vps-first-steps.sh sudo bash vps-first-steps.sh --user deploy --allow 80,443 

1. Install Docker Engine from Docker's repository

The distribution packages (docker.io) work but lag behind, and they do not include the Compose v2 plugin in the
same way. Remove any old packages, then add Docker's repository.

for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove -y "$pkg" 2>/dev/null done sudo apt-get update sudo apt-get install -y ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings . /etc/os-release sudo curl -fsSL "https://download.docker.com/linux/$ID/gpg" -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/$ID $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-plugin 

$ID is ubuntu or debian and $VERSION_CODENAME is noble or bookworm, read from /etc/os-release, so the
same block works on both. Check it:

sudo docker run --rm hello-world docker compose version 

Note the command is docker compose (with a space): Compose v2 is a Docker CLI plugin. The old Python
docker-compose is end-of-life.

Should you add your user to the docker group?

sudo usermod -aG docker deploy lets deploy run Docker without sudo. Be aware that membership in the docker
group is effectively root on the host: anyone in it can mount / into a container. On a single-admin server that is
usually acceptable; just do not hand that group to an app user. Log out and in again for it to take effect.

2. Rotate container logs

By default Docker uses the json-file log driver with no size limit. A chatty container can fill the disk in
days. Set a global limit in /etc/docker/daemon.json:

{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } 
sudo systemctl restart docker 

This applies to containers created after the change. Recreate existing ones (docker compose up -d --force-recreate)
to pick it up.

3. The ufw trap: published ports bypass your firewall

This surprises almost everyone. When you publish a port with -p 8080:80 or ports: - "8080:80", Docker inserts its
own iptables rules that accept traffic for that port before ufw's rules are evaluated. ufw status still shows
the port as closed, but the service is reachable from the whole internet.

Prove it to yourself once:

sudo docker run -d --name trap -p 8080:80 nginx:alpine # from your laptop: curl -I http://YOUR_SERVER_IP:8080 # answers, even though ufw never allowed 8080 sudo docker rm -f trap 

Two good ways to handle it:

A. Bind published ports to localhost and put a reverse proxy in front. This is the simplest pattern:

ports: - "127.0.0.1:8080:80" 

Now only processes on the server (your reverse proxy) can reach it. Only the proxy's ports 80 and 443 face the
internet, and you allow those in ufw.

B. Do not publish at all. Put the app and the proxy on the same Docker network and let the proxy reach the app by
service name. No host port is opened.

For advanced cases Docker provides the DOCKER-USER iptables chain, which is evaluated before Docker's own rules, so
filters you add there apply to published ports. It is powerful but easy to get wrong; prefer A or B.

4. A first Compose stack: an app behind Caddy

Create a project directory:

sudo mkdir -p /opt/stacks/whoami && cd /opt/stacks/whoami 

compose.yaml:

services: app: image: traefik/whoami:latest restart: unless-stopped networks: [web] caddy: image: caddy:2 restart: unless-stopped ports: - "80:80" - "443:443" - "443:443/udp" volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - caddy_data:/data - caddy_config:/config networks: [web] networks: web: volumes: caddy_data: caddy_config: 

Caddyfile (point an A record for the domain at your server first):

app.example.com { reverse_proxy app:80 } 

Start it and watch the logs:

sudo docker compose up -d sudo docker compose logs -f caddy 

Caddy obtains a certificate automatically, and https://app.example.com shows the whoami page. The app service
publishes no port at all; only Caddy faces the internet, and you opened 80 and 443 in ufw earlier.

5. Everyday commands

docker compose ps # status of this stack docker compose logs -f app # follow one service's logs docker compose pull && docker compose up -d # update images and recreate changed containers docker compose down # stop and remove containers (named volumes stay) docker system df # disk used by images, containers, volumes docker image prune -a # remove unused images (asks first) 

restart: unless-stopped together with the Docker service starting on boot means your stack comes back after a
reboot without any extra systemd units.

6. Data and backups

  • Keep each stack in its own directory under /opt/stacks/ with its compose.yaml and config files in Git.
  • Named volumes live under /var/lib/docker/volumes/. Back them up, but for databases dump first (docker compose exec db pg_dumpall -U postgres > dump.sql) rather than copying live database files.
  • Pin image tags you depend on (postgres:16, not postgres:latest) so a pull does not surprise you with a major upgrade.

Checklist

  • [ ] Server hardened before Docker
  • [ ] Docker from the official repository, docker compose version works
  • [ ] Log rotation in daemon.json
  • [ ] No ports published to 0.0.0.0 except the reverse proxy
  • [ ] Stacks in /opt/stacks/<name>, config in Git, volumes backed up

Where to run it

I work with PhantomVPS: VPS and dedicated servers with root access and KVM virtualization, so Docker runs without
restrictions, paid in Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX with no ID documents. See the
crypto VPS page for plans and payment options.