Q: What are the different Docker Restart Policies?
Answer:
Restart policies control whether a container is automatically restarted when it exits or when the Docker daemon restarts.
Available Policies
docker run --restart <policy> myimage
| Policy | Behavior |
|---|---|
no | Never restart the container (default). |
on-failure[:max-retries] | Restart only if the container exits with a non-zero exit code. Optionally limit the number of retries. |
always | Always restart the container, regardless of exit code. Also restarts when the Docker daemon starts. |
unless-stopped | Same as always, but does not restart if the container was manually stopped before the daemon restart. |
Examples
# Restart up to 5 times on failure
docker run --restart on-failure:5 myapp
# Always keep the container running (survives daemon restarts)
docker run --restart always nginx
# Same as always, but respects manual stops
docker run --restart unless-stopped nginx
always vs unless-stopped
The subtle but critical difference:
- You run a container with
--restart always. - You manually
docker stopit. - The Docker daemon restarts (e.g., server reboot).
- Result: The container starts again (because the policy is
always).
With unless-stopped:
- You run a container with
--restart unless-stopped. - You manually
docker stopit. - The Docker daemon restarts.
- Result: The container stays stopped (it respects your manual stop).
[!TIP] For production services, prefer
unless-stopped. It auto-recovers from crashes while still respecting your intent when you explicitly stop a container for maintenance.
In Docker Compose
services:
web:
image: nginx
restart: unless-stopped