Docker Compose Files and YAML Patterns

compose.yaml examples for services, variables, profiles, overrides, and dependency-aware startup.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## compose.yaml Basics
Minimal web + db stack
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret

# Simple two-service stack with port publishing and environment variables.

Service built from Dockerfile
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: myapp:dev

# Build a service image from local source while also tagging it.

Override service command
services:
  worker:
    image: myapp:latest
    command: ["python", "worker.py"]

# Runs a different default command than the image CMD.

Override entrypoint
services:
  app:
    image: myapp:latest
    entrypoint: ["./docker-entrypoint.sh"]

# Overrides the image entrypoint.

Set explicit container name
services:
  redis:
    image: redis:7
    container_name: local-redis

# Pins a specific container name.

## Environment Variables and Env Files
Environment as key/value map
services:
  api:
    image: myapi:latest
    environment:
      NODE_ENV: development
      PORT: "3000"

# Provide container environment values inline in YAML.

Environment as list
services:
  api:
    image: myapi:latest
    environment:
      - NODE_ENV=development
      - PORT=3000

# Alternative environment syntax using `KEY=value` entries.

Load variables from env file
services:
  api:
    image: myapi:latest
    env_file:
      - .env
      - .env.local

# Loads container environment variables from one or more files.

Interpolate variables into compose file
services:
  api:
    image: "myapi:${APP_TAG:-latest}"
    ports:
      - "${APP_PORT:-3000}:3000"

# Uses shell-style variable interpolation inside the Compose file.

Pass env file at runtime
docker compose --env-file .env.production up -d

# Uses a specific env file when rendering the Compose project.

## Profiles and Overrides
Define optional profiles
services:
  app:
    image: myapp:latest
  mailhog:
    image: mailhog/mailhog
    profiles: ["dev-tools"]

# Marks a service as optional unless its profile is enabled.

Enable a profile
docker compose --profile dev-tools up -d

# Starts the stack with a selected optional profile enabled.

Layer multiple compose files
docker compose -f compose.yaml -f compose.override.yaml up -d

# Merges multiple Compose files in order.

Use production-specific files
docker compose -f compose.yaml -f compose.prod.yaml up -d

# Applies production overrides like replicas, image tags, and secrets.

## Dependencies and Healthchecks
Simple service dependency
services:
  web:
    image: myapp:latest
    depends_on:
      - db
  db:
    image: postgres:16

# Starts `db` before `web` in dependency order.

Healthcheck on a service
services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

# Defines a readiness-style health check for the service.

Wait for healthy dependency
services:
  web:
    image: myapp:latest
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

# Uses health state as the dependency condition for startup ordering.

Recommended next

No recommendations yet.