9.5: [BONUS OPTIMIZATION]: Create your own Apache Dockerfile with PHP-FPM configuration

So far, we’ve been using the standard Apache image and mounting a filehttpd.conffrom the host to enable PHP-FPM.
Problem: this approach makes the deployment less portable and adds a manual step.

Solution: create a custom image that directly includes ourhttpd.conf and the configuration for PHP-FPM.

Steps to create the image

Project structure:

monprojet/
│
├─ Dockerfile
├─ httpd.conf
└─ app/
   └─ index.php

Example Dockerfile:

# Image de base : Apache standard
FROM httpd:2.4

# Copier notre configuration Apache personnalisée
COPY httpd.conf /usr/local/apache2/conf/httpd.conf

# Copier l'application web
COPY app/ /usr/local/apache2/htdocs/

# Exposer le port 80
EXPOSE 80

# Commande par défaut (déjà définie dans l'image httpd)
CMD ["httpd-foreground"]

httpd.confIn the Dockerfile, configure the proxy to PHP-FPM on your PHP-FPM container’s port, for example

ServerName localhost

LoadModule mpm_event_module modules/mod_mpm_event.so

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so

DocumentRoot "/var/www/html"

<Directory "/var/www/html">
    AllowOverride All
    Require all granted
</Directory>

DirectoryIndex index.php index.html

ProxyPassMatch ^/(.*\.php)$ fcgi://php-fpm:9000/var/www/html/$1

Build and run the image

  1. Build:
docker build -t mon-apache-phpfpm:latest .
  1. Launching the container:
docker run -d -p 8080:80 --name mon-site --network monprojet-network mon-apache-phpfpm:latest
  • Note: We connect the container to the networkDockerwhere PHP-FPM is runningmonprojet-network so that the proxy works.

Advantages of this approach

  • httpd.confPortable: no need to mount the file.
  • Ready to use: everything is built into the image.
  • app/Easy to replicate: for other projects, simply rebuild with a different .
  • Secure and clean: fewer exposed volumes and fewer manual configuration errors.

Beginner tip: to take it a step further, you can create an even lighter image by removing unnecessary Apache modules or starting with “slim” images, but the main idea here is to make deployment simple and self-contained.