Sub topic 10
Defining health checks in Docker Compose
This chapter assumes you can write a Dockerfile with a HEALTHCHECK instruction. It also assumes you know the probe exit codes (0 is healthy, 1 is unhealthy) and the timing options, and that you can read a basic compose.yaml.
Why define the check in Compose
A HEALTHCHECK in a Dockerfile travels with the image. Compose lets you define or change the check per service, without rebuilding. This is useful when you run an image you did not build, such as postgres:16, or when you want different timing in development than in production.
A healthcheck block in Compose replaces the check baked into the image for that service. Compose passes it to Docker as the same runtime override you saw with the --health-* flags.
The healthcheck block
The keys match the Dockerfile options.
| Compose key | Dockerfile option | Default |
|---|---|---|
| `test` | the command after `CMD` | none |
| `interval` | `--interval` | `30s` |
| `timeout` | `--timeout` | `30s` |
| `retries` | `--retries` | `3` |
| `start_period` | `--start-period` | `0s` |
| `start_interval` | `--start-interval` | `5s` |
Durations are strings such as 5s or 1m30s.
Here is a database service with a probe, plus a web service that depends on it:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
web:
build: .
depends_on:
db:
condition: service_healthyThe web service starts only after db reports healthy. The next chapter covers that ordering in detail. Here, focus on how test is written.
The three forms of test
The test key accepts a list or a string. The first list element selects how the command runs.
["CMD", "arg1", "arg2"]runs the program directly, with no shell. Shell features such as||,&&and pipes do not work.["CMD-SHELL", "command"]runs the string with the container's/bin/sh -c. Use it when you need shell syntax. The image must contain a shell.["NONE"]disables any check inherited from the image.
A plain string, such as test: pg_isready -U postgres, is treated as CMD-SHELL.
Overriding and disabling an image's check
Suppose your image already has the Python probe from the Dockerfile chapter, and you want a faster check in development:
services:
app:
image: myapp
ports:
- "8000:8000"
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8000/health"]
interval: 5s
timeout: 2s
retries: 2If you only set some keys, Compose still replaces the image's test only when you provide test. To switch a check off entirely, use disable:
services:
app:
image: myapp
healthcheck:
disable: trueThis is equivalent to docker run --no-healthcheck. It is also what test: ["NONE"] does.
Checking the result
Start the stack and watch the status column:
docker compose up -d
docker compose ps
docker inspect --format '{{json .State.Health}}' "$(docker compose ps -q db)"docker compose ps shows (health: starting) and then (healthy). The docker inspect call returns the same log of probe results as for a plain container.
Common errors
- Using
CMDwith shell operators, as shown in the warning above. - Probing with a tool the image lacks.
curlis missing from many slim images, so the probe exits non-zero and the service never becomes healthy. Use a tool the image has, or a short script. - Setting
start_periodtoo low for a slow database, so early failures count towardretries. Failures during the start period do not count, but only if it is long enough. - Putting
healthcheckunderdepends_oninstead of on the dependency. The check belongs to the service being waited on.
Exercise
A Compose service uses test: ["CMD", "curl -f http://localhost:8000/health || exit 1"] and stays in starting and then turns unhealthy. Name the cause and give a corrected test line.
Show answer
With CMD, Docker runs a program named curl -f http://localhost:8000/health || exit 1, which does not exist, so every probe fails. Use the shell form: test: ["CMD-SHELL", "curl -f http://localhost:8000/health || exit 1"]. If the image has no curl, use a tool it does have, such as wget -q --spider, or a Python one-liner.