Sub topic 11
Inspecting the policy and restart count
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes. It shows how to see which restart policy a container has, how many times Docker has restarted it, and what state it is in now.
Where Docker stores restart data
docker inspect returns a JSON document for a container. Three fields matter for restarts:
| Field | Meaning |
|---|---|
| `HostConfig.RestartPolicy.Name` | The configured policy: `no`, `on-failure`, `always` or `unless-stopped`. |
| `HostConfig.RestartPolicy.MaximumRetryCount` | The retry limit for `on-failure`. It is `0` when no limit is set. |
| `RestartCount` | How many times the Docker daemon has restarted the container. |
State.Status and State.ExitCode are also useful. State.Status is running, restarting, exited and so on. State.ExitCode is the exit code of the last run.
Reading the fields with a format string
Full docker inspect output is long. The -f option takes a Go template and prints only the fields you ask for.
Start a container that always fails, with at most three retries:
docker run -d --name flaky --restart on-failure:3 alpine sh -c 'exit 1'Then read its restart data:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flakyA few seconds after the start, the output looks like this:
on-failure 3 exitedThe policy is on-failure. Docker restarted the container three times, hit the limit and stopped. The container is now exited.
To see the retry limit and last exit code too, add those fields:
docker inspect -f 'policy={{.HostConfig.RestartPolicy.Name}} max={{.HostConfig.RestartPolicy.MaximumRetryCount}} restarts={{.RestartCount}} status={{.State.Status}} exit={{.State.ExitCode}}' flakyExpected output:
policy=on-failure max=3 restarts=3 status=exited exit=1Watching a restart as it happens
While Docker waits to restart a container, State.Status is restarting. You can poll it:
docker run -d --name web --restart unless-stopped nginx:alpine
docker inspect -f '{{.RestartCount}} {{.State.Status}}' webA healthy web shows 0 running. If the count climbs between two checks, the container is exiting repeatedly. Use docker ps -a for a quick look at several containers. The STATUS column shows text such as Restarting (1) 5 seconds ago, where the number in parentheses is the last exit code.
Checking many containers at once
docker inspect accepts several names. This prints one line per container:
docker inspect -f '{{.Name}} {{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' web flakyContainer names in .Name start with a slash, for example /web. This is normal.
How the count behaves
The diagram below shows how the count and status change under on-failure:3.
Keep these points in mind:
RestartCountcounts restarts done by the daemon's restart policy. It does not count a manualdocker restartordocker start.- The count resets when you recreate the container with
docker rmanddocker run. Changing the policy withdocker updatekeeps the same container, so the count is kept. - A container that stays up for a while before failing can have its count reset by Docker. Do not treat the number as a lifetime total.
Common mistakes
- Reading
docker psonly. It shows the current state, not the configured policy. Usedocker inspectfor the policy. - Expecting a count on a container with policy
no. Docker never restarts it, so the count stays0. - Forgetting quotes around the template. Without single quotes, the shell can break the
{{ }}braces.
Exercise
A container shows always 12 restarting from your inspect format string. What does each value tell you, and what would you check next?
Show answer
The policy is always, Docker has restarted the container 12 times, and it is currently waiting to restart again. The container keeps exiting. Next, run docker logs on it and check State.ExitCode with docker inspect to find why the main process stops.
Exercise
Write one command that prints the policy name and restart count of a container called api.
Show answer
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' api