Sub topic 10
Restart delay and backoff
This chapter assumes you can run containers with docker run. It also assumes you know the four restart policies and that a container stops when its main process exits. Here you learn how long Docker waits between restarts and why.
Why Docker waits between restarts
A container that fails on startup, for example because of a bad configuration, would exit within milliseconds. With always or on-failure, Docker would restart it at once, and it would fail again. That loop would burn CPU and fill the logs.
To prevent this, Docker inserts a delay before each restart and makes the delay longer each time.
The backoff rule
Docker's documented behavior has three parts:
- The first delay is 100 milliseconds.
- The delay doubles after each restart.
- The delay is capped at 1 minute.
The delay then grows like this:
| Restart number | Delay before restart |
|---|---|
| 1 | 100 ms |
| 2 | 200 ms |
| 3 | 400 ms |
| 4 | 800 ms |
| 5 | 1.6 s |
| 6 | 3.2 s |
| 10 | 51.2 s |
| 11 and later | 60 s (cap) |
If the container stays up for at least 10 seconds, Docker resets the delay to 100 ms. A container that runs for a while and then crashes is treated as a fresh failure. A container that fails immediately, over and over, is slowed down.
Watching the backoff
Start a container that always fails, with on-failure limited to three retries:
docker run -d --name flaky --restart on-failure:3 alpine sh -c 'exit 1'Docker restarts it after about 100 ms, 200 ms and 400 ms, then gives up. Check the result:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flakyThe output is on-failure 3 exited. The count of 3 shows that the retry limit stopped the restarts.
Now try a container with no retry limit:
docker run -d --name looper --restart always alpine sh -c 'exit 1'
docker inspect -f '{{.RestartCount}} {{.State.Status}}' looperRun the inspect command several times over a minute. The count rises quickly at first, then more slowly as the delay approaches 60 seconds. The status often reads restarting because Docker is waiting out the delay. Clean up afterwards:
docker rm -f flaky looperGotchas
- You cannot configure the delay. Docker has no flag for the initial delay, the multiplier or the cap. If you need different timing, handle it in the container's entrypoint (for example, a script that sleeps before retrying) or use an orchestrator.
- The 10-second reset is based on uptime. A service that crashes 15 seconds after each start restarts after about 100 ms every time. It never reaches the long delays, so a crash loop can still look busy.
- A container in the
restartingstate is not running.docker execfails against it, anddocker logsshows only output from earlier attempts.
Exercise
A container with --restart always fails immediately on every start. Roughly how long after the container's fifth restart does Docker wait before the sixth attempt, and what is the longest delay it will ever use?
Show answer
The delays double from 100 ms: 100 ms, 200 ms, 400 ms, 800 ms, 1.6 s for restarts 1 to 5. The wait before the sixth restart is 3.2 seconds. The longest delay Docker ever uses is 1 minute. If the container runs for at least 10 seconds before exiting, the delay resets to 100 ms.