问题描述
启动服务是个好习惯
CMD ["/go/bin/myapp"]
而不是
CMD /go/bin/myapp
在第一种方式中,SIGTERM 被应用程序捕获,而在第二种方式中,它们被底层的 /bin/sh
脚本捕获,该脚本不会将它们转发给服务.我从 https 了解到这一点://forums.docker.com/t/docker-run-cannot-be-killed-with-ctrl-c/13108/2.
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.
现在我想知道 CMD ["..."]
是否完全等同于将 ...
放入 /entrypoint.sh
脚本并调用 CMD ["/entrypoint.sh"]
?现在子应用程序是否也会作为 PID 1 运行并因此接收所有信号?
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;'
并在我的 Dockerfile
末尾调用 CMD ["/entrypoint.sh"]
.这安全吗?尽管脚本中有多个命令(envsubst
和 ngingx
),nginx 仍会以 PID 1 运行吗?
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?
这件事让我很困惑,我正试图弄清楚何时以及何时不使用 tini
(https://github.com/krallin/tini).
This matter confuses me and I am trying to figure out when and when not to use tini
(https://github.com/krallin/tini).
推荐答案
这是我尝试遵循 MySQL 5.7 Dockerfile
确实...但带有 Nginx 映像.
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 "$@"
我们的想法是在 docker-entrypoint.sh
脚本中做任何准备工作,然后使用 exec "$@"
将信号传递给 .(这意味着你必须在 docker 中使用
)envsubst </etc/nginx/conf.d/site.template >/etc/nginx/conf.d/default.conf
-entrypoint.sh
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
)
相关示例到 Dockerfile
文档的链接:如果您需要为单个可执行文件编写启动脚本,您可以使用 exec
和 gosu
命令...
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 运行,并且会收到信号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!