Sub topic 6
Understanding starting, healthy, and unhealthy
This chapter assumes you can read a compose.yaml file, run docker compose up and docker compose down, and use basic shell commands. It also assumes you have seen a healthcheck block, as covered in the earlier chapters on writing a healthcheck and tuning its timing options.
The three health states
A container with a healthcheck has a health status in addition to its running state. Docker reports one of three values:
| Status | Meaning |
|---|---|
| `starting` | The container is running, but the check has not yet passed. Docker is still within the start period or has not yet seen a first success. |
| `healthy` | The most recent check passed. |
| `unhealthy` | The check has failed `retries` times in a row after the start period. |
A container with no healthcheck has no health status at all. It is simply running. This matters later, because a service_healthy condition cannot be met by a service that defines no check.
How the state changes
Docker runs the test command inside the container every interval. Each run ends in one of two ways. An exit code of 0 is a success. A non-zero exit code, or a run that exceeds timeout, is a failure.
The container starts in starting. It moves to healthy on the first success. Failures during start_period are ignored, so a slow database does not use up its retries while it initializes. After the start period, consecutive failures are counted. When the count reaches retries, the status becomes unhealthy.
The counter resets on any success. A container that is unhealthy can return to healthy if a later check passes.
A worked example
Here is the healthcheck from the previous chapter:
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: 10sSuppose PostgreSQL needs 8 seconds to accept connections. The first checks run during the 10-second start period and fail. Compose ignores those failures for the retry count, so the status stays starting. The first check after PostgreSQL is ready succeeds, and the status becomes healthy.
Now suppose PostgreSQL never starts, for example because of a bad configuration. Once the start period ends, five consecutive failures at 5-second intervals take about 25 seconds. The status then becomes unhealthy.
Note that a success during the start period still counts. Docker marks the container healthy as soon as a check passes, even if the start period has not ended.
Reading the state
To print the current status of the db container, use docker inspect:
docker inspect --format '{{.State.Health.Status}}' "$(docker compose ps -q db)"The output is starting, healthy, or unhealthy. If the container has no healthcheck, the command fails because .State.Health does not exist.
docker compose ps shows the same information in its status column, for example Up 20 seconds (healthy).
Common errors
- Stuck in
starting. Checks are failing during the start period, or the first check has not yet run. Wait forstart_periodplus oneinterval, then look at the check output. - Going
unhealthyimmediately. Thetestcommand is wrong. A missing binary in the image, such ascurlin a minimal image, gives a non-zero exit code on every run. - Flapping between states. The
timeoutis shorter than the check's normal run time under load. Raisetimeoutor make the check cheaper.
You can see the output of recent checks with docker inspect --format '{{json .State.Health.Log}}' "$(docker compose ps -q db)".
Exercise
A service has interval: 10s, retries: 3, and start_period: 30s. Its check fails every time for the first 45 seconds of the container's life. Is the container unhealthy at the 45-second mark? Explain.
Show answer
Not necessarily. Failures in the first 30 seconds are ignored for the retry count. Checks run at about 10s intervals, so failures at about 40s and 50s are the first that count, which is fewer than 3. At 45 seconds at most one counted failure exists, so the status is still starting. It becomes unhealthy only after 3 consecutive counted failures, around the 60-second mark.