Most developers use Docker Compose to run a database alongside their app. That's fine, but it's barely scratching the surface. Docker Compose has patterns that dramatically reduce debugging time, eliminate configuration drift between environments, and keep secrets out of your codebase.

These five patterns cover the things that trip up developers every week in production: services starting before their dependencies are ready, credentials leaking into version control, configurations that drift between dev and prod, and workers that can't scale when load spikes.

Each pattern includes a complete, working docker-compose.yml snippet you can drop into your own project.

1. Health Checks and Dependency Ordering (The Right Way to Do depends_on)

The most common docker-compose.yml mistake: using depends_on and assuming your service waits for the dependency to be ready.

It doesn't. depends_on only waits for the container to start, not for the service inside it to be accepting connections. Your app will crash trying to connect to a PostgreSQL instance that's still initializing.

The fix is combining depends_on with condition: service_healthy and a proper healthcheck:

version: "3.9"

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d appdb"]
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 10s

  redis:
    image: redis:7
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5

  app:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://app:secret@db:5432/appdb
      REDIS_URL: redis://redis:6379

The start_period field is important — it gives the service time to initialize before health check failures count against the retries limit. Without it, PostgreSQL's slow first-boot triggers false failures and your stack never comes up.

Key takeaway: Always pair depends_on with condition: service_healthy and define a healthcheck for every stateful service.

2. Development vs Production Overrides (docker-compose.override.yml Pattern)

Running the same docker-compose.yml in dev and prod leads to one of two bad outcomes: either you bloat production with dev tools, or you strip dev of conveniences like hot reload and debug ports.

Docker Compose has a built-in solution: docker-compose.override.yml. When you run docker compose up, Compose automatically merges docker-compose.yml with docker-compose.override.yml if it exists.

docker-compose.yml — base config, shared between all environments

version: "3.9"

services:
  app:
    image: myapp:${APP_VERSION:-latest}
    ports:
      - "8000:8000"
    environment:
      DJANGO_SETTINGS_MODULE: myapp.settings

  db:
    image: postgres:16
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

docker-compose.override.yml — dev only, auto-merged

version: "3.9"

services:
  app:
    build: .           # Build from source in dev, use image in prod
    volumes:
      - .:/app         # Hot reload: mount source code
    environment:
      DEBUG: "true"
      DJANGO_SETTINGS_MODULE: myapp.settings.dev
    command: python manage.py runserver 0.0.0.0:8000

  db:
    ports:
      - "5432:5432"    # Expose DB port locally for GUI tools

docker-compose.prod.yml — explicit production overrides

version: "3.9"

services:
  app:
    restart: always
    environment:
      DJANGO_SETTINGS_MODULE: myapp.settings.prod
    deploy:
      resources:
        limits:
          memory: 512M

In production, specify files explicitly — no auto-merge:

# Development (auto-merges override.yml)
docker compose up

# Production (explicit files, no auto-merge)
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

This pattern keeps a single source of truth for shared config while letting each environment add its own layer without polluting the others.

3. Multi-Service Debugging (Debug One Service While Others Run)

When debugging, you want to run one service interactively — with a debugger attached, log output streaming, or a shell open — while the rest of the stack runs normally in the background.

The naive approach is docker compose up on everything and then docker compose logs -f app. That works for logs, but you can't attach a debugger or override the entrypoint.

version: "3.9"

services:
  app:
    build: .
    ports:
      - "8000:8000"
      - "5678:5678"    # debugpy port
    environment:
      PYTHONDONTWRITEBYTECODE: "1"
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app"]
      interval: 5s
      retries: 5
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb

  worker:
    build: .
    command: celery -A myapp worker -l info
    depends_on:
      - app

The debug workflow:

# Start everything except the app service
docker compose up -d db worker

# Run the app interactively with debugpy
docker compose run --rm --service-ports app \
  python -m debugpy --listen 0.0.0.0:5678 --wait-for-client \
  manage.py runserver 0.0.0.0:8000

The --service-ports flag publishes the ports defined in the compose file, including port 5678. Your VS Code or PyCharm debugger connects to localhost:5678 and execution pauses at your breakpoints.

For a quick shell into a running container without stopping it:

docker compose exec app /bin/sh
Key takeaway: Use docker compose run for interactive or debug sessions; use docker compose exec to shell into already-running containers.

4. Secrets Management (Env Files, Docker Secrets, Avoiding Hardcoded Credentials)

Hardcoded credentials in docker-compose.yml end up in version control. It happens to everyone, and it's a serious security issue. There are three progressively stronger approaches.

Level 1: .env files (minimum viable security)

version: "3.9"

services:
  db:
    image: postgres:16
    env_file:
      - .env.db        # Never commit this file
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_NAME}

  app:
    build: .
    env_file:
      - .env.app

Your .env.db file (gitignored) looks like:

DB_USER=app
DB_PASSWORD=s3cur3p@ssw0rd
DB_NAME=appdb

Always add .env* to .gitignore. Commit .env.example files with placeholder values so other developers know what's needed.

Level 2: Docker Secrets (for Swarm or production)

version: "3.9"

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password

  app:
    build: .
    secrets:
      - db_password
      - app_secret_key

secrets:
  db_password:
    file: ./secrets/db_password.txt   # Or use external: true for Swarm
  app_secret_key:
    file: ./secrets/app_secret_key.txt

Docker Secrets mount credentials as files at /run/secrets/<name> rather than environment variables — they never appear in docker inspect output.

Level 3: External secret stores

For the most sensitive environments, don't put secrets in Compose at all. Use HashiCorp Vault, AWS Secrets Manager, or 1Password CLI and have your application fetch secrets at startup. Your docker-compose.yml only contains the vault address, never the secrets themselves.

Critical rule: Never commit a file containing real credentials. Use CI/CD secret injection — GitHub Actions Secrets, GitLab CI variables — instead of committing .env files.

5. Scaling Workers (docker compose scale, When to Use It)

Docker Compose can run multiple instances of a service — useful for scaling background workers to chew through a queue faster without reaching for Kubernetes.

version: "3.9"

services:
  redis:
    image: redis:7

  web:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - redis

  worker:
    build: .
    command: celery -A myapp worker -l info --concurrency=2
    depends_on:
      - redis
    # No ports: workers don't need exposed ports
    deploy:
      replicas: 2      # Default replica count

Scale workers up at runtime without restarting the whole stack:

# Scale to 4 worker instances
docker compose up -d --scale worker=4

# Scale back down
docker compose up -d --scale worker=1

# Check your scaled instances
docker compose ps

# Tail logs from all worker instances simultaneously
docker compose logs -f worker

Important constraints to know before scaling:

When scaling with Compose makes sense: local load testing, single-server deployments with CPU-bound background tasks, and draining a backlogged queue faster during an incident.


Putting It Together

These five patterns solve the most common pain points in real Docker Compose workflows:

  1. Health checks — services start in the right order and only when truly ready
  2. Override files — dev and prod environments diverge cleanly without code duplication
  3. Debug workflow — attach a debugger to one service while the rest run normally
  4. Secrets management — credentials stay out of version control at every tier
  5. Worker scaling — handle load spikes on a single host without Kubernetes

Master these and you'll spend a lot less time fighting your local environment and a lot more time shipping.


Which of these patterns have saved you the most debugging time? I'd love to hear what your Docker Compose setup looks like in the comments.