11. Docker Compose: Multi-Container Applications
Defining and Running Multi-Service Apps
Running a multi-container application with individual `docker run` commands is tedious and error-prone. Docker Compose lets you define all your services (app, database, cache, queue, reverse proxy) in a single YAML file and start everything with one command.
Docker Compose Fundamentals
Docker Compose is a tool for defining and running multi-container Docker applications. You define your application's services, networks, and volumes in a `docker-compose.yml` file, then run `docker compose up` to start everything. Compose creates a dedicated network for your application, so services can reach each other by their service name. It also handles build order (building images if needed) and dependency ordering with `depends_on`. Compose is primarily for development and testing, but can be used for production in smaller deployments. For production, consider Docker Swarm (with `docker stack deploy`) or Kubernetes.
# docker-compose.yml version 3.8
version: '3.8'
services:
web:
build: .
ports:
- "8080:8080"
environment:
- NODE_ENV=production
- DB_HOST=database
- REDIS_HOST=redis
depends_on:
- database
- redis
restart: unless-stopped
database:
image: postgres:15-alpine
environment:
POSTGRES_DB: myapp
POSTGRES_USER: appuser
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis_data:/data
volumes:
postgres_data:
redis_data:
Essential Compose Commands
The `docker compose` CLI (note: modern versions use `docker compose` without a hyphen) replaces the need for separate `docker run`, `docker network create`, and `docker volume create` commands. `docker compose up -d` starts all services in detached mode. `docker compose down` stops and removes all containers (but preserves volumes unless you add `-v`). `docker compose logs -f` shows logs from all services. `docker compose exec service_name command` runs a command in a running service container — essential for database migrations and debugging.
# Start services in background docker compose up -d # Start with build (rebuild images before starting) docker compose up -d --build # View logs from all services docker compose logs -f # View logs from specific service docker compose logs -f web # Run one-off command (e.g., database migrations) docker compose exec web npm run migrate # Open a shell in a service docker compose exec web bash # Stop services (containers removed, volumes preserved) docker compose down # Stop and remove volumes (destroys data!) docker compose down -v # Rebuild images without cache docker compose build --no-cache # List services and their status docker compose ps
Environment Variables in Compose
Never hardcode secrets in your `docker-compose.yml` file. Compose supports variable substitution from a `.env` file in the same directory. The `.env` file contains key=value pairs. In your Compose file, use `${VARIABLE_NAME}` syntax. Compose also reads variables from your shell environment. This allows you to keep secrets out of version control — `.env` should be added to `.gitignore`.
# .env file (keep out of git!)
DB_PASSWORD=supersecret
API_KEY=abc123
NODE_ENV=production
# docker-compose.yml using .env
services:
database:
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
web:
environment:
NODE_ENV: ${NODE_ENV}
API_KEY: ${API_KEY}
Service Dependencies and Startup Order
The `depends_on` option ensures services start in the correct order, but it does NOT wait for the service to be ready — only that the container has started. For example, if your app depends on PostgreSQL, `depends_on` will start the database container, then start your app container — but your app might start before PostgreSQL is ready to accept connections. To solve this, use health checks combined with `condition: service_healthy`. This tells Compose to wait until the dependency's health check passes before starting the dependent service.
services:
database:
image: postgres:15-alpine
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
timeout: 5s
retries: 5
app:
build: .
depends_on:
database:
condition: service_healthy
# App will not start until database is healthy
Knowledge Check
Ready to test your understanding of 11. Docker Compose: Multi-Container Applications?