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 handles process crashes on a single host - it is not a substitute for an orchestrator like Kubernetes, which manages node failures, rolling updates, and cross-host load balancing.