Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 946
Loïc Avatar asked Jan 08 '19 19:01

Loïc


People also ask

What is the restart policy in Docker compose?

A restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop.

How do I restart docker without stopping containers?

Restart the Docker daemon. On Linux, you can avoid a restart (and avoid any downtime for your containers) by reloading the Docker daemon. If you use systemd , then use the command systemctl reload docker . Otherwise, send a SIGHUP signal to the dockerd process.

Does Docker compose automatically restart?

no: Containers won't restart automatically. on-failure[:max-retries]: Restart the container if it exits with a non-zero exit code, and provide a maximum number of attempts for the Docker daemon to restart the container. always: Always restart the container if it stops.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


1 Answers

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

like image 109
Jan Garaj Avatar answered Nov 06 '22 07:11

Jan Garaj