如果将应用程序引导到entrypoint.sh中,该应用程序是否将以PID 1运行并收到信号? [英] Will the application run as PID 1 and will signals be received if we bootstrap it in a entrypoint.sh?

查看:565
本文介绍了如果将应用程序引导到entrypoint.sh中,该应用程序是否将以PID 1运行并收到信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来启动服务是一个很好的习惯

CMD ["/go/bin/myapp"]

代替

CMD /go/bin/myapp

以第一种方式,SIGTERM被应用程序捕获,而在第二种方式中,底层的/bin/sh脚本捕获了SIGTERM,该脚本不将它们转发给服务.我从 https:中学到了这一点: //forums.docker.com/t/docker-run-can-be-killed-with-ctrl-c/13108/2 .

现在我想知道CMD ["..."]是否完全等同于将...放入/entrypoint.sh脚本并调用CMD ["/entrypoint.sh"]?现在,子应用程序也将以PID 1的身份运行并因此接收所有信号吗?

作为一个具体的例子,我想创建一个脚本:

envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf
&&
nginx -g 'daemon off;'

然后在我的Dockerfile末尾致电CMD ["/entrypoint.sh"].这样安全吗,尽管脚本中有多个命令(envsubstngingx),nginx仍将以PID 1的形式运行吗?

这件事使我感到困惑,我试图弄清楚何时以及何时不使用tini( https: //github.com/krallin/tini ).

解决方案

在这里,我尝试遵循MySQL 5.7

docker-entrypoint.sh:

#!/bin/bash
set -e

echo "preparing..."

exec "$@"

想法是在docker-entrypoint.sh脚本中做任何准备工作,然后使用exec "$@"将信号传递到CMD. (这意味着您必须在docker-entrypoint.sh内部使用envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf)


相关示例与Dockerfile文档的链接: 如果您需要为单个可执行文件编写启动脚本,则可以使用execgosu命令来确保最终的可执行文件接收Unix信号... >

It's good practice to start the service with

CMD ["/go/bin/myapp"]

instead of

CMD /go/bin/myapp

In the first way, SIGTERMs are caught by the applicaiton, while in the second way, they are caught by the underlying /bin/sh script, which does not forward them to the service. I learned this from https://forums.docker.com/t/docker-run-cannot-be-killed-with-ctrl-c/13108/2.

Now I am wondering if CMD ["..."] is completely equivalent to putting ... in an /entrypoint.sh script and calling CMD ["/entrypoint.sh"]? Will now child applications also be running as PID 1 and therefore receive all signals?

As a concrete example, I want to create a script with:

envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf
&&
nginx -g 'daemon off;'

And call CMD ["/entrypoint.sh"] at the end of my Dockerfile. Is this safe and will nginx run as PID 1 despite that there are multiple commands (envsubst and ngingx) in the script?

This matter confuses me and I am trying to figure out when and when not to use tini (https://github.com/krallin/tini).

解决方案

Here is my try to follow what also the MySQL 5.7 Dockerfile does... but with an Nginx image.

Dockerfile:

FROM nginx:latest
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod 777 /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"] 
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-entrypoint.sh:

#!/bin/bash
set -e

echo "preparing..."

exec "$@"

The idea is to do anything needed as a preparation inside the docker-entrypoint.sh script and with exec "$@" you then pass the signals to the CMD. (This means that you will have to use envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf inside docker-entrypoint.sh)


Link of relevant example to Dockerfile docs: If you need to write a starter script for a single executable, you can ensure that the final executable receives the Unix signals by using exec and gosu commands...

这篇关于如果将应用程序引导到entrypoint.sh中,该应用程序是否将以PID 1运行并收到信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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