What is Docker Compose?
Docker Compose lets you define a multi-container application in a single docker-compose.yml file and start everything with one command.
docker compose up -d # start all services in background
docker compose down # stop and remove containers
docker compose logs -f app # follow logs for 'app' service
docker compose ps # list running servicesA Full Stack Example
# docker-compose.yml
services:
app:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://postgres:secret@db:5432/mydb
REDIS_URL: redis://cache:6379
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
volumes:
- .:/app # mount source for live reload
- /app/node_modules # don't override node_modules
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: secret
POSTGRES_DB: mydb
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
cache:
image: redis:7-alpine
volumes:
- redis_data:/data
volumes:
pg_data:
redis_data:Networking
All services in the same Compose file share a default network. Services reach each other by service name:
app → db:5432 (not localhost:5432)
app → cache:6379 (not localhost:6379)
Environment Files
# .env (not committed to git)
POSTGRES_PASSWORD=secret
JWT_SECRET=supersecretservices:
app:
env_file:
- .envOverride Files
Use docker-compose.override.yml for local dev differences:
# docker-compose.override.yml (auto-loaded in dev)
services:
app:
command: npm run dev # override prod CMD
volumes:
- .:/appKeep docker-compose.yml production-safe and put developer conveniences (volume mounts, debug ports) in docker-compose.override.yml.