57

I've created a very simple Dockerfile:

FROM php:7.0-apache
COPY src/ /var/www/html
EXPOSE 80

But when I start it, I get this error:

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

So, I don't know how to solve it. Any idea?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
IgorAlves
  • 5,086
  • 10
  • 52
  • 83
  • I found this solution documented at https://serverfault.com/questions/1024606/configuring-a-php-web-service-container-at-build-time/1025053#1025053 gives me Access to _any_ **Configuration File** I need to configure. – Bodo Hugo Barwich Jul 13 '20 at 14:55

6 Answers6

54

I'd like to offer another (maybe more Docker-ish) way of solving the warning message. But first, it makes sense to understand what Apache is actually doing to figure out the domain name before just blindly setting ServerName. There is a good article at http://ratfactor.com/apache-fqdn.html which explains the process.

Once we can confirm that getnameinfo is what Apache uses to lookup the name and that it uses /etc/nsswitch.conf to determine where to go first for the lookup, we can figure out what to modify.

If we check out the nsswitch.conf inside the container we can see that it looks up hosts in the /etc/hosts file before external DNS:

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

Knowing this, maybe we can influence the file at Docker runtime instead of adding it to our configuration files?

If we take a look at the Docker run reference documentation there is a section on the /etc/hosts file at https://docs.docker.com/engine/reference/run/#managing-etchosts. It mentions:

Your container will have lines in /etc/hosts which define the hostname of the container itself as well as localhost and a few other common things.

So, if we just set the container hostname, it will get added to /etc/hosts which will solve the Apache warning. Fortunately, this is very easy to do with the --hostname or -h option. If we set this to a fully qualified domain name, Docker will set it in our /etc/hosts file and Apache will pick it up.

It is pretty easy to try this out. Example with the warning (no hostname):

$ 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'

Now with the -h option set to a fully qualified domain name:

$ 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'

Hope this helps answer the question and provide some learning about Apache and fully qualified domain names.

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
11

This is not an error it is just a warning and you can safely ignore it. What it is saying is that ServerName is not set so it will assume machine IP as the same.

If you look at the default configs present inside /etc/apache2/sites-available, you will find 000-default.conf and default-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>

The ServerName directive is commented in the config

#ServerName www.example.com

So if you are worried about the warning you should change it to the value you want like below

ServerName www.tarunlalwani.com
ServerAlias tarunlalwani.com

You would need to create a config in your folder and then overwrite the files from default config using Dockerfile. Then the warning would be gone.

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
6

If you are using docker do this: add ServerName localhost to apache2.conf from the Dockerfile and then restart the server

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

RUN service apache2 restart

this should get rid of the warning.

you can also do this from the terminal

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

and don't forget to restart your server

sudo systemctl restart apache2

The error should be gone after the server restart.

jerryurenaa
  • 3,863
  • 1
  • 27
  • 17
3

Problem:
While running docker file giving below error:

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


Solution:
goto root
goto etc file
then goto apache2
then nano apache2.conf file
and then below the line # The directory where shm and other runtime files will be stored. put a new line (Enter) and write:

ServerName localhost

Save the file and exit root and run the docker run command again, it will work.


Youtube video reference link for the same issue: Watch

GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27
1

To avoid that warning, you can set the ServerName with you IP.

See this example: 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 && \

(although iproute can work differently on a Mac: it depends on your host. See iproute2mac issue 8)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I use to add

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

either to the Dockerfile or to the entrypoint file if used.

Setop
  • 2,262
  • 13
  • 28