Restart policies

A restart policy tells Docker what to do when a container stops: nothing (no), restart it after a crash (on-failure), or keep it running through crashes and reboots (unless-stopped, always).

How it works

--restart unless-stopped restarts the container whenever it exits, and when Docker starts again after a reboot, unless you stopped it yourself. Restarts are spaced out, 100 ms, 200 ms, 400 ms and so on, and the delay resets once the container has run for 10 seconds. The default is no.

What breaks

on-failure is meant for crashes, not for reboots: to be sure a service comes back after the server restarts, use unless-stopped or always. A restart policy also hides a crash loop, so keep an eye on the restart count.

In a terminal

docker run -d --name api --restart unless-stopped -p 3000:3000 orders-api
docker update --restart unless-stopped api
docker inspect -f '{{.RestartCount}}' api

See it break, then fix it

Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.