Docker Compose
Docker Compose is a tool for defining and running multi-container applications from one YAML file. One docker compose up command creates the services, networks, and volumes the file describes.
What Compose solves
A real application is rarely one container: it needs an API, a database, and a cache. Without Compose you start each with a long docker run, wire networks by hand, and remember the order. Compose describes the whole stack in compose.yaml, creates a shared network with name-based DNS, and starts everything with one command.
services: payment-api: image: registry.example.com/payment-api:v3.1.0 ports: - "8080:8080" environment: DB_HOST: postgres REDIS_HOST: redis depends_on: postgres: condition: service_healthy restart: unless-stopped postgres: image: postgres:16-alpine environment: POSTGRES_DB: payments POSTGRES_USER: api_user POSTGRES_PASSWORD_FILE: /run/secrets/db_password secrets: - db_password volumes: - postgres-data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U api_user -d payments"] interval: 10s retries: 5 redis: image: redis:7-alpine volumes: postgres-data: secrets: db_password: file: ./secrets/db_password.txtThe top-level version: field is obsolete in current Compose and can be omitted.
Essential commands
docker compose up -d # start in the backgrounddocker compose up -d --build # rebuild images firstdocker compose ps # status and healthdocker compose logs -f payment-apidocker compose exec postgres psql -U api_user paymentsdocker compose config # print the resolved filedocker compose down # remove containers, keep volumesdocker compose down -v # also delete volumes and their dataCompose v1 and v2
Compose v2 is built into the Docker CLI as docker compose (with a space). The old standalone docker-compose binary is Compose v1 and is no longer maintained.
Remember
downkeeps your data volumes, anddown -vdeletes them permanently. Check the flag before pressing Enter.
SecurityDo not commit secrets in the Compose file. Use ignored
.envfiles, Compose secrets, or a secret manager.
Common MistakeTreating a single-host Compose setup as highly available. If that host fails, everything on it fails.
Frequently Asked Questions
What does Compose solve that a shell script of docker run commands does not?
It declares networks, volumes, environment, and dependencies in one reviewable file and recreates the stack reproducibly. Services get DNS names on a project network, so there is no manual wiring or ordering logic.
Where does Compose stop being the right tool?
Compose manages one Docker host. It has no cross-host scheduling, self-healing, or autoscaling, so production workloads needing node failure tolerance should move to Kubernetes or a managed container service.
What is the difference between docker compose and docker-compose?
docker compose is Compose v2, integrated into the Docker CLI. docker-compose is the legacy Python v1 tool. The file format is the same, but v2 is the supported one.