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

Core service definitions and simple stacks.

Minimal web + db stack

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

yamlANYdockercomposeyamlservices
yaml
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret

A good starting point for local dev stacks.

Service built from Dockerfile

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

yamlANYdockercomposebuildyaml
yaml
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: myapp:dev

Common for local development and CI.

Override service command

Runs a different default command than the image CMD.

yamlANYdockercomposecommand
yaml
services:
  worker:
    image: myapp:latest
    command: ["python", "worker.py"]

Useful for workers, migrations, and alternate entrypoints.

Override entrypoint

Overrides the image entrypoint.

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

Use carefully when you control container startup behavior.

Set explicit container name

Pins a specific container name.

yamlANYdockercomposecontainer-name
yaml
services:
  redis:
    image: redis:7
    container_name: local-redis

Useful occasionally, though it reduces scaling flexibility.

Environment Variables and Env Files

Inject configuration into services and interpolate values.

Environment as key/value map

Provide container environment values inline in YAML.

yamlANYdockercomposeenvironment
yaml
services:
  api:
    image: myapi:latest
    environment:
      NODE_ENV: development
      PORT: "3000"

Use strings for values that might be parsed unexpectedly.

Environment as list

Alternative environment syntax using `KEY=value` entries.

yamlANYdockercomposeenvironment
yaml
services:
  api:
    image: myapi:latest
    environment:
      - NODE_ENV=development
      - PORT=3000

Compact style for smaller service definitions.

Load variables from env file

Loads container environment variables from one or more files.

yamlANYdockercomposeenv-file
yaml
services:
  api:
    image: myapi:latest
    env_file:
      - .env
      - .env.local

Good for keeping service config out of the main YAML.

Interpolate variables into compose file

Uses shell-style variable interpolation inside the Compose file.

yamlANYdockercomposevariablesinterpolation
yaml
services:
  api:
    image: "myapi:${APP_TAG:-latest}"
    ports:
      - "${APP_PORT:-3000}:3000"

Great for portable dev and CI environments.

Pass env file at runtime

Uses a specific env file when rendering the Compose project.

bashANYdockercomposeenv-fileruntime
bash
docker compose --env-file .env.production up -d

Useful when switching between local, staging, and prod values.

Profiles and Overrides

Optional services and file layering patterns.

Define optional profiles

Marks a service as optional unless its profile is enabled.

yamlANYdockercomposeprofiles
yaml
services:
  app:
    image: myapp:latest
  mailhog:
    image: mailhog/mailhog
    profiles: ["dev-tools"]

Great for dev-only tools like Mailhog, Adminer, or Jaeger.

Enable a profile

Starts the stack with a selected optional profile enabled.

bashANYdockercomposeprofile
bash
docker compose --profile dev-tools up -d

Use multiple `--profile` flags when needed.

Layer multiple compose files

Merges multiple Compose files in order.

bashANYdockercomposemultiple-files
bash
docker compose -f compose.yaml -f compose.override.yaml up -d

Useful for base + local override or base + CI override patterns.

Use production-specific files

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

bashANYdockercomposeprodoverrides
bash
docker compose -f compose.yaml -f compose.prod.yaml up -d

Keeps local and production concerns separate.

Dependencies and Healthchecks

Startup ordering and service readiness.

Simple service dependency

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

yamlANYdockercomposedepends-on
yaml
services:
  web:
    image: myapp:latest
    depends_on:
      - db
  db:
    image: postgres:16

Order alone does not guarantee the dependency is ready for traffic.

Healthcheck on a service

Defines a readiness-style health check for the service.

yamlANYdockercomposehealthcheck
yaml
services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

Useful for observability and dependency-aware startup.

Wait for healthy dependency

Uses health state as the dependency condition for startup ordering.

yamlANYdockercomposedepends-onhealthcheck
yaml
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

Helpful when an app needs a database that is actually ready.

Recommended next

No recommendations yet.