Immortality: Restart Policies & Healing

In a distributed system, failure is inevitable. Processes crash. Memory leaks happen. The network blips. A robust system isn’t one that never crashes; it’s one that recovers automatically.

1. The 4 Policies

Docker provides 4 restart policies to control container resurrection.

Policy Description Use Case
no Do not restart automatically. (Default) One-off scripts, debugging.
on-failure Restart only if the process exits with a non-zero exit code. Batch jobs that might fail transiently.
always Always restart unless explicitly removed. Even if you manually stop it, it restarts when the daemon restarts. Web servers, databases.
unless-stopped Like always, but if you manually docker stop it, it stays stopped even after a daemon reboot. Production services you might want to turn off for maintenance.

2. Exponential Backoff

If your app crashes immediately on startup (CrashLoopBackOff), Docker is smart. It won’t restart it in a tight loop (burning CPU). It waits: 100ms, 200ms, 400ms, 800ms… up to 1 minute.


3. Interactive: Resurrection Lab

Test how different policies react to different exit scenarios.

App
Running
> Container Started (Policy: no)

4. Code Example: Defining Policies

version: '3.8'
services:
  web-server:
    image: nginx
    restart: always  # Restart automatically
    ports:
      - "80:80"

  worker:
    image: my-worker
    restart: on-failure  # Only if it crashes
    command: ["./process-jobs"]
# Run with restart policy
docker run -d --restart unless-stopped redis

# Update existing container
docker update --restart always my-container