问题描述
我现在在很多服务器上使用码头服务器,但有时候由于重负载,我使用了一些容器。我正在考虑添加一个cron,检查容器运行的每一分钟,但是我没有找到任何令人满意的方法。
我开始该容器具有保存正在运行的容器的id的cidfile。如果容器崩溃,cidfile保持在内部的id,我只是想知道你们如何确保容器运行或不运行,并重新启动它,以防万一它下降。我应该解析 docker ps -a
的输出,还是有更优雅的解决方案?
答案有些埋葬深度,但我发现了以最优雅为由的多种方式:
-
命名您的容器运行它,以便您可以附加到它的进程记录,并与过程监视器,如upstart / systemd / supervisord
docker run -itd -name = test ubuntu
upstart示例(
/ etc / init / test) conf
):
描述我的测试容器
/ p>
在文件系统上启动并启动docker
停在运行级别[!2345]
respawn
script
/ usr / bin / docker start -a test
end script
-
不太优雅:注意更改cidfile内容
docker run - itd --name = test --cidfile = / tmp / cidfile_path ubuntu
urly cron可能...
#!/ bin / bash
RUNNING = $(docker ps -a --no-trunc | awk'/ test /&& / Up /'| awk'{print $ 1}')
CIDFILE = $(cat / tmp / cidfile_path)
如果[$ RUNNING!=$ CIDFILE]
然后
#做某事聪明
fi
-
与上述类似,你可以看到如果给定的容器正在运行...在循环/ cron /无论什么
#!/ bin / bash
RUNNING = $(docker inspect --format'{{.State.Running}}'test)
如果[$ RUNNING== false]
然后
#一些聪明的
fi
你可以结合命令来做任何你喜欢的检查脚本,我去了 upstart
,因为它适合我的情况,但是如果你需要更多的控制,这些例子可以用于所有可能的情况。 p>
I'm using docker on quite a lot of servers right now but sometimes some of the containers I use crash due to heavy load. I was thinking on adding a cron that checks every minute of the container is running or not but I didn't find any satisfactory method on doing that.
I'm starting the container with a cidfile that saves the id of the running container. If the container crashes the cidfile stays there with the id inside and I was just wondering how do you guys make sure a container is running or not and respawn it in case it went down. Should I just parse the output of docker ps -a
or is there more elegant solution?
The answer is somewhat buried levels deep but I found out multiple ways of doing it starting with the most elegant:
Name your container when running it so you can attach to it's process logging and couple that with a process monitor such as upstart/systemd/supervisord
docker run -itd --name=test ubuntu
upstart example (
/etc/init/test.conf
):description "My test container"start on filesystem and started dockerstop on runlevel [!2345]respawnscript /usr/bin/docker start -a testend script
Less elegant: watch for changes in cidfile contents
docker run -itd --name=test --cidfile=/tmp/cidfile_path ubuntu
An hourly cron maybe...
#!/bin/bash RUNNING=$(docker ps -a --no-trunc | awk '/test/ && /Up/' | awk '{print $1}') CIDFILE=$(cat /tmp/cidfile_path) if [ "$RUNNING" != "$CIDFILE" ] then # do something wise fi
Similar to the above you can see if a given container is running...in a loop/cron/whatever
#!/bin/bash RUNNING=$(docker inspect --format '{{.State.Running}}' test) if [ "$RUNNING" == false ] then # do something wise fi
You can combine commands to do whatever checking script you like, I went with upstart
because it suits my situation but these examples could be used for all possible scenarios should you need more control.
这篇关于确保给定的docker容器正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!