使用-it选项时,Docker容器退出 [英] Docker container exits when using -it option

查看:251
本文介绍了使用-it选项时,Docker容器退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下Dockerfile:

 从ubuntu:16.04 

运行apt-get update& ;& \
apt-get install -y apache2&& b
易于获取干净的

ENTRYPOINT [ apache2ctl,-D, FOREGROUND]

使用命令 docker run -p 8080:80< image-id> 运行容器时,容器将启动并保持运行状态,从而允许主机从 https:// localhost:8080 上访问默认的Apache网页。但是,使用此运行命令,我也无法按预期使用 Ctrl + C 退出容器,因为容器未使用启动-it 选项。现在,如果将 -it 选项添加到run命令,则容器在启动后立即退出。这是为什么?在 Ctrl + C 上退出时,是否有一种优雅的方法让Apache在前台运行?

解决方案

此行为是由Apache引起的,与Docker无关。 Apache旨在在收到 SIGWINCH 信号时正常关闭。交互式运行容器时, SIGWINCH 信号从主机传递到容器,有效地指示Apache正常关闭。在某些主机上,容器可能在启动后立即退出。



在其他主机上,容器可能会一直运行。



可以通过查看以下内容确认容器退出后问题的根源Apache日志文件如下:

 #交互式运行容器:
docker run -it< image-id>

#退出后获取容器的ID:
docker ps -a

#将Apache日志文件从容器复制到主机:
docker cp< container-id>:/ var / log / apache2 / error.log。

#使用任何文本编辑器查看日志文件:
vim error.log

#日志文件的最后一行应包含以下内容:
AH00492:捕获了SIGWINCH,并正常关闭了

来源:




Consider the following Dockerfile:

FROM ubuntu:16.04

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

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

When running the container with the command docker run -p 8080:80 <image-id>, then the container starts and remains running, allowing the default Apache web page to be accessed on https://localhost:8080 from the host as expected. With this run command however, I am not able to quit the container using Ctrl+C, also as expected, since the container was not launched with the -it option. Now, if the -it option is added to the run command, then the container exits immediately after startup. Why is that? Is there an elegant way to have apache run in the foreground while exiting on Ctrl+C?

解决方案

This behaviour is caused by Apache and it is not an issue with Docker. Apache is designed to shut down gracefully when it receives the SIGWINCH signal. When running the container interactively, the SIGWINCH signal is passed from the host to the container, effectively signalling Apache to shut down gracefully. On some hosts the container may exit immediately after it is started. On other hosts the container may stay running until the terminal window is resized.

It is possible to confirm that this is the source of the issue after the container exits by reviewing the Apache log file as follows:

# 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:

这篇关于使用-it选项时,Docker容器退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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