Sub topic 9
What happens after docker stop and after a daemon restart
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes. It covers two events that restart policies treat differently: a manual docker stop and a restart of the Docker daemon (the background service that manages containers).
Two events, two rules
A restart policy reacts to a container exiting by itself. Two other events follow separate rules:
- Manual stop. You run
docker stop. Docker records that a person stopped the container and does not restart it right away, whatever the policy. - Daemon restart. The daemon starts again, for example after a reboot or a service restart. Docker then decides which containers to start, based on policy and on whether you stopped them manually.
Manual stop
When you run docker stop web, Docker sends the main process a termination signal and waits for it to exit. Even with --restart always, the container stays stopped. If Docker restarted it at once, you could never stop it.
You can see this with the first example from the plan:
docker run -d --name web --restart unless-stopped nginx:alpine
docker stop web
docker ps -a --filter name=web --format '{{.Names}} {{.Status}}'The status shows Exited, and the container does not come back on its own.
Daemon restart
After the daemon restarts, what happens depends on the policy and on how the container was last stopped.
| Policy | Container was running when the daemon stopped | Container was stopped manually before the daemon restarted |
|---|---|---|
| `no` | Stays stopped | Stays stopped |
| `on-failure` | Stays stopped if it had exited cleanly. A container that was running is started again. | Stays stopped |
| `always` | Started again | Started again |
| `unless-stopped` | Started again | Stays stopped |
The last row is the difference between always and unless-stopped. With always, a container you stopped by hand returns when the daemon restarts. With unless-stopped, Docker remembers your manual stop and leaves the container down.
A running container is stopped by the daemon when the daemon shuts down. Containers with a policy other than no are started again once the daemon is back. You can make containers survive a daemon restart without stopping, by enabling live restore in the daemon configuration, but that is a separate feature from restart policies.
Sequence of events
Starting the container again yourself
If you run docker start web for a manually stopped container, the policy applies again. The manual-stop flag is cleared, and a later crash or daemon restart will bring the container back as the policy says.
Check your understanding
Exercise
You run docker run -d --name api --restart always nginx:alpine, then docker stop api, then restart the Docker daemon. Is api running afterward? Would the answer change with --restart unless-stopped?
Show answer
With always, api is running again, because the daemon starts every always container when it comes back, even one you stopped by hand. With unless-stopped, api stays stopped, because Docker remembers the manual stop.
Exercise
A container uses --restart on-failure:3. It exited with code 0 and the daemon then restarts. Does the container start again?
Show answer
No. The on-failure policy only restarts containers that exit with a non-zero code. A clean exit with code 0 leaves the container stopped.