Sub topic 15
Beyond a single host: Swarm and Compose deploy restart_policy
This chapter assumes you know the four docker run restart policies (no, on-failure, always, unless-stopped) and the service-level restart key in Compose. Here you will see how restart behavior is configured when containers run as Swarm services.
Two different keys
The restart key and --restart flag belong to a single Docker Engine. Swarm mode uses a different setting called restart_policy. In a Compose file it sits under deploy.
| Setting | Where it applies | Values |
|---|---|---|
| `restart` / `--restart` | One container on one engine | `no`, `on-failure[:N]`, `always`, `unless-stopped` |
| `deploy.restart_policy` | Swarm services (`docker stack deploy`) | `condition`: `none`, `on-failure`, `any` |
Swarm has no unless-stopped. You stop a service by scaling it to zero or removing it.
Tasks are replaced, not restarted
Swarm does not restart the same container. Each replica is a task. When a task exits and the policy allows it, the scheduler creates a new task, possibly on another node. The old container stays in a stopped state until Swarm cleans it up.
The restart_policy fields
condition:nonenever replaces a task.on-failurereplaces it after a non-zero exit.anyreplaces it after any exit. The default isany.delay: how long to wait before replacing the task. The default is 5 seconds.max_attempts: how many replacements to try. The default is unlimited.window: how long to wait before deciding whether a restart worked. By default Swarm decides immediately. With a window, only attempts inside that period count towardmax_attempts.
A stack file
services:
web:
image: nginx:alpine
deploy:
replicas: 2
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 60sDeploy it with docker stack deploy -c compose.yaml demo. After web fails three times within the window, Swarm stops replacing that replica.
The same settings exist as flags on docker service create:
docker service create --name web --replicas 2 \
--restart-condition on-failure \
--restart-delay 10s \
--restart-max-attempts 3 \
--restart-window 60s \
nginx:alpineWhich key wins
docker stack deployignores the service-levelrestartkey. Usedeploy.restart_policy.docker compose upon a single engine usesrestart. Recent Compose v2 releases also mapdeploy.restart_policy.condition(andmax_attempts) onto the container's restart policy whenrestartis not set. They ignoredelayandwindow. Confirm this on your version withdocker inspect.- If you set both keys in a file used for both purposes, each tool reads the one it understands.
Inspecting service restarts
Use docker service ps to see the history of tasks, including the failed ones:
docker service ps webEach replacement appears as a new row, and the failed task keeps its error message. docker inspect -f '{{.RestartCount}}' on a task's container does not give a service-wide count, because every replacement is a new container.
Exercise
A stack file sets restart: unless-stopped on a service and has no deploy section. You run docker stack deploy. What restart behavior do you get, and how would you ask for "restart only after failures, at most 5 times, wait 15 seconds between tries"?
Show answer
docker stack deploy ignores the restart key, so the service uses the Swarm default of condition any with a 5 second delay and unlimited attempts. Add deploy.restart_policy with condition: on-failure, delay: 15s and max_attempts: 5.