Sub topic 5
Starting, healthy and unhealthy
This chapter assumes you can write a Dockerfile with a HEALTHCHECK instruction and know that the probe command returns exit code 0 for success and 1 for failure. Here you learn how Docker turns a series of probe results into one of three health states.
The three states
A container with a health check has a health status in addition to its normal running status. The status is one of three values:
starting: the container has just started and no verdict has been reached yet.healthy: the most recent probe succeeded.unhealthy: the probe has failed several times in a row.
A container without a health check has no health status at all. docker ps shows only Up 5 minutes, with no (healthy) suffix.
How the transitions work
Docker runs the probe once every --interval. The first probe runs one interval after the container starts, not immediately. Each probe ends in one of two ways:
- Success: the command exits with 0 before the timeout.
- Failure: the command exits with 1, or it runs longer than
--timeoutand Docker kills it. A timeout counts as a failure.
Docker keeps a counter of consecutive failures. The rules are:
- A container begins in
starting. - One successful probe moves it to
healthyand resets the counter to 0. - Failures that happen inside the
--start-periodwindow do not count toward--retries. - After the start period, each failure increments the counter. When the counter reaches
--retries, the status becomesunhealthy. - One successful probe at any time returns the container to
healthy.
The consecutive requirement matters. With --retries 3, the failure pattern fail, fail, success, fail never makes the container unhealthy, because the success resets the counter.
A worked timeline
Suppose a container uses --interval=30s --timeout=3s --start-period=10s --retries=3, and the application never comes up.
| Time | Event | Status |
|---|---|---|
| 0s | Container starts | starting |
| 30s | Probe 1 fails. This is after the 10s start period, so it counts: 1 of 3. | starting |
| 60s | Probe 2 fails: 2 of 3. | starting |
| 90s | Probe 3 fails: 3 of 3. | unhealthy |
The status changes only after about 90 seconds. If you need faster detection, shorten the interval or lower the retries. A shorter interval also means more probe load, so choose a balance.
If the application takes 40 seconds to boot and the start period is 10s, the probes at 30s fail and count against the retries. A start period shorter than your real startup time can push a slow but normal container toward unhealthy. Set --start-period a little longer than your slowest normal startup.
Watching the transitions
Poll the status and the failure counter with docker inspect:
docker run -d --name myapp -p 8000:8000 myapp
for i in 1 2 3 4 5 6; do
docker inspect --format '{{.State.Health.Status}} failures={{.State.Health.FailingStreak}}' myapp
sleep 10
doneFailingStreak is the consecutive failure counter described above. You should see starting failures=0, then healthy failures=0 after the first successful probe.
To see an unhealthy container, stop the application inside it or point the probe at a wrong port. The streak climbs by one each interval until it reaches the retries value.
Common mistakes
- Expecting
unhealthyright after the first failed probe. It takes--retriesconsecutive failures. - Setting
--timeoutlonger than--interval. Slow probes then pile up and the results are hard to read. - Treating
startingas a problem. It is the normal state until the first success.
Exercise
A container has --interval=10s, --start-period=30s and --retries=3. The application is broken from the beginning and every probe fails. Roughly when does the status become unhealthy, and why?
Show answer
Probes run at 10s, 20s and 30s, and these fall inside the 30s start period, so their failures are not counted. The probe at 40s is the first counted failure (1 of 3), then 50s (2 of 3) and 60s (3 of 3). The status becomes unhealthy at about 60 seconds. Until then it stays in starting.