Sub topic 7
Setting a policy with docker run and changing it with docker update
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes (0 means success, non-zero means failure). You also need to know that a container stops when its main process exits.
Setting a policy at creation time
Use the --restart flag on docker run. The value is one of no, on-failure, always or unless-stopped. The on-failure policy also accepts a maximum retry count after a colon.
docker run -d --name web --restart unless-stopped nginx:alpineThis container restarts after crashes and after a daemon restart, unless you stopped it yourself.
To limit retries, add a count to on-failure:
docker run -d --name flaky --restart on-failure:3 alpine sh -c 'exit 1'The command inside the container always exits with code 1. Docker restarts it up to three times, then leaves it stopped. Without :3, on-failure retries with no limit.
If you leave out --restart, the policy is no, and the container stays stopped after it exits.
Checking what was applied
Read the policy back with docker inspect and a Go template:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flakyThe output has three fields: the configured policy name, the number of restarts Docker has performed, and the current status. For flaky, after a short wait you should see on-failure 3 exited.
Changing a policy with docker update
You do not need to remove and recreate a container to change its policy. docker update modifies it in place:
docker update --restart no webThe container keeps running, and from now on Docker will not restart it automatically. You can update several containers in one command by listing their names or IDs.
The flow below shows the two paths for setting a policy.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Using `--restart on-failure:3` with a successful exit | Docker does not restart it, because exit code 0 is not a failure | Use `always` or `unless-stopped` for services that should never exit |
| Adding a retry count to `always` or `unless-stopped` | Docker rejects it, because only `on-failure` takes a count | Remove the count |
| Misspelling the policy name | `docker run` fails with an invalid restart policy error | Use one of the four exact names |
| Combining `--restart` with `--rm` | Docker refuses the combination | Choose one. `--rm` removes the container on exit, which conflicts with restarting it |
Exercise
Exercise
A container named api was started with docker run -d --name api myimage and has no restart flag. Which command makes Docker restart it after a crash or a daemon restart, but not after you stop it manually, without recreating it?
Show answer
docker update --restart unless-stopped api. The container was created with the default policy no, and docker update changes the policy in place. You can confirm it with docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' api.
Exercise
What does --restart on-failure:5 do when the container's main process exits with code 0?
Show answer
Nothing. Docker restarts only after a non-zero exit code, so a clean exit leaves the container stopped. The retry limit of 5 applies only to failed exits.