遵循Docker教程,但教程和实际输出有所不同

时间:2019-11-19 02:49:12

标签: docker

我是docker的新手,试图了解此域是什么。我偶然发现了tutorial。我是一个后端开发人员,并且喜欢本教程,因为它谈论微服务,但是本教程不解释命令。例如,我目前停留在following section of tutorial

我运行了以下命令

docker run  -p 80:80 --name my-apache2-alpine-1  my-apache2-alpine

此命令在我的终端上输出

$ docker run -p 80:80 --name my-apache2-alpine-1 my-apache2-alpine
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 30-resolver: executing... 
[cont-init.d] 30-resolver: exited 0.
[cont-init.d] 40-resolver: executing... 
[cont-init.d] 40-resolver: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] syncing disks.
[s6-finish] sending all processes the TERM signal.
[s6-finish] sending all processes the KILL signal and exiting.

此命令未达到教程中指出的预期结果,即http://localhost:80/myindex.html“无法连接”,即服务器不在此处运行。

现在这是我的问题

  1. 我在这里缺少什么,为什么我没有得到教程中指出的预期结果?
  2. 有人可以解释我上面运行的命令吗?

更新 以下是Dockerfile

的内容
~/docker-practice/static-site$ cat Dockerfile 
FROM smebberson/alpine-apache
ADD ./public-html/myindex.html /var/www/localhost/htdocs
~/docker-practice/static-site$ 

1 个答案:

答案 0 :(得分:1)

  

我在这里缺少什么,为什么我没有得到教程中指出的预期结果?

首先,我认为您正在学习错误的教程。我将首先从https://docs.docker.com获得文档和示例。但是,我们可以诊断出问题。

Apache似乎失败了,因为您的容器在启动Apache之后立即退出。当Apache失败时,它将消息写入通过Apache配置中的ErrorLog伪指令配置的任何路径。我们首先需要找出该文件的位置,然后在尝试启动Apache之后需要进行检查。

让我们从图像中生成外壳开始,绕过默认行为:

docker run -it --rm --entrypoint sh my-apache2-alpine 

这将为您提供一个外壳。尝试手动启动Apache:

/ # httpd -DFOREGROUND
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
/ #

现在,让我们找出Apache在哪里写入错误日志。我们可以使用apachectl -S命令来做到这一点:

/ # apachectl -S
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
ServerRoot: "/var/www"
Main DocumentRoot: "/var/www/localhost/htdocs"
Main ErrorLog: "/var/www/logs/error.log"
Mutex default: dir="/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/run/apache2/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=1000
Group: name="apache" id=1000
/ #

从以上输出中,我们看到主要错误日志为/var/www/logs/error.log。如果我们查看该文件,就会看到:

/ # cat /var/www/logs/error.log
[Wed Nov 20 01:22:25.012196 2019] [core:error] [pid 41] (2)No such file or directory: AH00099: could not create /run/apache2/httpd.pid
[Wed Nov 20 01:22:25.012226 2019] [core:error] [pid 41] AH00100: httpd: could not log pid to file /run/apache2/httpd.pid
/ #

我们现在知道为什么Apache无法启动:它想将PID文件写入/run/apache2/httpd.pid,但是没有/run/apache2目录。有几种解决方法:

  1. 您可以在Dockerfile中创建必要的目录:

    FROM smebberson/alpine-apache
    RUN mkdir -p /run/apache2
    ADD ./public-html/myindex.html /var/www/localhost/htdocs
    
  2. 您可以在运行时通过--tmpfs命令行选项创建目录:

    docker run  -p 80:80 --name my-apache2-alpine-1  --tmpfs /run/apache2 my-apache2-alpine
    

使用任何一种解决方案,您都应该会发现Apache容器现在能够正确启动。

  

有人可以解释我上面运行的命令吗?

您跑了:

docker run  -p 80:80 --name my-apache2-alpine-1  my-apache2-alpine

这将根据docker run图像创建一个新容器(my-apache2-alpine)。它将名称my-apache2-alpine-1分配给容器(--name my-apache2-alpine-1),因此您可以使用该名称来管理该容器,而不是容器ID。最后,它在主机端口80(-p 80:80)上发布了容器端口80,因此您可以访问Web服务器,就像它直接在主机上运行一样。

相关问题