Docker Compose
Multi-container applications with declarative configuration
Docker Compose
yaml
version: '3.8'
services:
app:
build: .
ports: ["3000:3000"]
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/myapp
- REDIS_URL=redis://cache:6379
depends_on:
db: { condition: service_healthy }
cache: { condition: service_started }
volumes:
- ./src:/app/src # dev: hot-reload
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: myapp
ports: ["5432:5432"]
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user -d myapp"]
interval: 5s
retries: 5
cache:
image: redis:7-alpine
ports: ["6379:6379"]
volumes: [redisdata:/data]
volumes:
pgdata:
redisdata:💬 What are Docker volumes used for?
Volumes persist data beyond container lifecycle. Without volumes, all data inside a container is lost when it's removed. Named volumes (pgdata:) are managed by Docker. Bind mounts (./src:/app/src) map host paths for development.