Combining PHP-FPM and Nginx in a Single Dockerfile
When deploying a PHP application without Docker Compose, you face a specific constraint: your build process must produce a single image containing both the web server and the application runtime. This usually stems from organizational policies that prefer monolithic images for production stability or simplified orchestration. The core question is architectural: should you base your image on php:7.1.8-fpm and layer Nginx on top, or start with an official Nginx image and install PHP-FPM using the system package manager?
The answer depends heavily on how you handle static assets versus dynamic requests and how you manage dependencies.
The Layering Strategy: Base Image Choice
Starting with php:7.1.8-fpm is generally the safer bet for a single-image deployment, provided your PHP version requirements align with that tag. This approach leverages the official Alpine or Debian-based PHP image which already includes essential build tools (like apt-get or apk) and common system libraries required by Nginx.
If you choose this path, your Dockerfile looks like this:
FROM php:7.1.8-fpm
# Install Nginx and necessary dependencies
RUN apt-get update && apt-get install -y nginx libnginx-mod-http-echo test-curl
# Copy custom Nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Move PHP-FPM socket to a location Nginx can access (optional but common)
# Note: Official images usually handle this, but you must ensure paths match
RUN cp /usr/share/nginx/html/* /var/www/html/ 2>/dev/null || true
# Disable the default Nginx site if it conflicts
RUN rm -f /etc/nginx/sites-enabled/default
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
The key advantage here is that you inherit a PHP environment pre-configured with PECL extensions, opcache settings, and standard MIME types. You avoid the friction of manually compiling or configuring complex PHP stacks from scratch inside an Nginx container. Since php:7.1.8-fpm images are Debian-based (in most official tags), installing Nginx via apt-get is straightforward and ensures binary compatibility with other system packages you might install later, such as Redis clients or specific system tools.
The Reverse Approach: Nginx Base with PHP-FPM
Alternatively, starting with an official nginx image introduces a different set of trade-offs. While the base layer will be smaller initially, installing PHP-FPM via apt-get on top of a minimal Nginx image often leads to dependency hell if you aren't careful. The official Nginx images are typically Alpine-based or Ubuntu-specific, but they do not include PHP runtime components by default.
To make this work, you would need to pull the specific PHP packages that match your desired version:
FROM nginx:alpine
RUN apk add --no-cache php71-fpm php71-fpm-pdo_mysql
# You must also ensure Nginx can connect to the FPM socket
COPY docker-php-fpm-wrapper.sh /usr/local/bin/docker-php-fpm-wrapper.sh
RUN chmod +x /usr/local/bin/docker-php-fpm-wrapper.sh
CMD ["/usr/local/bin/docker-php-fpm-wrapper.sh"]
This method requires more manual configuration to ensure the PHP-FPM process starts correctly alongside Nginx. You often have to create a wrapper script or modify the default.conf to explicitly point to the FPM socket path, which defaults to /var/run/php/php71-fpm.sock. If you forget to adjust the permissions on that socket or the web server user (www-data), you will encounter "Permission denied" errors at runtime.
Configuration and Socket Management
Regardless of the base image chosen, the critical failure point in a single-container setup is the connection between Nginx and PHP-FPM. In a multi-container setup using Docker Compose, this communication happens over the network or via a shared volume with relaxed permissions. In a single image, you must ensure the nginx user has read access to the FPM socket.
If you stick with the php:7.1.8-fpm base, the user is typically www-data by default in Nginx images, but the PHP-FPM user might differ depending on the specific tag. You may need to add a line like RUN chown -R www-data:www-data /var/run/php/ before copying your config files to ensure the socket is accessible.
Why Not Just Use Two Containers?
While your organization restricts Docker Compose for production, understanding why the standard practice favors two containers helps justify the complexity of the single-image approach. With separate containers, you get isolated resource limits; if PHP crashes, Nginx remains up to serve static assets or display a 502 error gracefully. In a monolithic image, a runaway PHP process consumes all CPU, effectively taking down the web server interface entirely. However, if your application is stateless and well-monitored, the single-image approach simplifies the deployment pipeline significantly by removing the need for service discovery logic in your orchestrator.
For environments where Docker Compose is off-limits, building a unified image that bundles the web server and PHP runtime offers a viable path forward, provided you meticulously manage user permissions and socket paths during the build phase.