Compose design
- Docker & Kubernetes course
- Beginner
- Lesson 61/82
- 4 min
Design the structure of a docker-compose.yml file.
Project: a multi-service app with Compose
Real applications don't consist of a single container: a web app, a database, maybe a cache. Starting each with long docker run commands is tedious and error-prone. Docker Compose defines all the services in a single compose.yaml file and manages them with one command:
docker compose up -d # create and start all services
docker compose ps # show their status
docker compose down # stop and removeIn this project you'll build a Python API + PostgreSQL + Redis stack step by step.
The skeleton of compose.yaml
The file's top-level keys are services (containers), volumes (named volumes) and networks (custom networks). In YAML, indentation carries meaning; spaces are used, not tabs:
services:
web:
image: nginx:alpine
ports:
- "8080:80"Instead of the old docker-compose (hyphenated, v1), the docker compose (v2) built into Docker is used now; the version: line at the top isn't needed either.
Practice The first compose file
Write a compose.yaml: the web service uses the nginx:alpine image and maps host port 8080 to container port 80.
Starter code
# compose.yaml
# buraya yazSample solution
# compose.yaml
services:
web:
image: nginx:alpine
ports:
- "8080:80"Expected output
[+] Running 2/2 ✔ Network proje_default Created ✔ Container proje-web-1 Started
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is Compose's main advantage over docker run commands?
- Not needing images
- Working only in production
- All service settings live in one file under version control Correct answer
- Faster containers
Explanation: Anyone who gets the file sets up the same stack with docker compose up.
2 Write the top-level key defining the services.
____:
web:
image: nginx:alpine
services:
web:
image: nginx:alpine
Explanation: Each service corresponds to one or more containers.
3 Match.
| Term | Meaning |
|---|---|
services |
Container definitions |
volumes |
Named volumes |
docker compose up -d |
Start the stack |
docker compose down |
Remove the stack |