从内部停止Docker容器

编程入门 行业动态 更新时间:2024-10-24 14:24:29
本文介绍了从内部停止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 / 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: docs.docker/engine/reference/run/#specify-an-init-process

更多推荐

从内部停止Docker容器

本文发布于:2023-10-07 12:57:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!