Sub topic 12
What happens when a container turns unhealthy
This chapter assumes you can write a HEALTHCHECK, read the starting, healthy and unhealthy states, and use docker ps and docker inspect. It covers what Docker does after a container reaches unhealthy, and what it leaves to you.
Docker only reports the status
With plain docker run, an unhealthy container keeps running. Docker does three things when the retry count is exhausted:
- It sets
State.Health.Statustounhealthy. - It shows
(unhealthy)in the STATUS column ofdocker ps. - It emits a
health_statusevent.
It does not stop, restart, or kill the container. It does not stop sending traffic through published ports either. The health state is information. Something else has to act on it.
Where the status is used
Different tools treat the status differently.
| Tool | Reaction to unhealthy |
|---|---|
| `docker run` | None. The status is displayed and an event is emitted. |
| Docker Compose | None at runtime. `service_healthy` is checked only when starting dependents. |
| Docker Swarm services | The task is treated as failed and replaced by a new one. |
| Kubernetes | Ignores the Dockerfile `HEALTHCHECK`. It uses its own probes. |
Compose is an easy one to get wrong. depends_on with condition: service_healthy orders startup. If db becomes unhealthy later, web is not stopped or restarted.
An unhealthy container can recover on its own. A single successful probe resets the failure count and moves the status back to healthy.
Watching for the change
Subscribe to events to see transitions as they happen:
docker events --filter type=container --filter event=health_statusEach line names the container and the new status, for example health_status: unhealthy. Monitoring agents and log shippers often consume this stream.
To list the containers that are unhealthy right now, filter docker ps:
docker ps --filter health=unhealthyActing on unhealthy containers
If you want automatic recovery outside Swarm, you add it yourself. The simplest form is a loop that restarts whatever is unhealthy:
#!/bin/sh
# restart-unhealthy.sh: restart every container currently reported unhealthy
for id in $(docker ps --filter health=unhealthy --format '{{.ID}}'); do
echo "Restarting $id"
docker restart "$id"
doneRun it from cron or a systemd timer. Dedicated "autoheal" containers do the same job by watching the Docker socket. That socket gives full control of the host, so grant it only to software you trust.
Restarting is not always the right response. If the database behind your app is down, restarting the app will not help and may cause a restart loop. Decide per service whether a restart can fix the failure.
Making a failed check useful
- Keep
retrieshigh enough that a brief slowdown does not flip the status. The unhealthy transition needs consecutive failures. - Make the probe test something a restart can repair, such as the app's own responsiveness.
- Read
docker inspect --format '{{json .State.Health}}' NAMEafter a failure. The log holds the last probe outputs and exit codes, which usually name the cause.
Exercise
A container has --restart always and its web server hangs, so every probe fails and docker ps shows (unhealthy). The process is still alive. Will Docker restart the container? What two options do you have to recover automatically?
Show answer
No. The restart policy reacts only to the main process exiting, and the hung process has not exited. Options: run the container as a Swarm service, where unhealthy tasks are replaced, or add an external watcher, such as a script or autoheal container, that runs docker restart on containers returned by docker ps --filter health=unhealthy. Making the process exit when it detects it is stuck would also let the restart policy work.