What is Docker Compose?
What It Does
Docker Compose lets you manage multiple containers as a single unit. Instead of running several 'docker run' commands with many flags, you write a 'compose.yaml' (or 'docker-compose.yml') file that specifies each service, its image or build context, ports, environment variables, volumes, and dependencies.
A typical Compose file has a 'services' section with entries for your app, database, cache, etc. You can also define named volumes and custom networks. Compose handles creating and starting all containers in the correct order, and it provides a single command to stop and remove everything.
- services: defines containers (web, db, redis)
- image: or build: specifies where the container comes from
- ports: maps host ports to container ports
- volumes: mounts host directories or named volumes
- depends_on: controls startup order (but not readiness)
Common Commands
Use 'docker compose up' to start all services in the foreground, or 'docker compose up -d' to run them in the background. 'docker compose down' stops and removes containers, networks, and default volumes. 'docker compose logs -f' follows logs from all services.
Compose is great for local development and simple production deployments. For complex production orchestration, Kubernetes or Docker Swarm might be better, but Compose remains a popular choice for single-host setups.
Common mistakes
- Assuming 'depends_on' waits for a service to be ready — it only waits for the container to start, not for the app inside to be healthy.
- Using the old 'docker-compose' command (with hyphen) instead of the newer 'docker compose' plugin, though both may work depending on installation.
- Forgetting to rebuild images after changing code when using 'build:' — run 'docker compose up --build' to pick up changes.
