Below is a quick “tool-kit” you can use whenever a container flips to unhealthy and you need to know why the health-check failed.
1. Look at the health object with docker inspect
docker inspect <container> --format '{{json .State.Health}}' | jq
Typical output:
{
"Status": "unhealthy",
"FailingStreak": 5,
"Log": [
{
"Start": "2024-03-21T11:55:11.7265813Z",
"End": "2024-03-21T11:55:12.2310302Z",
"ExitCode": 1,
"Output": "curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused\n"
},
…
]
}
What to look for:
ExitCode – anything ≠ 0 means the command signaled failure.
Output – stdout/stderr from the health-check command.
FailingStreak – how many consecutive failures occurred.
If you only care about the last run:
docker inspect \
--format '{{(index .State.Health.Log 0).Output}}' <container>
2. Replay the health-check command manually
Inside the container:
docker exec -it <container> /bin/sh # or /bin/bash
<run the same command you specified in HEALTHCHECK>
Outside the container (good for CMD-SHELL health-checks):
# Extract the exact command first
docker inspect --format '{{json .Config.Healthcheck}}' <image|container> | jq
# then run it verbatim on your host to see what happens
3. Watch health transitions live with docker events
docker events --filter 'container=<container>' --filter 'event=health_status:.*'
Gives output like:
2024-03-21T12:05:12Z container health_status: unhealthy <container> (healthcheck failed)
4. Common root causes checklist
| Symptom | Likely cause & fix |
|---|
curl: (7) Failed to connect | Service hasn’t started yet → increase start_period or interval, or wait for application to bind port |
Connection refused but service up later | Health-check running before app is ready → same as above |
command not found | Wrong path / missing binary in final image |
Exit code 137 | Health-check killed by OOM / SIGKILL → container may be starved of memory |
| No output at all | Your command suppresses stdout/stderr; make it verbose for debugging |
5. Tips for designing debuggable health-checks
- Always return clear exit codes (
0 = healthy, 1 = unhealthy).
- Emit one-line concise messages; Docker only stores the last 255 chars.
- Use
start_period=30s (or more) when the application has a slow boot.
- Keep the command small (bash ‑c scripts grow quickly; prefer a separate script in the image).
Example:
HEALTHCHECK --interval=30s --timeout=3s --start-period=30s --retries=3 \
CMD curl -fs http://localhost:8080/health || exit 1
6. If using Docker Compose
docker compose ps --format=json | jq '.[].Health'
docker compose logs -f --no-color <service>
Compose shows unhealthy in its status column; the above commands surface the same info as docker inspect.
TL;DR
docker inspect <container> → .State.Health.Log for exit code & output.
- Re-run the extracted command (
docker exec) to reproduce.
- Tweak
interval, timeout, retries, start_period or fix your app as needed.
That’s all you need to pinpoint most HEALTHCHECK failures. Good luck debugging! 🐳