Docker PHP-APACHE container set the ServerName directive globally

Docker PHP-APACHE container set the ServerName directive globally

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Docker PHP-Apache Container Set the ServerName Directive Globally

When you spin up a php:apache container, Apache is configured to run in the foreground by default. However, immediately after startup, you will often see this warning in your logs:

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

This isn't a critical error preventing Apache from serving requests, but it indicates that the server is missing crucial configuration metadata. The warning persists because Docker containers are inherently ephemeral and lack persistent state. When a container starts, it does not automatically inherit environment variables or hostnames from your local machine. Instead, it sees its own internal IP address—in this specific case 172.17.0.2—as the primary identifier. Without an explicit ServerName, Apache defaults to this vague hostname, which breaks virtual hosting logic and generates these warnings every time the container restarts.

To resolve this, you must explicitly define the server name within the container's configuration. The most reliable method is to override the default Apache configuration file included in the official PHP image. You can achieve this by copying a custom apache2.conf into the container or by creating a specific 001-default.conf file that takes precedence over the defaults.

Here is a robust pattern for your Dockerfile:

FROM php:7.4-apache

# Copy your application code
COPY . /var/www/html/

# Create a custom configuration file to set the ServerName
RUN mkdir -p /usr/local/apache2/conf-available && \
    echo "ServerName localhost" > /usr/local/apache2/conf-available/001-default.conf && \
    a2enmod rewrite headers 2>/dev/null || true

# Enable the custom configuration (on Debian-based systems)
RUN ln -s /usr/local/apache2/conf-available/001-default.conf /usr/local/apache2/conf-enabled/

EXPOSE 80
CMD ["apache2-foreground"]

The critical line here is the creation of the 001-default.conf file. By placing it in the conf-available directory and linking it to conf-enabled, you ensure that Apache reads this directive before applying its own defaults. Setting the value to localhost suppresses the warning effectively for local development environments. If you are deploying this container to a production environment where your application is accessible via a specific domain (e.g., example.com), you should replace localhost with that fully qualified domain name. This ensures that Apache logs and error reports reference the correct hostname, making debugging significantly easier.

Another approach involves passing environment variables directly when running the container, though this depends on the specific base image version and how it parses arguments. Some newer PHP images respect the APACHE_SERVERNAME environment variable, but relying on this is less portable than modifying the configuration files directly.

When deciding between modifying the entrypoint script versus changing the config file, the config file approach is generally preferred for clarity. The entrypoint script in the official image is often complex and difficult to patch without breaking other initialization steps. By injecting a simple ServerName directive into the standard Apache configuration hierarchy, you maintain compatibility with modules like mod_rewrite and ensure that virtual hosts are resolved correctly based on the Host header sent by the client.

If you are using Docker Compose, you can also mount a local configuration file into the container at runtime:

version: '3'
services:
  app:
    image: php:7.4-apache
    volumes:
      - ./apache2.conf:/usr/local/apache2/conf-enabled/001-default.conf
    command: apache2-foreground

In this scenario, your apache2.conf file on the host should contain ServerName example.com. This method is highly flexible, allowing you to change the server name without rebuilding the Docker image. It also keeps your application code and configuration separate, adhering to best practices for containerized deployments.

Ultimately, suppressing the AH00558 warning is not just about cleaning up logs; it is about establishing a clear identity for your web server. Whether you choose to bake the configuration into the image or mount it dynamically, defining the ServerName globally ensures that Apache behaves predictably across different environments and development setups.

Our network