Docker Compose is still the cleanest way to run a two- or three-container application on a single Linux host. You get reproducible builds, isolated networks, and a single YAML file you can keep in Git. Over time, though, I noticed the same small mistakes showing up in my own Compose files: databases exposed on all interfaces, apps crashing on boot because Postgres wasn't ready yet, and logs quietly filling the disk.
Here is the template I now use as a starting point for small self-hosted services.
A clean baseline compose.yaml
Notice that modern Docker Compose (v2, invoked as docker compose) does not need a top-level version: key. Keeping one directory per stack under /opt/stacks/<app>/ makes backups and upgrades predictable.
services:
app:
image: ghcr.io/example/my-app:1.4.2
restart: unless-stopped
env_file:
- .env
ports:
- "127.0.0.1:8080:8080"
depends_on:
db:
condition: service_healthy
networks:
- internal
volumes:
- ./config.toml:/app/config.toml:ro
- app_data:/app/data
mem_limit: 512m
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- pg_data:/var/lib/postgresql/data
networks:
- internal
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
networks:
internal:
driver: bridge
volumes:
app_data:
pg_data:
1. Bind ports to 127.0.0.1
If a reverse proxy like Nginx or Caddy runs on the same host, bind the application port to 127.0.0.1:8080:8080 instead of 8080:8080. Without 127.0.0.1, Docker publishes the port on 0.0.0.0 and punches a hole straight through UFW. Databases and caches should not have a ports: block at all—containers on the internal network reach them by service name (db:5432), and nothing outside the host can touch them.
2. Use healthchecks with depends_on
Writing depends_on: [db] only waits for the database container to start, not for Postgres to finish initializing and accept TCP connections. Adding a healthcheck and condition: service_healthy stops your app from entering a crash loop on the first boot or after a host reboot.
3. Pin image tags and separate .env
Avoid :latest in production Compose files. When you run docker compose pull six months later, :latest might pull a major database or app upgrade with breaking schema changes. Pin to a specific minor or patch tag like postgres:16-alpine or 1.4.2 so upgrades are deliberate.
Keep secrets in a .env file next to compose.yaml with chmod 600 .env and never commit it to Git. Commit a .env.example with placeholder keys instead so you remember which variables are required when recreating the stack.
4. Cap container logs and memory
Docker's default json-file logging driver has no size limit. A noisy container stuck in a retry loop can fill your root filesystem overnight. Setting max-size: "10m" and max-file: "3" caps each container at 30 MB of logs. You can also set this globally in /etc/docker/daemon.json so you don't have to repeat it in every file.
Adding a basic mem_limit on memory-hungry runtimes also prevents one misbehaving side project from triggering the kernel OOM killer against your database on a 2 GB VPS.
5. Mount config files read-only
Append :ro to bind-mounted configuration files (./config.toml:/app/config.toml:ro). It documents intent and prevents a misbehaving process from modifying its own config on disk.
6. Keep upgrades boring
When updating a service, inspect the release notes, back up the database volume first, and run:
docker compose pull
docker compose up -d --remove-orphans
docker compose ps
docker compose logs --tail=50 app
Using --remove-orphans cleans up old containers if you renamed or removed a service in compose.yaml. Periodically run docker image prune to clear old untagged layers, and avoid docker system prune -a --volumes unless you are completely sure no stopped container owns data you still care about.