Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Two services in a Compose file

After this lesson: I can describe two services that work together in one Compose file.

55 min lesson4 commandsDocker basics lab: learn containers, logs and restarts by fixing a broken service

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

WordWhat it meansExample
Serviceone program in a Compose file, run as one or more containers
Compose filea text file that describes services and how they connect
Published porta container door opened on the host8080:80
Depends ona rule that one service waits for another
Health checka 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)

Code: bash
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

Command
docker exec web getent hosts cache

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

See which service started when

Command
docker inspect --format '{{.Name}} {{.State.StartedAt}}' web cache worker

Shown 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

KeyWhat it doesWhat we saw
imagesays which image to runthe lab runs nginx and valkey
depends_onmakes a service wait for anotherin the lab, worker started a few seconds after web and cache
healthchecktells Docker how to test a servicecache answers valkey-cli ping, so it is healthy
environmenthands a setting to the programprintenv CACHE_HOST printed cache
volumeskeeps a folder outside the containercache-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?

Command
docker inspect --format '{{range .Mounts}}{{.Destination}} {{.RW}}{{end}}' web

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

And in worker?

Command
docker inspect --format '{{range .Mounts}}{{.Destination}} {{.RW}}{{end}}' worker

Shown 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.

About this lab and its source: Docker basics lab: learn containers, logs and restarts by fixing a broken service

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.