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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With