show me a realistic example of when to use docker-compose.yml.
- User
show me a realistic example of when to use docker-compose.yml.
Assistant
show me a realistic example of when to use docker-compose.yml.
Here's a realistic, production-level example where docker-compose.yml shines—a full-stack web application with multiple interconnected services:
You're building an online store with:
docker-compose.yml Solution:version: '3.8'
services:
# REACT FRONTEND
frontend:
build: ./frontend # Uses Dockerfile in frontend dir
ports:
- "80:80"
depends_on:
- backend
environment:
- API_URL=http://backend:3000
# NODE.JS BACKEND
backend:
build: ./backend
ports:
- "3000:3000"
environment:
- DB_HOST=postgres
- REDIS_HOST=redis
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_started
# POSTGRES DATABASE
postgres:
image: postgres:14-alpine
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=secret
# REDIS CACHE
redis:
image: redis:7-alpine
# ORDER PROCESSING WORKER
worker:
build: ./worker
depends_on:
- backend
- redis
environment:
- QUEUE_URL=redis://redis:6379
# MONITORING STACK
prometheus:
image: prom/prometheus
volumes:
- ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=secret
ports:
- "3001:3000"
volumes:
postgres-data:
Single-command orchestration:
docker compose up -d
Starts all 7 services with correct dependencies
Service discovery:
postgres:5432redis:6379backend:3000Health checks & dependencies:
pg_isready)Persistent storage:
volumes:
postgres-data:
Database survives container restarts
Centralized configuration:
Environment variables in one place (could use .env file)
Development efficiency:
Production readiness:
docker compose up -d --scale worker=3)docker network create ecom-net
docker run -d --name postgres \
--network ecom-net \
-v postgres-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=secret \
--health-cmd="pg_isready -U postgres" \
postgres:14-alpine
For this e-commerce example, Docker Compose saves hours of setup time, prevents configuration errors, and ensures all developers work with the exact same service topology. The alternative would be maintaining complex bash scripts or documentation that quickly becomes outdated.