TL;DR
Master Docker Compose V2 with this comprehensive guide covering new features, best practices, and advanced orchestration patterns for modern containerized applications
Docker Compose V2: The Complete Guide to Modern Container Orchestration
Executive Summary
Docker Compose V2 defines a multi-container application in a single YAML file and runs it with one command. Rewritten in Go and shipped as a Docker CLI plugin (docker compose, no hyphen), it implements the open Compose Specification, which means the same file format works across tools. This guide covers the working core — services, healthchecks, and dependency conditions — plus the features that make Compose manageable on real projects: profiles, include and extends, override files for per-environment differences, secrets, networks, and resource limits. After reading it, you will be able to structure a Compose setup that a whole team can develop against, and recognise the point where a project has outgrown Compose entirely.
Why This Matters
Every team that runs more than one container locally reinvents the same thing badly: a shell script full of docker run commands with hand-managed networks, startup sleeps, and environment variables nobody can reconstruct on a new laptop. The cost shows up as onboarding time, "works on my machine" drift between developer setups, and integration bugs discovered in CI that a faithful local environment would have caught at the desk. Compose is the standardised answer — the application's topology becomes a reviewed file in the repository, identical for every developer and close enough to production to be useful. Getting its details right matters because the small features, healthcheck-gated dependencies in particular, are the difference between a stack that starts reliably and one that fails randomly on cold boots.
Core Concepts
Services, Healthchecks, and Dependency Conditions
A Compose file declares services, named volumes, and networks. The part teams most often get wrong is startup ordering: depends_on in its short form only waits for a container to start, not for the process inside it to be ready. The long form with conditions fixes that, provided the dependency defines a healthcheck:
# docker-compose.yml
services:
web:
build:
context: ./web
dockerfile: Dockerfile
args:
NODE_ENV: development
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://user:pass@db:5432/dbname
depends_on:
db:
condition: service_healthy
deploy:
replicas: 2
resources:
limits:
cpus: '0.50'
memory: 512M
db:
image: postgres:14-alpine
volumes:
- db_data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=dbname
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user -d dbname"]
interval: 5s
timeout: 5s
retries: 5
volumes:
db_data:Three conditions cover most real topologies — wait for healthy, wait for started, and wait for a one-shot job to finish, which is the correct way to sequence database migrations:
# docker-compose.yml
services:
app:
build: .
depends_on:
db:
condition: service_healthy
restart: true
redis:
condition: service_started
migration:
condition: service_completed_successfully
migration:
image: flyway
command: migrate
depends_on:
db:
condition: service_healthyHealthchecks deserve the same care in your own services: a check that hits a real readiness endpoint beats a port-open check, because the failures you care about are application-level. Even with perfect conditions, applications should retry initial connections — conditions reduce the race, they do not repeal distributed systems.
Profiles
Real projects accumulate optional services: a mail catcher, an admin UI, a worker you only run sometimes. Profiles keep them out of the default run:
# docker-compose.yml
services:
web:
profiles: ["web", "frontend"]
# ... service configuration ...
api:
profiles: ["api", "backend"]
# ... service configuration ...
db:
profiles: ["db", "backend"]
# ... service configuration ...
redis:
profiles: ["cache", "backend"]
# ... service configuration ...Services without a profile always start; profiled services start when their profile is enabled or when another service depends on them. One file can then serve the frontend developer, the backend developer, and CI, each starting only their slice.
Compose Files: Base, Override, Include, Extends
Environment differences belong in additional files, not in copies of the whole file. Compose automatically merges compose.yaml with compose.override.yaml — the natural home for development settings such as bind mounts and debug flags — and named files can be added explicitly with -f for production variants:
# docker-compose.override.yml
services:
web:
ports:
- "3000:3000"
volumes:
- .:/app:cached
environment:
- DEBUG=trueFor larger projects, include pulls other Compose files into one project, and extends reuses a service definition across files:
# docker-compose.yml
include:
- base.yml
services:
web:
extends:
file: base.yml
service: base
environment:
- VIRTUAL_HOST=example.comUse include to compose independently owned stacks; use extends sparingly for genuine service-level reuse, because deep inheritance chains make the effective configuration hard to inspect — docker compose config prints the merged result and should be your first debugging step.
Networks, Volumes, and Resources
Compose creates a dedicated network per project, and services resolve each other by name — that default covers most development needs. Multiple networks let you express segmentation, for example a frontend network that can reach the API and a backend network, marked internal, that holds the database. Named volumes are the right home for persistent data; bind mounts are for source code in development, and confusing the two causes both lost data and permission surprises. The deploy section carries resource limits and restart policy:
# docker-compose.yml
services:
app:
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120sNote that outside Swarm mode, parts of deploy are honoured by Compose V2 (resource limits and restart policy among them) while replica semantics differ from an orchestrator's — treat Compose limits as guardrails for a workstation or single host, not as scheduling.
Secrets and Logging
Pass secrets as files, not environment values, wherever the application supports reading from a path — environment variables leak into logs, crash reports, and docker inspect output:
# docker-compose.yml
services:
web:
secrets:
- db_password
- ssl_cert
environment:
- DB_PASSWORD_FILE=/run/secrets/db_password
secrets:
db_password:
file: ./secrets/db_password.txt
ssl_cert:
file: ./secrets/ssl_cert.pemThe referenced files stay outside version control. For logging, set rotation explicitly; the default JSON file driver grows without bound on a long-lived host:
# docker-compose.yml
services:
app:
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"Decision Framework
| Situation | Recommendation |
|---|---|
| Local development of a multi-service app | Compose V2 with healthcheck-gated dependencies and profiles for optional services |
| CI integration testing | Same Compose file in CI, services started with the test profile; guarantees environment parity |
| Small production deployment on one host | Compose with the production file, resource limits, log rotation, and a restart policy — a legitimate setup at this scale |
| Multiple hosts, autoscaling, self-healing required | You have outgrown Compose; move to Kubernetes or a managed container service |
| Team members on different stacks | Profiles plus override files so each workflow starts only what it needs |
| Complex shared bases across many projects | include for whole stacks; avoid deep extends chains |
Implementation Checklist
service_healthy conditionservice_completed_successfullydocker compose up starts the common stack-f files, never in divergent copies of the base filedocker compose config output has been reviewed — it is the configuration that actually runsTrade-offs
Compose's simplicity is also its ceiling. There is no scheduler: no placement across hosts, no autoscaling, no self-healing beyond restart policies, and a failed host takes its containers with it. The merged-configuration model, while powerful, can surprise — a value in an override file silently winning over the base is a classic source of "but I changed the file" confusion, which is why reviewing the rendered config matters. Profiles and includes scale the file model a long way, but a project with dozens of services will eventually feel the absence of an orchestrator's service discovery, rollout control, and health-based traffic management. Treat Compose as the best tool in its class — single-host, developer-centred — and resist stretching it into a production platform for anything beyond small, well-understood deployments.
When Not to Use
Do not use Compose as your production orchestrator for a multi-host, high-availability service; that is the problem Kubernetes and managed container platforms exist to solve, and Compose's restart policies are not a substitute. Do not use it to model a large microservices estate locally — starting forty services on a laptop helps nobody; run the services you are changing and stub or share the rest. And do not use Compose files as a dumping ground for production secrets and environment-specific values in version control: the moment a Compose setup needs real secret management and per-environment review, it has become infrastructure that deserves the same tooling as the rest of your estate.
Conclusion
Compose V2 earns its place by making a multi-container application one reviewed file and one command. Use healthchecks and dependency conditions to make startup deterministic, profiles and file composition to keep one project serving many workflows, and file-based secrets and log rotation for hygiene. When the deployment target outgrows a single host, take the service definitions as documentation and move up to an orchestrator — deliberately, not by stretching Compose past its design.
References
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.