gpt4 book ai didi

docker-compose 重启策略 - 禁用重启之间的指数延迟

转载 作者:IT老高 更新时间:2023-10-28 21:26:03 31 4
gpt4 key购买 nike

我有一个配置服务的 docker-compose 文件,其中 restart 策略设置为 always

命令python3 script.py

script.py 只是打印当前时间戳:

import time

print(time.time())

使用 docker-compose up 我明白了:

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

如您所见,看起来 docker 尝试重新启动服务的次数越多,它在重新启动之间等待的时间就越长。一开始它每隔一两秒尝试一次,然后是四、七、十四、二十六……

我怎样才能禁用它?我希望我的服务在每次停止时尽快重新启动。

最佳答案

您可以选择在 docker-compose 级别自定义重启策略 - 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)

提到的重启行为记录在 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.

您仍然可以选择在容器或主机操作系统中使用另一个进程管理器,这可能满足您的需求(upstart、systemd、supervisor、monit,...)。一些建议:https://docs.docker.com/config/containers/start-containers-automatically/#use-a-process-manager

关于docker-compose 重启策略 - 禁用重启之间的指数延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54098405/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com