从内部停止Docker容器 [英] Stopping docker container from inside

查看:136
本文介绍了从内部停止Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在docker容器中运行了一个cronjob,该容器检查所有服务是否都按预期运行。如果此cronjob确定存在问题,我想停止docker容器(从内部...)



不幸的是退出只是停止我的cronjob脚本

解决方案

基本上,您需要PID 1退出才能停止容器。



我最初以为 kill -s SIGKILL 1 可以工作,但PID 1受保护,所以没有。



如@Thomasleveil所建议,您可以将诸如 trap exit SIGINT SIGTERM 之类的代码添加到PID 1脚本中,这意味着当发送 kill -s SIGINT 1 时,该过程将退出。我稍微喜欢这种方法,而不是您想出的方法(直接杀死子进程),因为它为父进程提供了清理的机会,并且父进程也应该能够找到子进程的PID而无需awk。 / p>

处理此问题的最简单方法是使用某种管理程序,而Docker现在方便地提供了一个名为 tini的程序。只需运行,将参数-init 添加到 docker run ,Docker就会在容器中将tini设置为PID 1。完整描述在这里: https://docs.docker .com / engine / reference / run /#specify-an-init-process


I have a cronjob running inside a docker container that checks whether all services are running as expected. If this cronjob determines that there is a problem I would like to stop the docker container (from inside...)

Unfortunately exit just stops my cronjob script

解决方案

Basically, you need PID 1 to exit to stop the container.

I initially thought kill -s SIGKILL 1 would work, but PID 1 is protected, so it doesn't.

As suggested by @Thomasleveil, you could add code such as trap "exit" SIGINT SIGTERM to the PID 1 script, which will mean the process will exit when sent a kill -s SIGINT 1. I slightly prefer this method to the one you came up with (killing the child process directly) as it gives the parent process a chance to clean up and also the parent process should be able to find the PID of the child process without awk.

The easiest way to handle this is with some sort of supervisor process, and handily Docker now provide one called "tini". Just run add the argument --init to docker run and Docker will set up tini as PID 1 in the container. A full description is here: https://docs.docker.com/engine/reference/run/#specify-an-init-process

这篇关于从内部停止Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆