Sub topic 4
Writing a healthcheck
This chapter assumes you can read a compose.yaml file, run docker compose up and docker compose down, and use basic shell commands. It covers how to define a healthcheck. The next chapter covers how to tune its timing.
What a healthcheck is
A healthcheck is a command that Docker runs inside a container on a schedule. If the command exits with code 0, Docker records the container as healthy. If it exits with code 1, Docker counts a failure. Any other exit code is reserved and should not be used.
The command tests the service, not just the process. A PostgreSQL container can have a running process while the database is still replaying its logs and refusing connections. A healthcheck asks the database directly whether it can accept connections.
The check is only as good as the command you pick. It should be cheap, fast, and tied to what your dependents need.
A first healthcheck
Add a healthcheck block to the service that others depend on. This example checks PostgreSQL with pg_isready, a tool that ships in the postgres image.
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: 10sEach field controls one part of the check:
| Field | Meaning |
|---|---|
| `test` | The command to run inside the container. |
| `interval` | How long to wait between checks. |
| `timeout` | How long one check may run before it counts as failed. |
| `retries` | How many consecutive failures mark the container unhealthy. |
| `start_period` | A grace period after start. Failures during it do not count toward `retries`. |
Here Docker runs pg_isready every 5 seconds. Failures in the first 10 seconds are ignored. After that, five failed checks in a row mark db as unhealthy.
The two forms of test
The test field takes a list or a string. The first list element selects how Docker runs the command.
["CMD", "pg_isready", "-U", "postgres"]runs the program directly, with no shell. Each argument is a separate list item.["CMD-SHELL", "pg_isready -U postgres"]passes one string to the container's default shell (/bin/sh). Use this form when you need pipes,||, or environment variable expansion.["NONE"]disables a healthcheck that the image defines.
The command must exist in the image
The command runs inside the container, not on your host. If the image lacks the tool, every check fails, and the container never becomes healthy. This is a common cause of a service stuck as unhealthy, and the troubleshooting chapter returns to it.
Slim images often omit curl. A web service might use one of these instead:
services:
web:
image: myapp:latest
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://localhost:8080/health || exit 1"]
interval: 10s
timeout: 3s
retries: 3The -f flag makes curl exit with an error on HTTP status 400 or higher. Without it, a 500 response would still exit 0 and look healthy. If curl is missing, use a tool the image does have, or install one when you build the image.
How the check relates to depends_on
A healthcheck on its own does not change startup order. It only produces a status. A dependent service uses that status through depends_on with condition: service_healthy, which a later chapter covers. Until then, you can see the status with docker compose ps, which shows (healthy) or (unhealthy) beside the container's state.
Choosing what to test
Pick a check that matches what dependents need.
- Test the capability, not the process.
pg_isreadyconfirms the database accepts connections. A check for the process name does not. - Keep it light. The check runs forever, so avoid heavy queries.
- Use credentials the container already has. The example passes
-U postgresso the check uses a user that exists.
Exercise
A web image is based on a slim distribution with no curl. Its healthcheck uses ["CMD-SHELL", "curl -fsS http://localhost:8080/health || exit 1"], and the container stays unhealthy even though the app works. What is the likely cause, and what are two fixes?
Show answer
The curl command does not exist inside the container, so every check fails. Fix it by installing curl when you build the image, or by using a tool the image already has (such as wget or a command from the app's own runtime) as the test command.