Skip to main content
Docker restart policies let you control if and when a container is automatically restarted. Whether you’re running a critical web service or a batch job, these policies ensure your containers recover from failures or daemon restarts.

Why Containers Stop

A container can exit for several reasons:
  1. Normal completion
    The primary process finishes successfully (exit code 0), for example, a script completes its task.
  2. Failure
    The process crashes or throws an error, exiting with a non-zero code (e.g., bad input).
  3. Manual intervention
    Running docker container stop sends a SIGTERM, then a SIGKILL after a timeout:
    • If the process traps SIGTERM and exits cleanly, it may return code 0.
    • If it’s killed with SIGKILL, it usually exits with a non-zero code.
When you need a container—say, a production API or CI runner—to restart immediately after a crash, Docker’s restart policies come into play.

Docker Restart Policies

Specify a restart policy with --restart when you run a container:
Use on-failure[:<max-retries>] to prevent infinite restart loops. Docker immediately retries with no backoff delay.

Quick Reference

  • no: No auto-restart.
  • on-failure: Auto-restart only on errors (non-zero exit).
  • always: Auto-restart on any exit.
  • unless-stopped: Auto-restart on any exit, but not after a manual stop.

Examples

1. Default (no)

2. On Failure

3. Always

  • After sleep 5 finishes (exit 0), Docker restarts immediately.
  • docker stop test-always prevents restart only until the next Docker daemon reboot.

4. Unless-Stopped

  • Behaves like always on crashes or normal exit.
  • Honors manual docker stop even if the daemon restarts later.

Live Restore of Containers

By default, stopping the Docker daemon halts all containers. With live restore, containers remain running when the daemon is down.
  1. Edit or create /etc/docker/daemon.json:
  2. Restart the Docker service:

Verifying Live Restore

Without live restore:
With live restore enabled:
Live restore requires compatible Docker versions and proper permissions. Check the Docker daemon.json reference before enabling.

Watch Video