Building Middleware Docker Images: PHP-FPM with Nginx

Having established a base CentOS 7 image, the next step is to construct middleware images. This example demonstrates building a Docker image that integrates Nginx and PHP-FPM.

The project structure is as follows:

docker-training/
├── centos7/
├── mysql/
├── php-fpm/
│   ├── Dockerfile
│   ├── nginx_default.conf
│   ├── nginx_nginx.conf
│   ├── php_www.conf
│   ├── supervisor_nginx.conf
│   └── supervisor_php-fpm.conf
├── README.md
└── wordpress/

The supervisor_*.conf files define service configurations for managign Nginx and PHP-FPM processes within the container.

Dockerfile Breakdown

FROM csphere/centos:7.1

ENV APP_ROOT="/var/www/html"

# Install required packages
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs && \
    yum -y install nginx php-cli php-mysql php-pear php-ldap php-mbstring \
                   php-soap php-dom php-gd php-xmlrpc php-fpm php-mcrypt && \
    yum clean all

# Configure Nginx
ADD nginx_nginx.conf /etc/nginx/nginx.conf
ADD nginx_default.conf /etc/nginx/conf.d/default.conf

# Configure PHP-FPM
ADD php_www.conf /etc/php-fpm.d/www.conf
RUN sed -i 's/;cgi\.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini

# Create web root and test file
RUN mkdir -p ${APP_ROOT} && echo "<?php phpinfo(); ?>" > ${APP_ROOT}/info.php

EXPOSE 80 443

# Supervisor configuration
ADD supervisor_nginx.conf /etc/supervisor.d/nginx.conf
ADD supervisor_php-fpm.conf /etc/supervisor.d/php-fpm.conf

ONBUILD ADD . ${APP_ROOT}
ONBUILD RUN chown -R nginx:nginx ${APP_ROOT}

Key points:

  • The base image is csphere/centos:7.1.
  • The web root is set to /var/www/html via the APP_ROOT environment variable.
  • Required PHP extensions and Nginx are installed using yum.
  • Default Nginx and PHP-FPM configurations are replaced with custom versions.
  • A minimal info.php file is created for testing.
  • Ports 80 and 443 are explicitly exposed.
  • Supervisor manages both Nginx and PHP-FPM as child processes.
  • ONBUILD instructions prepare the image for downstream application layers.

Building the Image

Build the image with:

docker build -t csphere/php-fpm:5.4 .

Testing the Image

Run a container from the image:

docker run -d -p 8080:80 --name web-test csphere/php-fpm:5.4

Verify the container is running:

docker ps -a

Assuming the host’s public IP is 10.10.4.182, access http://10.10.4.182:8080/info.php in a browser. A successful PHP info page confirms Nginx and PHP-FPM are correctly integrated.

To inspect services inside the container:

docker exec -it web-test /bin/bash

Within the container shell, use Supervisor to check process status:

supervisorctl

Expected output:

nginx                            RUNNING   pid 7, uptime ...
php-fpm                          RUNNING   pid 8, uptime ...

This confirms both services are active. Exiting the shell leaves the container running, ready for application deployment.

Tags: docker PHP-FPM nginx middleware containerization

Posted on Fri, 29 May 2026 22:51:44 +0000 by esscher