Docker Compose
Definition
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file (docker-compose.yml) to configure application services, networks, and volumes, then creates and starts all services with a single command.
Compose is primarily used for development, testing, and local staging environments. For production orchestration, Kubernetes is typically used instead.
Key Concepts
- Service: A containerized application (e.g., web app, database, cache)
- Network: Docker network connecting services
- Volume: Persistent data storage mounted into containers
- docker-compose.yml: Declarative configuration file
- docker compose up: Start all services
- docker compose down: Stop and remove all services
Example docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
depends_on:
- app
networks:
- frontend
app:
build: .
environment:
- DATABASE_URL=postgres://user:pass@db:5432/mydb
networks:
- frontend
- backend
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- backend
volumes:
pgdata:
networks:
frontend:
backend:
Compose Commands
| Command | Description |
|---|---|
docker compose up |
Start all services |
docker compose up -d |
Start in detached mode |
docker compose down |
Stop and remove all |
docker compose ps |
List running services |
docker compose logs |
View service logs |
docker compose build |
Build images |
docker compose exec |
Execute command in container |
docker compose config |
Validate and display config |
Compose vs Kubernetes
| Feature | Docker Compose | Kubernetes |
|---|---|---|
| Scope | Single host | Multi-node cluster |
| Scale | Manual | Auto-scaling |
| HA | No built-in | Built-in |
| Config | YAML file | YAML/CRD manifests |
| Use case | Dev/local | Production |
| Orchestration | No | Yes |
Related Terms
- Docker — Podman-compatible Compose implementation
- Docker Swarm — Docker’s native orchestration (legacy)
- Dockerfile — defines individual container images
References
- Docker Compose docs: https://docs.docker.com/compose/
- Docker Compose file spec: https://docs.docker.com/compose/compose-file/
- Podman Compose: https://github.com/containers/podman-compose