Script Valley
Docker: Complete Course
Docker in ProductionLesson 5.5

Container restart policies and production uptime strategies

restart policy, always, unless-stopped, on-failure, no policy, exit codes, health-based restart, Compose restart, production availability

Keeping Containers Running Through Failures

Docker restart policy decision flow

Docker can automatically restart containers when they crash. Without a restart policy, a container that crashes stays down โ€” a single unhandled exception kills your production service.

The Four Restart Policies

# Never restart (default)
docker run my-app

# Restart unless manually stopped
docker run --restart unless-stopped my-app

# Always restart, even after docker daemon restart
docker run --restart always my-app

# Restart only on non-zero exit code, max 5 attempts
docker run --restart on-failure:5 my-app

Choosing the Right Policy

unless-stopped is the recommended policy for long-running services. It restarts on crash and survives host reboots, but respects manual docker stop. always restarts even after a manual stop โ€” only use for system-critical containers. on-failure is good for one-off jobs that should retry on error but stop after a fixed number of attempts.

In Compose

services:
  api:
    restart: unless-stopped
  db:
    restart: unless-stopped

A restart policy is not a substitute for a proper orchestrator like Kubernetes in production. It handles process crashes but not node failures, rolling updates, or load balancing across multiple hosts.

Container restart policies and production uptime strategies โ€” Docker in Production โ€” Docker: Complete Course โ€” Script Valley โ€” Script Valley