Skip to main content

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.

YAML
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.txt

The top-level version: field is obsolete in current Compose and can be omitted.

Essential commands

Bash
docker compose up -d # start in the background
docker compose up -d --build # rebuild images first
docker compose ps # status and health
docker compose logs -f payment-api
docker compose exec postgres psql -U api_user payments
docker compose config # print the resolved file
docker compose down # remove containers, keep volumes
docker compose down -v # also delete volumes and their data

Compose 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

down keeps your data volumes, and down -v deletes them permanently. Check the flag before pressing Enter.

Security

Do not commit secrets in the Compose file. Use ignored .env files, Compose secrets, or a secret manager.

Common Mistake

Treating 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.

Found a mistake, an outdated step or a broken link?