Difficulty: Beginner
What is Docker Compose and why is it used? How do services in a Compose file talk to each other?
Docker Compose is a tool for defining and running multi-container applications from a single YAML file, usually compose.yaml. You describe each service (its image or build, ports, environment, volumes, dependencies) once, then docker compose up creates the network, volumes and containers together, and docker compose down tears them down. It turns a long list of docker run commands into versioned, shareable configuration.
When you run docker compose up -d in a project directory, Compose:
compose.yaml and merges compose.override.yaml if present.<project>_default.volumes:.build: section, or pulls the image:.On that network every service is reachable by its service name through Docker's built-in DNS. If you have services api and redis, the API connects to redis:6379, not localhost:6379. Inside a container, localhost means that container itself.
For multi-node production with self-healing and autoscaling, teams typically move to Kubernetes or a managed container service; Compose is a single-host tool.
docker compose (with a space). The old Python docker-compose v1 is end of life.version: key is obsolete under the Compose Specification; current Compose ignores it and prints a warning.depends_on only controls start order by default. To wait until a database is actually ready, add a healthcheck to the database and use condition: service_healthy.docker compose watch with a develop.watch section syncs file changes or rebuilds automatically, an alternative to bind mounts for hot reload.profiles let you start optional services (for example a monitoring stack) only when asked.localhost from one container to reach another.depends_on waits for readiness, causing the API to crash on boot because Postgres is still initialising.docker compose down -v and losing the local database volume.environment:; use an .env file that is git-ignored, or Compose secrets.# compose.yaml (no version key needed)
services:
api:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:devpass@db:5432/orders
REDIS_URL: redis://cache:6379
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: devpass
POSTGRES_DB: orders
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d orders"]
interval: 5s
retries: 10
cache:
image: redis:7-alpine
volumes:
pgdata:
The API reaches the database at host db and Redis at host cache via Compose DNS, and it only starts once Postgres passes its healthcheck.
docker compose up -d --build # build images and start in background
docker compose ps # status of services
docker compose logs -f api # follow logs of one service
docker compose exec db psql -U app orders
docker compose down # stop and remove containers + network
docker compose down -v # ALSO delete named volumes (data loss!)
These cover the daily loop of starting, inspecting, debugging and tearing down a local stack.
Docker Compose, compose.yaml, Service Discovery, depends_on, Multi-container Apps