为什么httpd容器会在Docker中没有任何错误的情况下立即退出

我是Docker的新手。 我的Dockerfile(来自教程)是:

FROM ubuntu ENV DEBIAN_FRONTEND noninteractive RUN apt-get update \ && apt-get install -y --no-install-recommends apache2 \ libapache2-mod-jk VOLUME ["/var/log/apache2"] RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf EXPOSE 80 443 ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"] 

我给docker build -t mg:httpd apache\ 。 没有错误。

 λ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mg httpd 262d7a4f85fc About an hour ago 248MB 

docker run -it mg:httpd

没有错误,但光标立即返回到命令提示符。 获取Unable to connecthttp://localhost/在浏览器中。

 λ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 600231bbd461 mg:httpd "apache2ctl -D FOR..." 2 minutes ago Exited (0) 2 minutes ago 

docker日志是空的

 λ docker events 2017-09-08T15:26:10.726720800+05:30 container create 5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157 (image=mg:httpd, name=zealous_ritchie) 2017-09-08T15:26:10.740310700+05:30 container attach 5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157 (image=mg:httpd, name=zealous_ritchie) 2017-09-08T15:26:11.035777100+05:30 network connect 2b8829b336575a2aa2210f55105d793e8f1b196d51845d38d3dc8ff322af6143 (container=5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157, name=bridge, type=bridge) 2017-09-08T15:26:11.090873500+05:30 volume mount e8421cbd867fca04f0dd3ee6da815c59cad5fc32c5a427710d144213b43caa26 (container=5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157, destination=/var/log/apache2, driver=local, propagation=, read/write=true) 2017-09-08T15:26:11.548736900+05:30 container start 5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157 (image=mg:httpd, name=zealous_ritchie) 2017-09-08T15:26:11.573272300+05:30 container resize 5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157 (height=41, image=mg:httpd, name=zealous_ritchie, width=168) 2017-09-08T15:26:12.733164900+05:30 container die 5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157 (exitCode=0, image=mg:httpd, name=zealous_ritchie) 2017-09-08T15:26:13.227455500+05:30 network disconnect 2b8829b336575a2aa2210f55105d793e8f1b196d51845d38d3dc8ff322af6143 (container=5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157, name=bridge, type=bridge) 2017-09-08T15:26:13.306885700+05:30 volume unmount e8421cbd867fca04f0dd3ee6da815c59cad5fc32c5a427710d144213b43caa26 (container=5325b79abe1988312952629165551c169a8bdc3fbe6e473cf0177859db7c0157, driver=local) 

可能是什么原因? 当容器未启动时,如何查看Apache日志\ conf文件?

所以你的Web服务器不是作为前台进程的startet,这就是为什么容器立即停止。

我想你应该改变

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

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

因为您希望在安装容器时运行该命令。

ENTRYPOINT指令声明了用来执行CMD的可执行文件。

 ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"] results in: $ apache2ctl -D FOREGROUND <command either from the run command line or the CMD directive> 

Ubuntu映像中的默认命令是/bin/bash 。 所以,当你运行容器使用:

 docker run -it mg:httpd 

结果执行的命令将是:

 $ apache2ctl -D FOREGROUND /bin/bash 

这显然没有什么意义。 我会推荐这个post,它很好的解释了细节。

在上面描述的变化之后,它将如下所示:

 (default) ENTRYPOINT ["/bin/sh", "-c"] CMD ["apache2ctl", "-D", "FOREGROUND"] what results in: $ /bin/sh -c "apache2ctl -D FOREGROUND"