为什么 CTRL-C 不再停止我的 nginx 容器?

Joh*_*men 3 nginx docker dockerfile

我正在试验一个基于 nginx 的 Dockerfile。最后一行目前看起来像这样:

FROM nginx:alpine
... # not really relevant
CMD /bin/sh -c "envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
Run Code Online (Sandbox Code Playgroud)

现在,当我使用 运行容器时docker run my-nginx-image,我注意到它CTRL-C不再停止容器。

在进行更改之前,我最后有以下CMD声明:

CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)

在这里,CTRL-C按预期工作:容器已停止。这是为什么?我怎么能同时拥有两个世界?

  • CTRL-C 在职的
  • envsubst 包括

更新

经过一些阅读,我意识到我必须使用CMD [...]. 但我未能将整个命令envsubst < ... > ... && nginx -g 'daemon off;'整合到[...]语法中。

Moi*_*sei 5

https://forums.docker.com/t/docker-run-cannot-be-killed-with-ctrl-c/13108/2

所以这里有两个因素在起作用:

如果您为入口点指定一个字符串,如下所示:

入口点 /go/bin/myapp

Docker 使用 /bin/sh -c 'command' 运行脚本。此中间脚本获取 SIGTERM,但不会将其发送到正在运行的服务器应用程序。

为避免中间层,请将您的入口点指定为字符串数组。

入口点 ["/go/bin/myapp"]