Docker Compose for Local Dev
Docker Compose lets you define and run multi-container applications with a single YAML file.
Why Compose?
Real apps need more than one container: a web server, a database, a cache, maybe a queue. Compose wires them all together.
docker-compose.yml
yaml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://user:pass@db:5432/myapp
- REDIS_URL=redis://cache:6379
depends_on:
- db
- cache
volumes:
- .:/app # Hot reload for development
- /app/node_modules
db:
image: postgres:15-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: myapp
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
cache:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
postgres_data:Essential commands
bash
# Start all services
docker compose up -d
# See logs
docker compose logs -f app
# Stop everything
docker compose down
# Rebuild after Dockerfile changes
docker compose up --build
# Run a one-off command
docker compose exec app npm run migrateVolumes
Named volumes (like postgres_data) persist data between container restarts. Your database won't lose data when you run docker compose down.
Bind mounts (like .:/app) sync your local files into the container — essential for hot-reload during development.