8

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.

2 Answers 2

6
+100

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:

Sign up to request clarification or add additional context in comments.

2 Comments

docker-compose reference states that This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run.
Unfortunately, the provided links are either dead, or the contents have been changed and the cited increasing delay paragraph is nowhere to be found. It seems to me like the newer docker compose spec / docker swarm got rid of this behaviour? Or is it completely undocumented?
-3

I have just figured out a trick which is to remove the restart policy, and run docker-compose with watch like this :

watch -n 0 docker-compose up

Now there is ~2.5 secs between each run.

So, it is not really satisfying, what happens if only one service shutsdown (?) but closer to what I want. A real solution is welcome.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.