Docker Compose
Describing a multi-container app - web server, app, and database - in one YAML file.
On this page4 sections
Almost nothing real is just one container. A typical app is a web server, an application process, and a database, all talking to each other. Docker Compose lets you describe all of them, and how they connect, in a single YAML file.
Anatomy of a compose file
services:
web:
image: nginx
ports:
- "80:80"
app:
build: .
depends_on:
- db
db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
- services - one entry per container: which image to run (or build), ports to expose, what it depends on.
- depends_on - start order;
appwon't start untildbhas. - volumes - named, persistent storage that survives a container being recreated - essential for a database, which should never lose its data just because its container got rebuilt.
One command instead of many
Without Compose, standing up that same three-container app means three separate docker run commands, each with the right flags, in the right order, every single time. With a compose file written once:
docker compose up -d # start everything
docker compose down # stop and remove everything
docker compose logs -f # follow logs from every service at once
Why this matters for local development
Compose is most commonly reached for to spin up an entire application stack - with its database, cache, and message queue - on a single laptop, in one command, identically for every developer on the team. No more "works on my machine because I have Postgres 14 and you have 15."
Try it
touch docker-compose.yml
When you're ready, hit Mark complete and move to the next lesson.
Challenge
Create an empty docker-compose.yml - the file where a multi-container app gets described in one place.
Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.
Quiz
Lock in the concepts. Score at least 80% to mark the lesson complete.
Done with this one?
Skipped the challenge? You can mark the lesson complete by hand.