Docker restart policies
Topic: Containers core
Summary
Set container restart policy with --restart (no, on-failure, always, unless-stopped). Use always or unless-stopped for long-running services so they come back after reboot or crash. Use this when you want containers to restart automatically.
Intent: How-to
Quick answer
- docker run --restart=always image. Options: no (default), on-failure[:max], always, unless-stopped. always and unless-stopped start on daemon start.
- unless-stopped: restart except if user stopped the container. on-failure: only restart on non-zero exit; optional max count.
- In compose: restart: always (or on-failure, unless-stopped). Ensures services survive host reboot when Docker starts.
Prerequisites
Steps
-
Choose policy
Use always or unless-stopped for services. Use on-failure for batch jobs with retry limit.
-
Run with policy
docker run -d --restart=unless-stopped myimage. Or in compose: restart: unless-stopped.
-
Verify
Kill container or reboot; confirm container restarts after Docker starts.
Summary
Set —restart=always or unless-stopped for services so they restart on failure or host reboot.
Prerequisites
Steps
Step 1: Choose policy
Pick always, unless-stopped, or on-failure with optional max.
Step 2: Run with policy
Use —restart in docker run or restart in compose.
Step 3: Verify
Kill or reboot; confirm restart behavior.
Verification
- Container restarts as expected after crash or reboot.
Troubleshooting
No restart — Check policy and Docker daemon running. Restart loop — Fix container entrypoint or fix cause of exit.