NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Docker Compose Guide: Run Multi-Container Apps with Databases, Volumes and a Reverse Proxy

Most real applications need more than one container: the app itself, a database, a cache, maybe a worker and a reverse proxy. Docker Compose describes all of them in one YAML file and starts, stops and updates them together with a single command. It is the easiest way to run self-hosted software and small production services on a VPS or dedicated server.

This guide assumes Docker and the Compose plugin are installed — see installing Docker.

The basics

A project lives in its own directory with a compose.yaml (older name: docker-compose.yml):

/srv/myapp/
├── compose.yaml
├── .env
└── data/

Commands are run from that directory:

docker compose up -d          # create and start everything in the background
docker compose ps             # status
docker compose logs -f app    # follow logs of one service
docker compose down           # stop and remove containers (volumes are kept)
docker compose pull           # download newer images
docker compose restart app
docker compose exec db psql -U app   # run a command in a container

Use docker compose (with a space, the plugin). The old standalone docker-compose (with a hyphen) is deprecated.

A complete example

An application with PostgreSQL, Redis and the Caddy web server providing automatic HTTPS:

# /srv/myapp/compose.yaml
services:
  app:
    image: ghcr.io/example/myapp:2.4.1
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    volumes:
      - ./data/uploads:/app/uploads
    networks: [internal, web]

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

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis-data:/data
    networks: [internal]

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    networks: [web]

volumes:
  db-data:
  redis-data:
  caddy-data:
  caddy-config:

networks:
  internal:
    internal: true
  web:

Caddyfile:

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

.env:

DB_PASSWORD=generate-a-long-random-value
chmod 600 .env
docker compose up -d

Caddy obtains a Let’s Encrypt certificate for app.example.com automatically once DNS points to the server.

What each part does

Services and images

Each service is one container (or several with deploy.replicas). Pin image versions (postgres:16, myapp:2.4.1) so docker compose pull does not unexpectedly jump to a new major version.

restart policies

Policy Behaviour
no Never restart (default)
on-failure Restart only after a crash
unless-stopped Always restart, including after reboot, unless you stopped it
always Always restart, even if you stopped it manually (on daemon restart)

unless-stopped is right for most long-running services.

Volumes vs bind mounts

  • Named volumes (db-data:) are managed by Docker under /var/lib/docker/volumes/. Best for database files.
  • Bind mounts (./data/uploads:/app/uploads) map a host directory. Convenient for configuration and files you want to see and back up directly.

Networks

Containers on the same network reach each other by service name (db, redis). In the example, internal: true means the internal network has no route to the outside — the database and Redis cannot be reached from the internet at all, and only app connects both networks. Only Caddy publishes ports.

depends_on and health checks

depends_on alone only controls start order. With condition: service_healthy, the app waits until PostgreSQL actually accepts connections — avoiding crashes on first start.

Environment variables and secrets

  • .env in the project directory is read automatically for ${VAR} substitution in the YAML.
  • env_file: passes variables into the container.
  • Keep .env out of Git (.gitignore) and readable only by you (chmod 600).

For more sensitive setups, Compose secrets mount values as files under /run/secrets/ instead of environment variables, which do not show up in docker inspect:

services:
  db:
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets: [db_password]
secrets:
  db_password:
    file: ./secrets/db_password.txt

Publishing ports safely

Docker’s published ports bypass UFW and firewalld. Publish only what must be public (80/443 on the reverse proxy). For admin interfaces, bind to localhost and use an SSH tunnel:

ports:
  - "127.0.0.1:8080:8080"
ssh -L 8080:127.0.0.1:8080 deploy@your-server

See Docker and firewalls.

Resource limits

Stop one container from consuming the whole server:

services:
  app:
    deploy:
      resources:
        limits:
          cpus: "2"
          memory: 1g

Updating an application

cd /srv/myapp
# change the image tag in compose.yaml if you pin versions
docker compose pull
docker compose up -d          # recreates only containers whose image changed
docker image prune -f

Read the release notes before major upgrades — especially for databases. A PostgreSQL major version upgrade (16 → 17) needs a dump and restore or pg_upgrade; just changing the tag will fail.

Backups

Back up the data, not the containers:

# Database dump from the running container
docker compose exec -T db pg_dump -U app app | gzip > /var/backups/myapp-$(date +%F).sql.gz

# Files: bind-mounted directories and the project directory (compose.yaml, .env, Caddyfile)

For named volumes, either dump through the application (preferred for databases) or archive the volume with a temporary container:

docker run --rm -v myapp_redis-data:/data -v /var/backups:/backup alpine \
  tar czf /backup/redis-$(date +%F).tar.gz -C /data .

Volume names are prefixed with the project name (myapp_). Automate it and copy off-site — see restic backups.

Useful patterns

  • Overrides: compose.override.yaml is merged automatically — handy for local development settings that differ from production.
  • Profiles: mark optional services (profiles: [debug]) and start them only with docker compose --profile debug up.
  • Logs: limit log size globally in /etc/docker/daemon.json or per service with logging: options.

Troubleshooting

Problem Check
App cannot connect to db Same network? Using service name and container port (5432), not localhost?
Changes to compose.yaml ignored Run docker compose up -d again
Data gone after down Data was inside the container, not in a volume
“port is already allocated” Another container or host service uses it: ss -tlnp
Container keeps restarting docker compose logs SERVICE