How can I start php-fpm in a Docker container by default?

How can I start php-fpm in a Docker container by default?

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

Stefan Bogdanescu

Founder & Senior Architect

How to Start php-fpm by Default in a Docker Container

You are currently using a CentOS 7 base image that boots into Apache HTTPD (httpd) as its default process. The container exposes ports 80 and 443, and the CMD instruction explicitly launches /usr/sbin/httpd -D FOREGROUND. While your PHP-FPM installation works when you manually execute /usr/sbin/php-fpm inside the running container, it does not start automatically because Docker expects the main process (PID 1) to be the one managing the application logic.

To have PHP-FPM start alongside Apache by default, you must replace the default CMD instruction in your Dockerfile. However, simply swapping the command is not enough if you intend to maintain a specific architecture where Apache acts as a reverse proxy. In that scenario, you need to ensure both processes are running simultaneously without one killing the other or orphaning the child processes.

Managing Multiple Processes in PID 1

The core challenge here is process management within a container. By default, httpd runs as the primary process. If you replace the CMD entirely with just /usr/sbin/php-fpm, Apache will stop running, and your web server won't serve static files or handle reverse proxy requests. Conversely, if you try to run both using shell commands like sh -c "php-fpm && httpd", you create a race condition: the first process finishes (or exits), and the container shuts down because PID 1 is no longer active.

The standard approach for running multiple services in a single container is to use an initialization script that launches both daemons and keeps them alive. This script acts as the entrypoint for the container. You can create a file named start.sh or similar, make it executable, and set it as your new CMD.

Here is how you modify your Dockerfile to achieve this:

FROM centos:7
MAINTAINER Me <me.me>
RUN yum update -y
RUN yum install -y git https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

RUN yum install -y ansible
# Ensure the PHP-FPM binary exists and is installed via your bootstrap logic
RUN git clone https://github.com/.../dockerAnsible.git
RUN ansible-playbook dockerFileBootstrap.yml

# Clean up systemd targets as per your original setup
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;

VOLUME [ "/sys/fs/cgroup" ]
EXPOSE 80 443 3306

# Create a startup script to manage both services
RUN touch /start.sh && chmod +x /start.sh << 'EOF'
#!/bin/bash
/usr/sbin/php-fpm
/usr/sbin/httpd -D FOREGROUND
EOF

CMD ["/start.sh"]

Why the Ansible Approach Failed

Your previous attempt used the Ansible command module to start PHP-FPM inside the container:

- name: Start php fpm
  command: /usr/sbin/php-fpm

This method fails for two primary reasons. First, Ansible tasks run sequentially or within a specific loop; simply adding a command to an existing playbook does not alter the Dockerfile's CMD instruction, which dictates what happens when the container launches. Second, even if you modified the Dockerfile to run this command, it would still execute as a child of PID 1 unless explicitly managed by a supervisor process. Without a parent process to manage signal forwarding (like SIGTERM), PHP-FPM might exit immediately upon receiving a stop signal, causing the container to restart unnecessarily or crash abruptly.

Architecture Considerations: Reverse Proxy vs. Direct Execution

There is a significant architectural decision to make here. If you are running Apache and PHP-FPM in the same container, you are likely implementing a reverse proxy pattern where Apache handles incoming HTTP requests (static assets, SSL termination) and forwards PHP requests to the PHP-FPM socket or pipe.

In this setup, both processes must remain active. The script approach shown above works, but it lacks process supervision. If httpd crashes due to a misconfiguration, the container will keep running with only PHP-FPM active, leading to confusing error logs and potential memory leaks.

For production environments, consider using a supervisor tool like supervisord. You would install supervisord, configure a supervisor.conf file that defines both httpd and php-fpm as programs, and set the container's entrypoint to run supervisord -c /etc/supervisor/conf.d/supervisor.conf. This provides robust process management, automatic restart policies, and proper signal handling.

Alternatively, if you prefer a simpler setup for development or non-critical workloads, the shell script method is sufficient, provided you accept that manual intervention might be required to recover from crashes without a dedicated supervisor.

If your stack evolves into a larger Laravel application, understanding how these entrypoints interact with orchestration tools like Docker Swarm or Kubernetes becomes critical. In those environments, the command executed by the orchestrator must align perfectly with the health checks and restart policies defined in your deployment manifests.

Our network