Search code examples
dockerdocker-compose

docker-compose restart policy - disable exponential delay between restarts


I've a docker-compose file configuring a service with the restart policy set to always

The command is python3 script.py

And script.py just prints the current timestamp :

import time

print(time.time())

Using docker-compose up I get this :

random_service    | 1546974860.1233172
random_service    | 1546974861.9269428
random_service    | 1546974863.616101
random_service    | 1546974865.4225447
random_service    | 1546974867.2077854
random_service    | 1546974869.4796813
random_service    | 1546974873.4290836
random_service    | 1546974880.5541086
random_service    | 1546974894.0697372
random_service    | 1546974920.4050376

As you can see, it looks like the more docker tries to restart the service, the more it waits between restarts. At the beginning it tries every one or two seconds, then four, seven, fourteen, twenty-six...

How can I disable that ? I want my service to be restarted as soon as possible, every time it stops.


Solution

  • You have options to customize your restart policy on the docker-compose level - https://docs.docker.com/engine/reference/commandline/service_create/:

    --restart-condition Restart when condition is met (“none”|”on-failure”|”any”) (default “any”)

    --restart-delay Delay between restart attempts (ns|us|ms|s|m|h) (default 5s)

    --restart-max-attempts Maximum number of restarts before giving up

    --restart-window Window used to evaluate the restart policy (ns|us|ms|s|m|h)

    Mentioned restart behaviour is documented in https://docs.docker.com/engine/reference/run/#restart-policies---restart:

    An ever increasing delay (double the previous delay, starting at 100 milliseconds) is added before each restart to prevent flooding the server. This means the daemon will wait for 100 ms, then 200 ms, 400, 800, 1600, and so on until either the on-failure limit is hit, or when you docker stop or docker rm -f the container.

    If a container is successfully restarted (the container is started and runs for at least 10 seconds), the delay is reset to its default value of 100 ms.

    You have still option to use another process manager in the container or in the host OS, which may fit your needs (upstart, systemd, supervisor, monit, ...). Some recommendations: https://docs.docker.com/config/containers/start-containers-automatically/#use-a-process-manager


    2024 links: