Docker Compose Dev, Debugging, CI, and Production Patterns

Developer workflows, troubleshooting commands, CI flows, and small-host production Compose patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Developer Workflows
Watch files and sync/rebuild
docker compose watch

# Watches project files and applies sync or rebuild actions when configured.

Run test suite in service
docker compose run --rm app pytest -q

# Runs tests in a disposable service container.

Install dependencies in app service
docker compose run --rm app npm install

# Executes dependency installation in the service environment.

Scale a service
docker compose up -d --scale worker=3

# Starts multiple instances of a service.

Run database migration
docker compose run --rm web python manage.py migrate

# Runs a migration task in an ephemeral application container.

## Debugging and Troubleshooting
Validate effective config
docker compose config --quiet

# Checks whether the Compose config is valid without printing it.

List all containers including stopped
docker compose ps -a

# Shows current and exited containers for the project.

View recent logs since time
docker compose logs --since=10m api

# Shows recent logs for a specific service.

Inspect runtime env
docker compose exec api env | sort

# Prints environment variables inside the running service.

Start ephemeral debug shell
docker compose run --rm --entrypoint sh api

# Launches a temporary debug shell using the service image.

Print container mounts and settings
docker inspect $(docker compose ps -q api)

# Inspects the underlying container created for a service.

## CI/CD and Release Patterns
Build stack in CI
docker compose -f compose.yaml -f compose.ci.yaml build

# Builds the project with CI-specific overrides applied.

Run integration stack and exit with service code
docker compose up --abort-on-container-exit --exit-code-from test

# Runs an integration stack and returns the test container exit code.

Tear down CI stack with volumes
docker compose down -v --remove-orphans

# Fully cleans up the test environment and removes stale containers.

Pull latest images and recreate
docker compose pull && docker compose up -d

# Refreshes image-based services to the newest available tags.

## Production-Minded Compose Patterns
App with healthcheck and restart policy
services:
  app:
    image: myapp:${APP_TAG:-latest}
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:3000/health || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3

# Adds self-healing and health visibility to a service.

Proxy + app pattern
services:
  proxy:
    image: nginx:alpine
    ports:
      - "80:80"
    depends_on:
      app:
        condition: service_started
  app:
    image: myapp:${APP_TAG:-latest}

# Basic reverse-proxy plus app service pattern.

Backup sidecar pattern
services:
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data
  backup:
    image: postgres:16
    profiles: ["ops"]
    depends_on:
      - db
    entrypoint: ["sh", "-c", "pg_dump -h db -U postgres appdb > /backup/appdb.sql"]
    volumes:
      - ./backup:/backup

volumes:
  pgdata: {}

# Defines an operational backup task as a profile-gated service.

Recommended next

No recommendations yet.