Docker Compose & Multi-Container Apps
Complete guide to Docker Compose v2, compose.yaml specification, services, volumes, networks, environment files, and multi-container orchestration.
Docker Compose is a declarative tool for defining and running multi-container Docker applications using a single YAML configuration file (compose.yaml or docker-compose.yml).
Compose File Specification (compose.yaml)
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- DATABASE_URL=postgresql://postgres:secret@db:5432/myapp
depends_on:
db:
condition: service_healthy
volumes:
- .:/app
- /app/node_modules
networks:
- app-network
db:
image: postgres:16-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
POSTGRES_DB: myapp
ports:
- "127.0.0.1:5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
networks:
- app-network
volumes:
pgdata:
networks:
app-network:
driver: bridgeEssential Compose Commands (Compose V2)
[!NOTE] Modern Compose is integrated directly into the Docker CLI as
docker compose(without a hyphen).
# Start all services in detached background mode (builds if needed)
docker compose up -d
# Rebuild images and start
docker compose up -d --build
# View logs from all services or a specific service
docker compose logs -f
docker compose logs -f web
# Check status of running compose services
docker compose ps
# Execute command inside a compose service
docker compose exec web npm test
docker compose exec db psql -U postgres
# Stop all services (preserves volumes)
docker compose stop
# Stop and remove containers, networks, and anonymous volumes
docker compose down
# Stop and destroy everything including named volumes
docker compose down -vRelated Recipes
Docker Container Lifecycle & States
Complete guide to container lifecycle states (created, running, paused, stopped, dead), transitions, and lifecycle commands.
Docker Multi-Stage Builds
Complete guide to optimizing image sizes, stripping build tools, caching dependencies, and creating secure production images using multi-stage builds.