Docker Compose
What is Docker Compose?
Section titled “What is Docker Compose?”Running a real application almost always means running more than one container — a web server, a database, maybe a cache. Without Compose you would open multiple terminals, type a long docker run command for each service, and remember to wire them together manually on the same network. That process is fragile and impossible to share reliably.
Docker Compose lets you describe your entire multi-container application in a single file called compose.yaml. One command starts everything:
docker compose upOne command tears it all down:
docker compose downcompose.yaml vs long docker run commands
Section titled “compose.yaml vs long docker run commands”Compare running a simple web app two ways.
Without Compose:
docker network create myapp-netdocker run -d \ --name web \ --network myapp-net \ -p 8080:8080 \ -e APP_ENV=production \ myapp:latestWith Compose:
services: web: image: myapp:latest ports: - "8080:8080" environment: - APP_ENV=productionThe Compose version is self-documenting, version-controlled, and reproduced identically by anyone on your team.
What this module covers
Section titled “What this module covers”This module walks you through every layer of Docker Compose:
| Lesson | Topic |
|---|---|
| 1 (this page) | Why Compose — one-file declarative apps |
| 2 | compose.yaml structure in depth |
| 3 | Multi-service apps with networking |
| 4 | Environment variables, volumes, and networks |
| 5 | Lifecycle commands and scaling |
A minimal first Compose app
Section titled “A minimal first Compose app”Here is the smallest useful compose.yaml — a single Nginx service:
services: web: image: nginx:alpine ports: - "8080:80"Start it with docker compose up (add -d to run in the background). Visit http://localhost:8080 and you see the Nginx welcome page. Stop it with Ctrl-C, or docker compose down if you used -d.
Hands-on practice
Section titled “Hands-on practice”The snippet below writes a compose.yaml, starts it, and checks the running service — all inside Play with Docker.
# Write a minimal compose.yaml
cat > compose.yaml <<'EOF'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
EOF
# Start in detached mode
docker compose up -d
# Check running services
docker compose ps
# Fetch the Nginx welcome page
curl -s http://localhost:8080 | head -5
# Tear it down
docker compose down