Sub topic 6
unless-stopped: always, except after a manual stop
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes (0 is success, non-zero is failure). It builds on the always policy from the previous chapter.
What unless-stopped does
unless-stopped behaves like always with one exception. Docker restarts the container whatever its exit code, and it also starts the container again when the Docker daemon starts. If you stopped the container yourself, Docker remembers that and leaves it stopped, even across a daemon restart.
docker run -d --name web --restart unless-stopped nginx:alpineIf the main process of web crashes or exits with code 0, Docker starts it again. If the host reboots, web comes back up once the daemon is running.
The difference from always
Both policies restart a container after a crash. They differ after a manual stop followed by a daemon restart.
| Situation | always | unless-stopped |
|---|---|---|
| Process exits with any code | Restarts | Restarts |
| You run `docker stop` | Stays stopped for now | Stays stopped |
| Daemon restarts after `docker stop` | Container starts again | Container stays stopped |
| Daemon restarts, container was running | Container starts again | Container starts again |
With always, a container you stopped on purpose can reappear after a reboot. With unless-stopped, your decision to stop it survives the reboot. This is why unless-stopped is often the better choice for long-running services on a single host.
Seeing it in practice
Start a container, stop it, and check its state:
docker run -d --name web --restart unless-stopped nginx:alpine
docker stop web
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.State.Status}}' webThe output is unless-stopped exited. The policy is still set, but Docker will not restart the container because you stopped it.
Start it again with docker start web. The "manually stopped" mark is cleared, and the policy applies again.
Changing the policy
You can switch an existing container to unless-stopped without recreating it:
docker update --restart unless-stopped webTo turn automatic restarts off, set the policy to no:
docker update --restart no webIn Docker Compose
Compose uses the service-level restart key:
services:
web:
image: nginx:alpine
restart: unless-stoppedRunning docker compose stop counts as a manual stop, so the services stay stopped after a daemon restart until you run docker compose up or docker compose start.
Common mistakes
- Expecting a restart after
docker stop. Docker will not restart a container you stopped, and that is the purpose of this policy. - Assuming it limits retries. Unlike
on-failure:N,unless-stoppedhas no maximum. A container that fails at startup will be retried again and again, with a growing delay between attempts. - Assuming it replaces a health check. The policy reacts to the process exiting. A running but broken process is not restarted.
Exercise
A container runs with --restart unless-stopped. You run docker stop on it, then the host reboots and the Docker daemon starts. Is the container running afterwards? What would happen with --restart always?
Show answer
With unless-stopped, the container stays stopped, because you stopped it manually before the daemon restarted. With always, Docker starts the container again when the daemon starts.
Exercise
Which command changes an existing container named web to use unless-stopped without recreating it?
Show answer
docker update --restart unless-stopped web