require-restart-policy
Docker Compose services don't restart after a crash or reboot by default. Choosing between restart: always and unless-stopped.
On this page
The default restart policy is no: when a service crashes at 3 a.m. — or the host reboots after a power cut or kernel update — the container simply stays down until a human notices. This rule flags Compose services with no restart policy.
What the rule catches
services:
api:
image: acme/api:1.4.2Scanning this file reports:
⚠ WARN [docker-doctor/require-restart-policy]
Service 'api' has no restart policy configured. It will not restart if it crashes or if the host reboots.Why it matters
Crashes are normal operation: OOM kills, unhandled exceptions, dropped database connections, host reboots. A restart policy is the difference between a 10-second blip and an outage that lasts until someone checks. unless-stopped is the policy that matches operational intent — it restarts on any crash and after reboot, but stays down when you deliberately stopped the service (whereas always will resurrect it at the next daemon restart, surprising whoever stopped it for maintenance).
How to fix it
Define restart: always or restart: unless-stopped (or deploy.restart_policy) so services restart on crashes or host reboot. A one-shot job that another service waits on with condition: service_completed_successfully is exempt.
services:
api:
image: acme/api:1.4.2
restart: unless-stoppedon-failure[:max-retries] restarts only on non-zero exits, useful for one-shot jobs that should retry a bounded number of times. Restart policies handle a dead process; they can't see a process that's alive but broken — pair them with a HEALTHCHECK so unhealthy states are visible too. Docker restarts with exponential backoff, so a crash-looping service won't peg the host.
A service that another service names under depends_on with condition: service_completed_successfully is a one-shot job, such as a database migration or a bucket setup. The rule skips it, because restart: always would run the job again every time it finishes. Give such a job restart: on-failure if it should retry, or nothing. A service under profiles: is optional, not one-shot, so it is still checked.
A service with a provider: mapping is skipped. Compose hands it to a provider instead of running it as a container, so a restart policy has nothing to restart.
Compose runs a base file merged with its override. When a directory holds a base file (compose.yaml, compose.yml, docker-compose.yml or docker-compose.yaml) and an override (compose.override.yml, compose.override.yaml, docker-compose.override.yml or docker-compose.override.yaml), this rule judges each service after the override merges over the base, the way docker compose does without -f. A setting in either file counts. When the base sets one and the override takes it away with !reset or !override, the finding lands on the override at that tag, because only the override can give it back. Any other finding lands on the base at the service's line, because that is where you add the setting. A service that only the override defines is reported on the override. Files passed with -f, extends and include are not followed.
Rule details
- Rule key —
docker-doctor/require-restart-policy - Category — Compose
- Default severity —
warning - Applies to — Docker Compose files
Explain this rule from the CLI:
npx @docker-doctor/cli@latest rules explain docker-doctor/require-restart-policyChange its severity — or turn it off — in your config file:
// docker-doctor.config.ts
export default {
rules: {
"docker-doctor/require-restart-policy": "off",
},
};Severity affects the health score: error findings cost more points than warning, and info costs the least.