How to restart apache2 without terminating docker container?
When working with a PHP Docker container based on php:5.6-apache, you might encounter a frustrating behavior where restarting Apache causes the entire container to stop rather than just reloading the web server. This happens because of how the base image is configured to run, specifically the use of a foreground mode combined with process supervision logic that isn't present in standard Apache installations.
In your specific scenario, running service apache2 restart sends a SIGTERM signal to the main Apache process. Because the container's entrypoint script detects that the main process (PID 1) has terminated, it immediately exits the container. The log output confirms this behavior:
phlaconapp_1 | [Mon May 30 10:21:11.754993 2016] [mpm_prefork:notice] [pid 1] AH00169: caught SIGTERM, shutting down
dltasklight_phlaconapp_1 exited with code 0
The message AH00169: caught SIGTERM indicates the graceful shutdown sequence was triggered. However, in a Docker environment without an init system like tini or supervisord, this exit code is treated as the end of the container lifecycle. When you see the container stop immediately after a restart command, it means there is no parent process to keep the container alive once Apache goes down and back up.
The Foreground Mode Trap
The root cause lies in how the apache2 package is launched inside these base images. They typically start Apache with the -D FOREGROUND flag. This forces Apache to run in the foreground, attaching directly to stdout and stderr. While convenient for logging, it removes the separation between the web server process and the container's shell session.
When you execute service apache2 restart, the service manager kills the existing Apache process and attempts to start a new one. If the entrypoint script expects PID 1 to remain active (which is common in simple Dockerfiles that just copy the default Debian entrypoint), the moment Apache dies, the container exits. Even if Apache starts successfully afterward, the container has already terminated.
Correct Restart Strategies
To customize your environmentâsuch as installing new extensions or modifying configuration filesâyou need a method to reload Apache without killing the PID 1 process that keeps the container running.
The most reliable approach is to use apache2ctl directly instead of relying on the service command, which behaves differently in Docker containers. You can force a graceful restart using the graceful-restart directive:
apache2ctl graceful-restart
This command signals Apache to finish processing current requests before reloading its configuration and restarting worker processes. Crucially, it does not kill the main parent process, so the container remains active. You can verify that the restart succeeded by checking the logs for a new start message:
[pid X] AH00163: Apache/2.4.x ... configured -- resuming normal operations
If apache2ctl graceful-restart is unavailable or fails, another option is to use the kill -HUP signal on the master process. This triggers a hang-up signal that forces Apache to re-read its configuration files and restart workers without terminating the main process:
kill -HUP $(cat /var/run/apache2/apache2.pid)
Handling Configuration Changes
When installing extensions or modifying .htaccess files, ensure you are editing the correct configuration paths. For example, if you added an extension to /etc/php/5.6/mods-available, you must also enable it in the Apache configuration (/etc/apache2/mods-available). After making these changes, a simple reload is insufficient; a graceful restart is required to apply the new modules.
Additionally, address the warning about the fully qualified domain name (FQDN) seen in your logs:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name...
While this doesn't stop the container, it can clutter logs and cause issues if virtual hosts rely on hostname matching. Add ServerName localhost or a specific IP address to your Apache config file (usually /etc/apache2/ports.conf or a custom vhost) before restarting.
Why This Matters for Development
Using this approach allows you to iterate quickly during development. You can install extensions, tweak virtual host configurations, and verify behavior without rebuilding the image every time. Once your customization is stable, you can bake these changes into a Dockerfile or use a more robust base image that includes tini as PID 1, which handles process supervision more gracefully even when foreground processes restart.
For production environments where stability is paramount, consider switching to a modern PHP version (e.g., php:8.2-apache) or using php-fpm with Nginx, as Apache-based containers have largely become legacy choices in the Laravel ecosystem. However, for your current setup, apache2ctl graceful-restart is the definitive solution to avoid container termination.