使用 -it 选项时 Docker 容器退出

2023-12-19

考虑以下 Dockerfile:

FROM ubuntu:16.04

RUN apt-get update && \
    apt-get install -y apache2 && \
    apt-get clean

ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]

使用命令运行容器时docker run -p 8080:80 <image-id>,然后容器启动并保持运行,允许访问默认的 Apache 网页https://localhost:8080正如预期的那样来自主机。但是,使用此运行命令,我无法使用以下命令退出容器Ctrl+C,也正如预期的那样,因为容器不是随-it选项。现在,如果-it在运行命令中添加选项,则容器启动后立即退出。这是为什么?有没有一种优雅的方法可以让 apache 在退出时在前台运行Ctrl+C?


此行为是由 Apache 引起的,不是 Docker 的问题。 Apache 被设计为在收到以下请求时正常关闭SIGWINCH信号。当以交互方式运行容器时,SIGWINCH信号从主机传递到容器,有效地向 Apache 发出信号以正常关闭。在某些主机上,容器可能会在启动后立即退出。在其他主机上,容器可能会保持运行状态,直到调整终端窗口大小。

容器退出后,可以通过查看 Apache 日志文件来确认这是问题的根源,如下所示:

# Run container interactively:
docker run -it <image-id>

# Get the ID of the container after it exits:
docker ps -a

# Copy the Apache log file from the container to the host:
docker cp <container-id>:/var/log/apache2/error.log .

# Use any text editor to review the log file:
vim error.log

# The last line in the log file should contain the following:
AH00492: caught SIGWINCH, shutting down gracefully

Sources:

  • https://bz.apache.org/bugzilla/show_bug.cgi?id=50669 https://bz.apache.org/bugzilla/show_bug.cgi?id=50669
  • https://bugzilla.redhat.com/show_bug.cgi?id=1212224 https://bugzilla.redhat.com/show_bug.cgi?id=1212224
  • https://github.com/docker-library/httpd/issues/9 https://github.com/docker-library/httpd/issues/9
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 -it 选项时 Docker 容器退出 的相关文章

随机推荐