Show contents
Chapter 5 of 7 · 20 min
Docker Compose
By the end of this chapter, you'll be able to
- Describe a multi-service application in a compose.yaml file
- Start, watch and stop the application with docker compose
- Handle start-up order with depends_on and a health check
- Keep secrets out of the file thanks to a .env file
The compose.yaml file
The compose.yaml file is written in YAML, a configuration format where indentation (the spaces at the start of lines) shows what belongs to what. Here is the club's library application, with its database:
services:
web:
build: .
ports:
- "5000:5000"
environment:
DATABASE_URL: postgresql://club:${DB_PASSWORD}@db:5432/club
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
environment:
POSTGRES_USER: club
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: club
volumes:
- data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U club"]
interval: 5s
retries: 10
volumes:
data:
services: each service will become one or more containers;build: .: build the image with the current folder's Dockerfile;image:uses an existing image;ports,environment,volumes: the equivalents of-p,-eand-v;volumes:at the very bottom: declares the named volumedata.
Compose automatically creates a network for the application: web reaches the database with the name db, without any configuration.
The commands
docker compose up -d # build if needed and start everything in the background
docker compose ps # the state of each service
docker compose logs -f web # follow the web service's messages
docker compose exec db psql -U club # open a console in the database
docker compose down # stop and remove containers and network
docker compose down -v # … and the volumes too (careful: data erased)
Only now does web start, and it can connect to the database.
Text description of the animation
- Compose creates a network specific to the application, named after the project folder.
- The volume declared at the bottom of the file is created if it does not exist yet.
- The database starts first, since web depends on it.
- Compose waits for pg_isready to answer: the database finally accepts connections.
- Only now does web start, and it can connect to the database.
Start-up order
depends_on tells Compose to start db before web. But "started" does not mean "ready": PostgreSQL takes a few seconds to accept connections. With condition: service_healthy, Compose waits for the database's health check (healthcheck) to succeed. Here, the pg_isready command answers as soon as the database is ready.
Secrets in a .env file
The compose.yaml file is shared in Git with the whole team: it must contain no password. The ${DB_PASSWORD} values are read from a .env file, placed next to it, which is never committed:
# .env — to add to .gitignore
DB_PASSWORD=Gm7-baobab-river-29
Instead, provide a .env.example file with no real value, so every team member knows what to fill in.
Your turn
Worked exercise
Worked exercise: adding a service
Add to the compose.yaml file an admin service that uses the adminer:5 image (a web interface to explore the database), published on port 8081 of your machine (Adminer listens on the container's port 8080). It must only start once the database is healthy. Then give the commands to start the application and follow the messages of this service only.
Hint 1
services:, at the same indentation level as web and db.Hint 2
depends_on as the web service.Show the solution and common mistakes
admin:
image: adminer:5
ports:
- "8081:8080"
depends_on:
db:
condition: service_healthy
docker compose up -d
docker compose logs -f admin
Open http://localhost:8081, choose PostgreSQL, and enter db as the server: on the application's network, that is the database's name.
Common mistakes:
- Writing
"8080:8081": the ports are swapped, your machine's port comes first. - Leaving Adminer reachable on a production server: it is a development tool that gives access to the whole database. Keep it for your own machine.
Summary
- 1A compose.yaml file describes all the services, volumes and settings of an application.
- 2docker compose up -d starts everything; down stops; down -v also deletes the data.
- 3Compose creates a network where each service is reachable by its name.
- 4depends_on with condition: service_healthy waits until the database is really ready.
- 5Secrets go into an uncommitted .env file, read with the ${NAME} syntax.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.