Docker Compose turns a multi-container application into a single YAML file, a single docker compose up, and a single namespaced lifecycle. It’s the easiest path from “I have a Dockerfile” to “I’m running my app, its database, and its cache together.” This article covers Compose v2 (the Go-based plugin shipped via docker-compose-plugin) and the file layout you’ll see in real projects.
How to verify
Check whether you have Compose v2 and what it sees in the current directory:
docker compose version
ls -la compose.yaml docker-compose.yml docker-compose.yaml 2>/dev/null
docker compose config
docker compose ps
docker compose top
docker-compose (with a hyphen) is the legacy Python tool — end of life. The current command is docker compose (space). If docker compose version errors, the plugin isn’t installed; on Debian/Ubuntu, sudo apt-get install docker-compose-plugin fixes it. docker compose config prints the resolved file with all variable expansion and overrides applied — the single best command to confirm “what is Compose actually going to do?”
What’s happening
Compose reads a YAML file (default compose.yaml, also accepts docker-compose.yml), expands environment variables, merges any override files, and produces a fully-resolved configuration. From that configuration, it derives a set of containers, networks, and volumes, prefixed with the project name (by default the directory name) so multiple Compose projects coexist on a host without collision.
A Compose stack is conceptually a single unit — up creates everything, down removes everything. Within the stack, services can depend on each other (depends_on), share networks, mount shared volumes, and refer to each other by service name via DNS. The “project” is the namespace; everything Compose creates is tagged with com.docker.compose.project=<name> so it can find and clean up its own resources.
The procedure
- A minimal
compose.yamlfor a web app with PostgreSQL and Redis:
name: myapp
services:
web:
image: ghcr.io/example/web:1.42.0
restart: unless-stopped
ports:
- "127.0.0.1:8080:8080"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
REDIS_URL: redis://cache:6379/0
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-fsS", "http://localhost:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- pg-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
cache:
image: redis:7-alpine
restart: unless-stopped
command: ["redis-server", "--maxmemory", "256mb", "--maxmemory-policy", "allkeys-lru"]
volumes:
- redis-data:/data
volumes:
pg-data:
redis-data:
Note: there’s no version: field. Compose v2 deprecates it — the schema is determined by docker compose itself. Also note depends_on with condition: service_healthy — the web container won’t start until db reports healthy, which requires the healthcheck on db.
- The
.envfile in the same directory provides variable values:
DB_PASSWORD=changeme-not-this-one
COMPOSE_PROJECT_NAME=myapp
COMPOSE_PROJECT_NAME overrides the default (directory name). Helpful when you want stable names regardless of where the file is cloned.
- Bring the stack up and watch what happens:
docker compose up -d
docker compose ps
docker compose logs -f web
docker compose exec web sh
docker compose down
up -d runs in detached mode. logs -f web tails the web service. exec runs commands in a running container. down stops and removes containers, networks, and the project’s anonymous volumes; named volumes (like pg-data above) survive unless you add --volumes.
- Use override files to layer environment-specific config without forking the base file:
# compose.override.yaml — picked up automatically alongside compose.yaml
services:
web:
ports:
- "0.0.0.0:8080:8080" # dev-only: expose to LAN
volumes:
- ./src:/app/src # dev-only: live code reload
environment:
LOG_LEVEL: debug
For production, an explicit compose.prod.yaml is clearer than an override:
docker compose -f compose.yaml -f compose.prod.yaml up -d
Compose merges the two: lists are replaced, maps are merged, scalars in later files win.
- Profiles let you keep optional services in the same file without starting them by default:
services:
metrics:
image: prom/prometheus:latest
profiles: ["observability"]
docker compose up -d skips metrics. docker compose --profile observability up -d includes it.
- Inspect the resolved configuration before committing:
docker compose config # full resolved YAML
docker compose config --services # list of service names
docker compose config --volumes # list of named volumes
docker compose config --quiet # validate without printing
CI can run docker compose config --quiet as a syntax check.
Common pitfalls
- Putting secrets in the YAML file or in
.envand committing them..envbelongs in.gitignore; secrets belong in Docker secrets, a vault, or environment variables injected by the orchestrator. See Docker secrets handling. - Using
depends_onwithoutcondition: service_healthyand discovering the web container races the database on startup. The defaultdepends_ononly waits for the dependency container to exist, not to be ready. - Running
docker compose up(no-d) and Ctrl-C in production — that takes everything down. Always use-dfor anything beyond local dev. - Forgetting that
docker compose downremoves the network — a sibling stack that needed to reach it now fails. Usedocker compose stopif you want to leave the resources in place. - Putting
build:andimage:together with a tag likelatest— Compose builds, but never pushes;latestwill overwrite stale dev images quietly. For production, build in CI and reference the tag.
For larger or more careful Compose deployments, see Docker Compose in production. We treat every client Compose file as a release artifact — versioned, reviewed, and shipped via managed operations.