Start the cache, then the shop
Docker Compose describes several services, such as a web server and a cache, in one file and starts them together in the right order. It saves typing long commands by hand and getting them wrong.
A shop needs a web server and a cache. Starting each by hand means typing long commands, in the right order, with the right names.
On the third try somebody starts them the wrong way round. One file can say it once, for everyone.
By the end of this lesson
Note
I can describe two services that work together in one Compose file.
Key words
| Word | What it means | Example |
|---|---|---|
| Service | one program in a Compose file, run as one or more containers | |
| Compose file | a text file that describes services and how they connect | |
| Published port | a container door opened on the host | 8080:80 |
| Depends on | a rule that one service waits for another | |
| Health check | a test Docker repeats to see if a service answers |
The cells students fill in are left blank in this preview.
What the lab cannot do
Note
The lab's terminal has no docker compose command. The lab started its own three services for you from a Compose file, which you can open with Edit the file. The file in this lesson was run on our own machine. You read it, look at what the lab's file did, and check start order in the assignment.
One file describes every service
Under services: each service has a name and the image it runs. Other keys say how it connects: its doors, its settings, the volume it keeps and what it waits for.
docker compose up reads the file and starts everything in the right order. Here is a web server and a cache.
Two services in one Compose file (our example)
services:
web:
image: nginx:1.27-alpine
ports:
- "8080:80"
environment:
CACHE_HOST: cache
depends_on:
cache:
condition: service_healthy
cache:
image: valkey/valkey:8.1-alpine
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 3s
volumes:
- cache-data:/data
volumes:
cache-data:
Find the cache by its service name
docker exec web getent hosts cacheShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
See which service started when
docker inspect --format '{{.Name}} {{.State.StartedAt}}' web cache workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: what each key does
| Key | What it does | What we saw |
|---|---|---|
| image | says which image to run | the lab runs nginx and valkey |
| depends_on | makes a service wait for another | in the lab, worker started a few seconds after web and cache |
| healthcheck | tells Docker how to test a service | cache answers valkey-cli ping, so it is healthy |
| environment | hands a setting to the program | printenv CACHE_HOST printed cache |
| volumes | keeps a folder outside the container | cache-data stays when the services are removed |
The cells students fill in are left blank in this preview.
A published port opens a door
Inside the network the services reach each other by name. A visitor on the host needs a door.
"8080:80" means: host port 8080 goes to port 80 in the container. The host side is first. On our machine curl http://localhost:8080 answered 200. The lab's web has no published port: only other services and the lab's Open button reach it.
A common mistake
Warning
Many people think depends_on waits until the other program is ready. On its own it waits only until the other container has started. Give the other service a health check and use condition: service_healthy. Then web starts only when the cache answers.
Is the shared folder writable in web?
docker inspect --format '{{range .Mounts}}{{.Destination}} {{.RW}}{{end}}' webShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
And in worker?
docker inspect --format '{{range .Mounts}}{{.Destination}} {{.RW}}{{end}}' workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
Add a third service to the example file: a worker that depends on both web and cache. Write only its three lines: the name, an image and its depends_on.
Then work out in what order Compose starts the three. The assignment asks the same of any list.