Sub topic 13
Troubleshooting a container stuck in starting or unhealthy
This chapter assumes you can build and run images, read docker ps output, and know that a shell command returns 0 on success and non-zero on failure. It also assumes you have met the HEALTHCHECK options and the three health states. Here you will work through the usual causes of a check that never reaches healthy.
Start with the probe log
Guessing wastes time. Docker keeps the output of the last few probes, so read it first.
docker inspect --format '{{json .State.Health}}' myappThe Log array holds each probe's start time, ExitCode and Output. FailingStreak shows how many consecutive probes have failed. The exit code and output usually name the cause.
You can also run the probe by hand inside the container. This shows exactly what Docker sees:
docker exec myapp python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=2)"
echo $?Decision flow
Stuck in starting
A container stays in starting while the check is inside its start period or has not yet passed. Common causes:
- Slow startup. Failures during
--start-perioddo not count toward--retries, but a success ends the start period early. If the app needs 60 seconds and the start period is 10, the failures begin to count too soon. Raise--start-periodto cover a realistic startup time. - Long interval. The first probe runs one
--intervalafter the container starts. With--interval=5m, you wait five minutes to see any result. - A probe that hangs. If the command ignores
--timeout, it is killed when the timeout expires and counts as a failure. Give the probe its own short timeout, as in theurlopen(..., timeout=2)call.
Unhealthy from the first probe
| Symptom in the log | Likely cause | Fix | Column | Column |
|---|---|---|---|---|
| Exit code 127, "not found" | `curl` or `wget` is not in the image | Use a tool the image has, such as the Python one-liner, or copy a script in | ||
| Exit code 126 | Script is not executable | Run `chmod +x` in the Dockerfile | ||
| Connection refused | App listens on `127.0.0.1` in a different port, or only on another interface | Check the port and bind address, and probe the address the app actually uses | ||
| Exit code 1, HTTP 500 | The app is running but its endpoint reports a problem | Fix the application or the dependency it reports on | ||
| Timed out | Probe is slower than `--timeout` | Raise the timeout or make the endpoint cheaper | ||
| Exit code other than 0 or 1 | Reserved or unexpected code | End the probe command with ` | exit 1` |
A frequent trap is probing localhost while the app binds to a specific address. Inside the container, localhost is the container's own loopback. The probe never goes through the published port, so a wrong -p mapping does not cause a failure, but a wrong container port does.
Another trap is Windows line endings in a script. A healthcheck.sh saved with CRLF endings fails with "not found" even though the file exists, because the shell looks for an interpreter named /bin/sh\r.
Check that the settings took effect
Overrides can hide the problem. A --health-cmd flag at runtime replaces the Dockerfile check, and --no-healthcheck removes it. If a container shows no health status at all, inspect the configuration:
docker inspect --format '{{json .Config.Healthcheck}}' myappA result of null means no check is defined, so nothing is wrong with the probe. In Compose, docker compose config shows the merged result of your files.
Remember what unhealthy does
Docker only records the status. It does not restart the container or stop it. If you expected a restart, the fix is in the orchestrator or a tool that acts on the status, not in the probe.
Exercise
A container has been in the starting state for several minutes. docker inspect shows a FailingStreak of 0 and an empty Log. What is the most likely cause, and how would you confirm it?
Show answer
The first probe has not run yet, so --interval is probably very long (for example 5m). Confirm it by checking .Config.Healthcheck with docker inspect and reading the Interval value, then shorten it.
Exercise
The probe log shows exit code 127 with the output "wget: not found". Give two ways to fix it.
Show answer
Install wget in the image, or replace the probe with a tool the image already has, such as a Python one-liner that requests the health URL. A copied script that uses an available tool also works.