services:
web:
image: nginx:alpine
ports:
- "8080:80"
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secretA good starting point for local dev stacks.
compose.yaml examples for services, variables, profiles, overrides, and dependency-aware startup.
Core service definitions and simple stacks.
services:
web:
image: nginx:alpine
ports:
- "8080:80"
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secretA good starting point for local dev stacks.
services:
app:
build:
context: .
dockerfile: Dockerfile
image: myapp:devCommon for local development and CI.
services:
worker:
image: myapp:latest
command: ["python", "worker.py"]Useful for workers, migrations, and alternate entrypoints.
services:
app:
image: myapp:latest
entrypoint: ["./docker-entrypoint.sh"]Use carefully when you control container startup behavior.
services:
redis:
image: redis:7
container_name: local-redisUseful occasionally, though it reduces scaling flexibility.
Inject configuration into services and interpolate values.
services:
api:
image: myapi:latest
environment:
NODE_ENV: development
PORT: "3000"Use strings for values that might be parsed unexpectedly.
services:
api:
image: myapi:latest
environment:
- NODE_ENV=development
- PORT=3000Compact style for smaller service definitions.
services:
api:
image: myapi:latest
env_file:
- .env
- .env.localGood for keeping service config out of the main YAML.
Uses shell-style variable interpolation inside the Compose file.
services:
api:
image: "myapi:${APP_TAG:-latest}"
ports:
- "${APP_PORT:-3000}:3000"Great for portable dev and CI environments.
docker compose --env-file .env.production up -dUseful when switching between local, staging, and prod values.
Optional services and file layering patterns.
services:
app:
image: myapp:latest
mailhog:
image: mailhog/mailhog
profiles: ["dev-tools"]Great for dev-only tools like Mailhog, Adminer, or Jaeger.
docker compose --profile dev-tools up -dUse multiple `--profile` flags when needed.
docker compose -f compose.yaml -f compose.override.yaml up -dUseful for base + local override or base + CI override patterns.
docker compose -f compose.yaml -f compose.prod.yaml up -dKeeps local and production concerns separate.
Startup ordering and service readiness.
services:
web:
image: myapp:latest
depends_on:
- db
db:
image: postgres:16Order alone does not guarantee the dependency is ready for traffic.
services:
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5Useful for observability and dependency-aware startup.
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: 5Helpful when an app needs a database that is actually ready.