# Restart Policies — Docker Compose

Source: https://www.skillbyai.com/en/docker-compose/h-restart

> Recover from crashes automatically.

## no, on-failure, always, unless-stopped

The `restart` policy controls what Docker does when a container exits: `no` (default), `on-failure` (restart after a non-zero exit, optionally with a retry limit), `always` (always restart, including after the Docker daemon restarts) and `unless-stopped` (like always, but not if you stopped it deliberately). For long-running services on a server, `unless-stopped` is a common choice; for one-off jobs, leave it as `no`.

## Choosing a restart policy

Match the policy to the workload.

```text
policy           restarts after crash   after daemon restart   typical use
no               no                     no                     one-off tasks, migrations
on-failure[:N]   yes (non-zero exit)    yes                    workers that may crash
always           yes                    yes                    critical services
unless-stopped   yes                    yes, unless stopped    services on a single server
```

## Watch for crash loops

A service restarting every few seconds hides a real bug; check docker compose ps and logs for repeated restarts.

**Quiz:** Which policy suits a database migration job?

- [ ] always
- [x] no (run once and exit)
- [ ] unless-stopped
- [ ] on-failure with infinite retries for a job that succeeds once

*Answer:* no (run once and exit). Jobs should finish, not restart forever.
