Restart apache on Docker

Restart apache on Docker

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

Stefan Bogdanescu

Founder & Senior Architect

Restart Apache on Docker

When managing a web server inside a Docker container, the standard Unix service command often fails to behave as expected. Attempting to restart Apache with service apache2 restart frequently results in an "Address already in use" error for port 80, even when the application appears to have stopped gracefully. This specific failure mode stems from a fundamental misunderstanding of how container processes manage resources compared to native system services.

The root cause lies in the initialization order and process lifecycle defined in your Dockerfile. In the provided configuration, the container entrypoint is set to run Apache in the foreground via CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]. When a command fails or returns an exit code, the container stops immediately. However, if you attempt to restart the service using service apache2 restart while the container is still running (or if the previous process hasn't fully detached), the Apache child processes may still be holding sockets open.

The error message (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 indicates that the main Apache parent process or a lingering worker is still bound to port 80. The service command tries to start the new instance, but the OS refuses the request because the network socket is occupied by the previous incarnation. This is a classic "zombie" scenario where the logical service has stopped, but the underlying file descriptor remains open.

To resolve this in a Docker environment, you must avoid relying on host-level service commands unless the container is specifically designed to act as a daemon that stays alive after the command completes. Instead, the robust approach involves manipulating the container's process directly or ensuring clean termination before restart.

If you need to update an .htaccess file and apply changes dynamically without rebuilding the image, follow this sequence:

  1. Stop the container completely: Use docker stop <container_id> rather than trying to kill just the Apache process inside it. This ensures that all child processes are terminated and sockets are released.
  2. Apply your changes: Mount a volume containing the updated .htaccess file or copy the new file into the /var/www/html/ directory while the container is stopped.
  3. Start the container fresh: Use docker start <container_id> to launch the process again.

Alternatively, if you prefer not to stop the container and want a hot-reload capability, you can execute the restart command inside the running container using docker exec. However, because Apache runs in the foreground by default, executing service apache2 restart inside the container often fails for the same reason described above: it tries to bind to a port already held by the parent process.

The correct internal approach requires stopping the current master process explicitly before spawning a new one. You can do this by sending a termination signal to the Apache parent process ID (PID) or by restarting the container via the Docker API. A practical script within the container might look like this:

# Kill existing Apache processes
pkill -9 apache2

# Small delay to ensure sockets are fully released
sleep 1

# Start Apache again in foreground mode
/usr/sbin/apache2 -D FOREGROUND

This manual intervention guarantees that no orphaned processes hold the port. It is important to note that this approach bypasses the graceful shutdown logic of Apache, which might discard open connections abruptly. For production environments where connection stability is critical, stopping and restarting the entire container (docker stop followed by docker start) remains the safest method.

If you are deploying a Laravel application or similar framework, understanding this distinction is crucial. While tools like php artisan serve handle their own lifecycle, Apache serves static files and handles backend requests differently. When integrating Apache with PHP-FPM or running PHP directly via modules, the configuration in your Dockerfile must ensure that the www-data user owns the web directory and that modules like rewrite are enabled, as shown in your snippet.

For further guidance on configuring PHP environments and optimizing Apache setups for modern applications, resources such as https://laravelcompany.com offer valuable insights into best practices for deploying these stacks. However, the core principle remains: never assume a service command will handle process cleanup in an isolated container environment. Always verify that the socket is truly free before attempting to bind to it again.

Our network