Docker Compose · DevOps: Containers, CI and Kubernetes
How do you run an app that needs a database, a cache and a worker?
Course contentsWhy you need it
Real apps are several programs: a web server, a database, a cache, a background worker. Starting each by hand with the right settings is error-prone. Docker Compose describes them all in one file, so the whole stack starts with one command on any machine.
The intuition
A compose.yaml lists services. Each one is a container:
services:
web:
build: . # build from the Dockerfile here
ports:
- "8080:8000" # your machine's 8080 → the container's 8000
environment:
DATABASE_URL: postgres://shop:shop@db:5432/shop
depends_on: [db] # start db first
db:
image: postgres:16 # use a ready-made image
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:- Services reach each other by service name: inside
web, the database is at hostdb, notlocalhost(each container has its own localhost). portspublishes a port to your machine:"HOST:CONTAINER".- A named volume keeps data when the container is recreated.
depends_oncontrols start order. (It waits for the container to start, not for the database to be ready to accept connections; add a healthcheck for that.)
docker compose up starts everything; docker compose down stops it.
YAML is indentation-sensitive: use spaces, never tabs.
Definition
Service
One container in a Compose file, with its image or build, ports, environment and volumes.
Volume
Storage that lives outside a container, so data survives when the container is replaced.
Port mapping
"8080:8000" forwards port 8080 on your machine to port 8000 inside the container.
Examples
A web app and its database
Run it in the lessonRun it to see the start order and the parsed file.
services:
web:
build: .
ports:
- "8080:8000"
environment:
DATABASE_URL: postgres://shop:shop@db:5432/shop
depends_on: [db]
db:
image: postgres:16
environment:
POSTGRES_USER: shop
POSTGRES_PASSWORD: shop
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:- L3web is built from the Dockerfile in this folder.
- L5Open http://localhost:8080 on your machine to reach port 8000 in the container.
- L7The host is the service name db.
- L8db starts before web.
- L15Database files live in a named volume, so they survive a restart.
Common mistakes
localhost inside a container
In a container, localhost is that container itself. Use the other service's name (db, redis) as the host.
Data without a volume
A database without a volume loses everything when its container is recreated. Mount a named volume at its data folder.
Expecting depends_on to wait for readiness
depends_on only orders the starts. The app should retry its database connection, or use a healthcheck with condition: service_healthy.
Quick quiz
1. Inside the `api` container, what host reaches the `redis` service?
Show the answer
Answer: A
Compose gives every service a name that the others can use as a hostname.
2. `ports: ["5000:80"]`. Where do you point your browser?
Show the answer
Answer: A
HOST:CONTAINER: your machine's 5000 forwards to the container's 80.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictStart order
In what order does Compose start these services? (When several are ready at once, it starts them alphabetically here.)
- Modifyweb can't reach the database
webfails with "connection refused" when it talks to the database. Fix the database host inDATABASE_URL, and make suredbstarts beforeweb. - WriteA three-service stack
Write a Compose file with three services:
web: built from., publishes8080:8000, starts afterapiapi: imageshop-api:1.4, environmentDB_HOST: db, starts afterdbdb: imagepostgres:16, with a named volumedbdatamounted at/var/lib/postgresql/data
Declare the
dbdatavolume at the top level too.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Docker Compose | Docker Compose | Runs several containers described in one YAML file. |
| Volume | 卷 | Storage that outlives a container. |
| YAML | YAML | An indentation-based text format for configuration. |