gpt4 book ai didi

How to remove shell from docker image with healthcheck?(如何去除带有健康检查功能的坞站图像中的外壳?)

转载 作者:bug小助手 更新时间:2023-10-25 18:31:55 26 4
gpt4 key购买 nike



I have an NGINX docker image where I use healthcheck, like this:

我有一个使用Health Check的Nginx docker镜像,如下所示:


HEALTHCHECK --interval=3s --timeout=1s \
CMD nc -vz -w1 127.0.0.1 8080 || exit 1

For security reasons, I also want to remove the shell, but healthcheck stops working after that. I tried to completely delete the busybox (alpine-basilayout), separately only remove symbolyc link for /bin/sh, replace shell with tiny etc.

出于安全原因,我也想删除外壳,但在那之后Health Check停止工作。我尝试完全删除Busybox(alpine-basilayout),单独删除/bin/sh的symbolyc链接,将外壳替换为mini等。


How to do this correctly so as not to disrupt the application in the container and save healthcheck?

如何正确地做到这一点,以便不中断容器中的应用程序并保存健康检查?


更多回答

Please always include relevant code and configuration in your question; don't rely on links to external sites.

请务必在您的问题中包含相关代码和配置;不要依赖外部站点的链接。

优秀答案推荐

The CMD directive has two forms. When you write this:

CMD指令有两种形式。当你写下这篇文章时:


CMD nc -vz -w1 127.0.0.1 8080 || exit 1

That gets translated to sh -c 'nc -vz -w1 127.0.0.1 8080 || exit 1. You can avoid that shell execution by using the "exec" form the CMD directive, like this:

它将被转换为sh-c‘nc-vz-w1 127.0.0.1 8080||exit 1。您可以通过使用CMD指令中的“exec”来避免该外壳执行,如下所示:


CMD ["nc", "-vz", "-w1", "127.0.0.1", "8080"]

That will execute nc directly, without involving the shell. Note that your existing healthcheck is explicitly using shell syntax (the || exit 1) part; that won't work if you're not using the shell.

这将直接执行NC,而不涉及外壳。请注意,您现有的Health Check显式使用了外壳语法(||exit 1)部分;如果不使用外壳,这将不起作用。




For example, a container built with this Dockerfile will never be healthy:

例如,使用此Dockerfile构建的容器将永远不会正常:


FROM alpine

RUN apk add darkhttpd curl
RUN rm -f /bin/sh

ENTRYPOINT ["darkhttpd"]
CMD ["/var/www/localhost/htdocs", "--port", "8080"]

HEALTHCHECK --interval=3s --timeout=1s \
CMD nc -vz -w1 127.0.0.1 8080

But this one works as desired:

但这一次的效果和预期的一样:


FROM alpine

RUN apk add darkhttpd curl
RUN rm -f /bin/sh

ENTRYPOINT ["darkhttpd"]
CMD ["/var/www/localhost/htdocs", "--port", "8080"]

HEALTHCHECK --interval=3s --timeout=1s \
CMD ["nc", "-vz", "-w1", "127.0.0.1", "8080"]

更多回答

Okay, thanks for the answer. But I already tried it before with /usr/bin/nc instead just nc. I can try it again... Can I remove /bin/sh after that?

好的,谢谢你的回答。但我以前已经用/usr/bin/nc尝试过了,而不是只用nc。我可以再试一次。在那之后我可以删除/bin/sh吗?

That is what I am suggesting with this answer, yes.

这就是我对这个答案的建议,是的。

It works! Thank you so much!👏🏼

它起作用了!非常感谢!👏🏼

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com