Skip to content
Blog

Docker Compose on Servers: The Pragmatic Small-Scale Path

Run a production Compose stack on a single VPS — restart policies, healthchecks, .env config, a Caddy reverse proxy with automatic TLS, log rotation, a safe deploy flow, and backups — plus the honest limits that tell you when to move on.

Published on • November 15, 2026

AI Assistant

Most container-orchestration writing assumes you have a cluster. Most small products don’t: one VPS (or three), a handful of services, and a team of one to five people who would rather ship features than operate a control plane. For that workload, Docker Compose in production is not a toy — it is the correct default. Compose defines services, networks, and volumes in one YAML file and drives the whole lifecycle with a single command, and it works identically in development, CI, and on a production host (Docker Compose overview). This is a tutorial for doing it properly on a real server: the compose file, the surrounding operational plumbing, and an honest list of the limits that tell you when to leave.

The production compose file

Everything starts with a file that assumes failure: restart policies so the stack survives reboots and crashes, healthchecks so nothing is marked “up” before it answers HTTP, and resource limits so one runaway container cannot take the host down.

# docker-compose.yml
name: shop

services:
  app:
    image: registry.example.com/shop/app:1.4.2
    restart: unless-stopped
    environment:
      DATABASE_URL: ${DATABASE_URL}
      SESSION_SECRET: ${SESSION_SECRET}
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost:3000/healthz"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 512M
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: shop
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U shop"]
      interval: 10s
      timeout: 5s
      retries: 5

  caddy:
    image: caddy:2.10
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - app

volumes:
  pgdata:
  caddy_data:
  caddy_config:

Three details do most of the work:

  • restart: unless-stopped survives both container crashes and host reboots. on-failure with a retry cap is for batch jobs you do not want restarted forever.
  • condition: service_healthy in depends_on stops the classic boot race where the app starts before Postgres accepts connections. The healthcheck must actually test something (pg_isready, an HTTP /healthz), not just sleep.
  • Per-service logging options keep json-file logs from eating the disk — the default is unbounded growth, and an unbounded log is a production outage with extra steps.

Configuration: .env, not literals

Secrets and host-specific values belong in a .env file next to the compose file, referenced as ${VAR} in the YAML. Compose reads .env automatically for variable interpolation; the file never enters the image and never enters git.

# .env (chmod 600, one per host)
DATABASE_URL=postgres://shop:${POSTGRES_PASSWORD}@db:5432/shop
POSTGRES_PASSWORD=...
SESSION_SECRET=...

The rules that keep this from becoming an incident: commit .env.example with dummy values, keep the real .env at 0600 owned by the deploy user, and rotate by editing the file and recreating the service (docker compose up -d app). Note that .env values are visible to anyone with host access via docker inspect — for anything highly sensitive, mount Docker secrets or, on a single host, a root-owned file read by the app.

Volumes and mounts: where data lives

Two kinds of persistence, two different jobs:

  • Named volumes (pgdata:/var/lib/postgresql/data) live under Docker’s data root (/var/lib/docker/volumes/) and are the right home for database files, object storage, and anything the engine should manage. They are backed up by copying the volume directory.
  • Bind mounts (./Caddyfile:/etc/caddy/Caddyfile:ro) pin host files into containers — config files, certificates, scripts. Mark them :ro unless the container genuinely must write.

Avoid host-path volumes into application data directories (/home/app/data): they work until the container’s UID changes and the permissions silently break. If you need the data visible at a fixed path for a backup script, bind-mount the named volume’s directory instead.

Reverse proxy and TLS

Put one process in front of everything. Caddy is the lowest-friction option: it obtains and renews Let’s Encrypt certificates itself, given a domain pointing at the host.

# Caddyfile
example.com {
	encode zstd gzip
	reverse_proxy app:3000
}

api.example.com {
	reverse_proxy app:3000
}

Because Caddy joins the compose network, reverse_proxy app:3000 resolves by service name — no extra wiring. Caddy stores certificates in the caddy_data volume, so renewals survive redeploys. Traefik is the alternative if you want container-label-driven routing (traefik.http.routers.app.rule=Host(\example.com`)`), which pays off when services come and go frequently; for a fixed set of routes, a static Caddyfile is easier to audit. Either way, terminate TLS at the proxy and keep application containers listening on the internal network only.

Log rotation at the engine level

The per-service logging block above covers services you remember. Cover everything else in /etc/docker/daemon.json:

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

Then sudo systemctl restart docker (which restarts containers — do it in a maintenance window, or set it per-service and skip the global change on a busy host). Verify with docker inspect <container> --format '{{.HostConfig.LogConfig}}'.

The deploy flow

Keep deploys boring and identical every time. A deploy is: get the new compose file, pull new images, recreate changed services, verify.

cd /opt/shop
git pull --ff-only                      # compose file + Caddyfile change
docker compose pull                      # fetch new images
docker compose up -d --remove-orphans --wait   # recreate, wait for healthchecks
docker compose ps                        # confirm everything is healthy
docker compose logs --tail=100 app       # spot-check

--wait blocks until healthchecks pass (or times out), which turns “did it work?” into an exit code your CI can act on. Rollback is the same command with the previous image tag: edit .env or the compose file back, docker compose up -d. Since images are immutable and tags are immutable (make them so — never deploy :latest to a server), rollback is deterministic.

A minimal CI job is exactly this script, run over SSH, with a health gate after --wait.

Backups you have actually restored

A backup that has never been restored is a hypothesis. For a single host, schedule one script:

#!/usr/bin/env bash
# /usr/local/bin/shop-backup.sh — run from cron at 03:00
set -euo pipefail
stamp=$(date +%F)
docker compose exec -T db pg_dump -U shop | gzip > /var/backups/shop-db-$stamp.sql.gz
tar czf /var/backups/shop-volumes-$stamp.tgz \
  /var/lib/docker/volumes/shop_pgdata/_data
find /var/backups -name 'shop-*' -mtime +14 -delete

Ship /var/backups off-host (restic/rclone to object storage) and restore into a scratch host quarterly. Named volumes make this trivial: the data is in one directory, not scattered across the host.

The honest limits

Compose on one host fails in specific, predictable ways:

  • Single host = single point of failure. No replication, no live migration, no zero-downtime host OS upgrade. When the box dies, the site is down until you rebuild it elsewhere.
  • No rolling updates. docker compose up -d recreates containers; with one replica per service, every deploy has a seconds-long interruption. You can run --scale app=2 behind a proxy that reconfigures itself, but there is no built-in surge/unavailability budget, no maxUnavailable, no automatic rollback on failed healthcheck.
  • No autoscaling. Capacity is what the VPS has. Vertical resize means a reboot.
  • Everything is manual SSH. Drift, certificate incidents, and kernel updates are your on-call rotation, not a managed service’s.

Move on when any of these becomes true: you need a second host for availability (not just backups); you have more than ~10 services or multiple teams editing the same host; you need per-tenant isolation, autoscaling, or zero-downtime deploys as a hard requirement. The next step up is a managed container platform or a real orchestrator — the jump is a product decision, not a maturity badge. Running Compose well for two years and then migrating deliberately is a better engineering outcome than adopting a cluster you operate badly.

References