Cloud Notes
Master Docker Compose for defining and running multi-container applications, covering service configuration, networking, volumes, and production deployment patterns.
Docker Compose is a tool for defining and running multi-container Docker applications. Using a YAML file, you configure all your application's services, networks, and volumes, then create and start everything with a single command.
Docker Compose Architecture
| │ docker-compose.yml defines | │ |
| │ │ │ | 80 │──│ :3000 │──│ :5432 │ │ │ |
| │ │ Port 80 | 80 ┌────┴────┐ │ │ |
Complete Docker Compose Example
Docker Compose Commands
# Start all services
docker compose up -d
# Start with build
docker compose up -d --build
# View running services
docker compose ps
# View logs
docker compose logs -f
docker compose logs -f api # Single service
# Stop all services
docker compose down
# Stop and remove volumes (clean slate)
docker compose down -v
# Scale a service
docker compose up -d --scale api=3
# Execute command in a service
docker compose exec api npm run migrate
# Restart a single service
docker compose restart api
# View resource usage
docker compose topEnvironment Variables and Secrets
# docker-compose.yml with .env file
services:
api:
image: my-api:${APP_VERSION:-latest}
env_file:
- .env
- .env.production
environment:
- API_KEY=${API_KEY}
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txtDevelopment vs Production Compose
# docker-compose.override.yml (dev - auto-loaded)
services:
api:
build:
context: ./backend
volumes:
- ./backend:/app # Live code reload
- /app/node_modules
environment:
- NODE_ENV=development
- DEBUG=true
command: npm run dev# Production deployment
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -dHealth Checks and Dependencies
Interview Questions
- What is Docker Compose and when would you use it?
Docker Compose defines multi-container applications in a YAML file. Use it when your application has multiple services (web server, API, database, cache) that need to work together. It simplifies local development and can be used for single-host production deployments.
- Explain the difference between
depends_onand health checks.
depends_on only waits for a container to start (process running). Health checks verify the service is actually ready (database accepting connections). Combine them with condition: service_healthy to ensure dependent services wait for true readiness.
- How do you manage secrets in Docker Compose?
Use Docker secrets (file-based), environment variables from .env files (not committed to git), or external secret managers. Never hardcode secrets in docker-compose.yml. For production, use Docker Swarm secrets or external tools like Vault.
- How would you set up a development environment with Docker Compose?
Use bind mounts for live code reload, docker-compose.override.yml for dev-specific settings, development commands (hot reload), exposed debug ports, and lighter resource constraints. This gives the full production stack locally with developer ergonomics.
- What are the limitations of Docker Compose for production?
Single-host only (no clustering), limited health management, no rolling updates natively, no load balancing across hosts. For production at scale, use Kubernetes, Docker Swarm, or ECS. Compose is ideal for development, testing, and small single-server deployments.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Docker Compose.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Cloud Computing topic.
Search Terms
cloud-computing, cloud computing, cloud, computing, containers, and, orchestration, docker
Related Cloud Computing Topics