【问题标题】:Could not reliably determine the server's fully qualified domain name ... How to solve it in Docker?无法可靠地确定服务器的完全限定域名...如何在 Docker 中解决?
【发布时间】:2022-03-10 01:59:56
【问题描述】:

我刚开始使用 Docker,我正在关注tutorial,它基本上显示了这些步骤:

  1. 像这样创建一个 Dockerfile:

    From php:7.0-apache
    copy src/ /var/www/html
    EXPOSE 80
    
  2. 从我拥有 dockerfile 的地方构建容器:

    $ docker build -t mytest .
    
  3. 生成图像“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'

所以,我不知道如何解决它。有什么想法吗?

【问题讨论】:

标签: apache docker


【解决方案1】:

我想提供另一种(可能更像 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 和完全限定域名的知识。

【讨论】:

    【解决方案2】:

    这不是错误,它只是一个警告,您可以放心地忽略它。它的意思是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 覆盖默认配置中的文件。然后警告就会消失。

    【讨论】:

      【解决方案3】:

      为避免该警告,您可以使用您的 IP 设置 ServerName。

      请参阅此示例:tagplus5/docker-php/7-apache/Dockerfile

          apt-get install -y --no-install-recommends iproute2 apache2 php7.0 libapache2-mod-php7.0 \
              php7.0-mysql php7.0-sqlite php7.0-bcmath php7.0-curl ca-certificates && \
          apt-get autoremove -y && \
          rm -rf /var/lib/apt/lists/* && \
      echo "ServerName $(ip route get 8.8.8.8 | awk '{print $NF; exit}')" >> /etc/apache2/apache2.conf && \
      

      (虽然 iproute 在 Mac 上的工作方式不同:这取决于您的主机。请参阅 iproute2mac issue 8

      【讨论】:

        【解决方案4】:

        问题:
        运行 docker 文件时出现以下错误:

        AH00558:apache2:无法可靠地确定服务器的完全限定域名,使用 172.17.0.2。全局设置“ServerName”指令以禁止显示此消息


        解决方案:
        转到root
        转到 etc 文件
        然后转到apache2
        然后纳米apache2.conf文件
        然后在# The directory where shm and other runtime files will be stored. 行下方换一个新行(回车)并写入:

        ServerName localhost
        

        保存文件并退出root并再次运行docker run命令,它将起作用。


        同一问题的 Youtube 视频参考链接:Watch

        【讨论】:

          【解决方案5】:

          如果您使用 docker,请执行以下操作:从 Dockerfile 将 ServerName localhost 添加到 apache2.conf,然后重新启动服务器

          RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
          
          RUN service apache2 restart
          

          这应该消除警告。

          您也可以从终端执行此操作

          echo "ServerName localhost" >> /usr/local/etc/apache2/apache2.conf
          

          别忘了重启你的服务器

          sudo systemctl restart apache2
          

          服务器重启后错误应该消失了。

          【讨论】:

            【解决方案6】:

            我用来添加

            sed -i -e 's/Listen 80/Listen 80\nServerName localhost/' /etc/apache2/ports.conf
            

            到 Dockerfile 或入口点文件(如果使用)。

            【讨论】:

              猜你喜欢
              • 2021-05-29
              • 2011-08-16
              • 2013-08-19
              • 1970-01-01
              • 2018-05-19
              • 2022-07-18
              • 2015-10-15
              • 2021-08-14
              • 1970-01-01
              相关资源
              最近更新 更多