bash 使 docker 容器永远运行,同时能够优雅地停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40608507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:24:11  来源:igfitidea点击:

Make docker container run forever while being able to gracefully stop

bashdockerdockerfile

提问by VarunAgw

I am trying to create a docker container. It is supposed to run forever in background. But whenever I press Ctrl+C or docker stop, it should close without wasting any further time.

我正在尝试创建一个 docker 容器。它应该在后台永远运行。但是每当我按 Ctrl+C 或 时docker stop,它应该关闭而不会浪费更多时间。

My current code

我当前的代码

#RUN trap 'echo $excode' EXIT HUP INT QUIT PIPE TERM
ENTRYPOINT while :; do read; done
#ENTRYPOINT trap "exit 1" SIGTERM SIGHUP && tail -f /dev/null & wait && exit 0

I can make it run forever but for some reason my script is not receiving TRAP events. Any clue how to fix that.

我可以让它永远运行,但由于某种原因,我的脚本没有接收到 TRAP 事件。任何线索如何解决这个问题。

回答by VonC

You could consider using (with docker 1.9+) STOPSIGNALin your Dockerfile.

您可以考虑STOPSIGNAL在 Dockerfile 中使用(使用 docker 1.9+)。

The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit.
This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9, or a signal name in the format SIGNAME, for instance SIGKILL.

STOPSIGNAL 指令设置将发送到容器退出的系统调用信号。
该信号可以是与内核系统调用表中的位置匹配的有效无符号数,例如 9,或格式为 SIGNAME 的信号名称,例如 SIGKILL。

But for a script managing such a signal, see "Trapping signals in Docker containers" and its program.shto orchestrate other non-PID1 processes.

但是对于管理此类信号的脚本,请参阅“在 Docker 容器中捕获信号”及其program.sh编排其他非 PID1 进程。

回答by Rambler

Start your container with the flags : -t& -i:

使用标志 : -t&启动您的容器-i

docker run -it --name=<container_name> <image>:<tag>

This will enable you to exit using ctrl+c

这将使您能够退出使用 ctrl+c

See this docker issuefor more info.

有关更多信息,请参阅此docker 问题