无法可靠地确定服务器的完全限定域名...如何在 Docker 中解决?

新手上路,请多包涵

我刚开始使用 Docker,我正在遵循该 教程,该教程基本上显示了这些步骤:

  1. 像这样创建一个 Dockerfile:
    From php:7.0-apache
   copy src/ /var/www/html
   EXPOSE 80

  1. 从我有我的 dockerfile 的地方构建容器:
    $ docker build -t mytest .

  1. 生成图像“mytest”后,我运行它:
    $ docker run  -p 80 mytest

这就是我得到的错误:

AH00558:apache2:无法可靠地确定服务器的完全限定域名,使用 172.17.0.2。全局设置“ServerName”指令以抑制此消息 AH00558:apache2:无法可靠地确定服务器的完全限定域名,使用 172.17.0.2。全局设置“ServerName”指令以禁止显示此消息 [Sun Sep 17 16:25:12.340564 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.23 已配置 – 正在恢复正常操作 [Sun Sep 17 16:25:12.340666 2017] [core:notice] [pid 1] AH00094:命令行:’apache2 -D FOREGROUND’

所以,我不知道如何解决它。任何想法?

原文由 IgorAlves 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

我想提供另一种(可能更像 Docker-ish)解决警告消息的方法。但首先,在盲目设置 ServerName 之前, 了解 Apache 实际在做什么来计算域名是有意义的。 http://ratfactor.com/apache-fqdn.html 上有一篇很好的文章解释了这个过程。

一旦我们可以确认 getnameinfo 是 Apache 用来查找名称的名称,并且它使用 /etc/nsswitch.conf 来确定首先查找的位置,我们就可以弄清楚要修改什么。

如果我们查看容器内的 nsswitch.conf 我们可以看到它在外部 DNS 之前的 /etc/hosts 文件中查找主机:

 # cat /etc/nsswitch.conf | grep hosts
hosts:          files dns

知道了这一点,也许我们可以在 Docker 运行时影响文件而不是将其添加到我们的配置文件中?

如果我们查看 Docker 运行参考文档,在 https://docs.docker.com/engine/reference/run/#managing-etchosts/etc/hosts 文件中有一节。它提到:

您的容器将在 /etc/hosts 中包含定义容器本身的主机名以及 localhost 和其他一些常见内容的行。

因此,如果我们只是设置容器主机名,它将被添加到 /etc/hosts 这将解决 Apache 警告。幸运的是,使用 --hostname-h 选项很容易做到这一点。如果我们将其设置为完全限定的域名,Docker 会将其设置在我们的 /etc/hosts 文件中,Apache 会选择它。

试试这个很容易。带有警告的示例(无主机名):

 $ docker run --rm php:7.0-apache
AH00558: apache2: 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
AH00558: apache2: 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
[Sun Sep 17 19:00:32.919074 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.23 configured -- resuming normal operations
[Sun Sep 17 19:00:32.919122 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

现在将 -h 选项设置为完全限定的域名:

 $ docker run --rm -h myapp.mydomain.com php:7.0-apache
[Sun Sep 17 19:01:27.968915 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.23 configured -- resuming normal operations
[Sun Sep 17 19:01:27.968968 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

希望这有助于回答这个问题并提供一些关于 Apache 和完全限定域名的知识。

原文由 Andy Shinn 发布,翻译遵循 CC BY-SA 3.0 许可协议

这不是一个错误,它只是一个警告,您可以放心地忽略它。它的意思是 ServerName 未设置,因此它将假定机器 IP 相同。

如果您查看 /etc/apache2/sites-available 中存在的默认配置,您会发现 000-default.confdefault-ssl.conf

 root@d591ab6febff:/etc/apache2/sites-available# cat 000-default.conf
<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

ServerName 指令在配置中被注释

#ServerName www.example.com

因此,如果您担心警告,您应该将其更改为您想要的值,如下所示

ServerName www.tarunlalwani.com
ServerAlias tarunlalwani.com

您需要在文件夹中创建一个配置,然后使用 Dockerfile 覆盖默认配置中的文件。然后警告就会消失。

原文由 Tarun Lalwani 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进