Sub topic 9
Health checks and the unhealthy state
This chapter assumes you can run containers, read docker inspect output, and have read the earlier chapters on exit codes and restart policies. A running container is not always a working one. A health check lets Docker test that the service inside actually responds.
What a health check does
A health check is a command that Docker runs inside the container on a schedule. The exit code of that command sets the container's health status:
0means the probe succeeded.1means the probe failed.
The status is separate from the lifecycle state. A container can be running and unhealthy at the same time. The status has three values:
| Status | Meaning |
|---|---|
| `starting` | The container is inside its start period, or no probe has succeeded yet. |
| `healthy` | The latest probe succeeded. |
| `unhealthy` | The probe failed the configured number of consecutive times. |
Defining a check
Add a HEALTHCHECK instruction to the Dockerfile:
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1--intervalis the time between probes.--timeoutis how long one probe may run before it counts as a failure.--start-periodis a grace time. Failures during it do not count toward--retries, but a success ends the period early.--retriesis the number of consecutive failures needed to mark the containerunhealthy.
The command runs inside the container, so curl must exist in the image. A missing binary makes every probe fail, and the container becomes unhealthy even though the service is fine.
Reading the results
docker ps shows the status in the STATUS column, for example Up 2 minutes (unhealthy). For the cause, read the probe history:
docker inspect --format '{{json .State.Health}}' webThe output holds Status, FailingStreak, and a Log array of the most recent probes. Each entry has the start and end time, the ExitCode, and the Output of the probe. The output text is usually the fastest clue. It might say curl: not found, Connection refused, or HTTP 503.
Common causes of an unhealthy container
- The probe tool is missing. The output says
not foundor exit code127. Install the tool or use one the image already has. - The service listens on the wrong address. If the app binds to
127.0.0.1it works for a probe inside the container, but a probe aimed at the container's external address fails. Check the host and port in the probe. - The start period is too short. A slow application fails its first probes. Raise
--start-period. - The timeout is too short. Under load, the endpoint responds in 5 seconds and the probe allows 3.
- The endpoint checks too much. If
/healthfails when a database is down, a database outage marks every web container unhealthy.
You can run the same command by hand to reproduce a failure:
docker exec web curl -f http://localhost:8080/health
echo $?Exercise
A container shows Up 5 minutes (unhealthy). docker inspect shows probe output sh: curl: not found with exit code 127. The application itself answers requests normally. What is wrong, and how do you fix it?
Show answer
The health check command uses curl, which is not installed in the image, so every probe fails. The service is fine. Either install curl in the image, or change the probe to a tool the image has, such as wget or a small binary shipped with the application.