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

I have this Docker image -

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
RUN git clone https://github.com/.../dockerAnsible.git
RUN ansible-playbook dockerFileBootstrap.yml
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

CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

Basically, I want it so that php-fpm starts when the docker container starts. I have php-fpm working if I manually go into the container and turn it on with /usr/sbin/php-fpm.

I tried it inside of my ansible file with this command (it didn't work). I tried using the service module as well with no luck.-

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

How can I have php-fpm running along with apache?



Solution 1:[1]

I came here looking for how to run php-fpm in the foreground so it could be PID 1 in a docker container. The solution is

php-fpm -F -R

Explanation

We can check the available options with php-fpm --help

-F, --nodaemonize 
      force to stay in foreground, and ignore daemonize option from config file

If you are running php-fpm in a docker container, there is a good chance you are running the process as root. php-fpm won't start as root without an extra flag:

  -R, --allow-to-run-as-root
        Allow pool to run as root (disabled by default)

Solution 2:[2]

I needed similar thing recently. For alpine linux images it sufficed to run both php-fpm and a web server as the container command. Defined in Dockerfile somewhat like this:

CMD /usr/bin/php-fpm -D; nginx

ie. to daemonize php-fpm and then run nginx in foreground.

On ubuntu/debian images there is also necessary to allow starting recently installed packages by running a Dockerfile RUN command like this:

RUN echo "exit 0" > /usr/sbin/policy-rc.d

and then restart the php-fpm within a CMD command

CMD /etc/init.d/php7.0-fpm restart && nginx -g "daemon off;"

More on policy-rc.d to be found in this askubuntu question

Solution 3:[3]

You may find this config useful if you'd like to run php-fpm and Apache (usually using MPM Event) in the same container:

[supervisord]

[program:php-fpm]
command=php-fpm -F -R
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:apachectl]
command=apachectl -D "FOREGROUND" -k start
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

Solution 4:[4]

I use rc.local to start services inside a container, and then run a command inside that container to execute rc.local. So here's a sample run command:

sudo docker run --restart=always -itd --name mycontainer -p 80:80 -p 443:443 myimage/name /bin/bash -c "/etc/rc.local && while true; do echo hello world; sleep 100; done"

And this is one of the rc.local files from /etc

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sleep 5
service mysql start
sleep 5
service php5-fpm start
sleep 5
service nginx start
exit 0

What this does is launch a container called "mycontainer" using the image "myimage/name" with ports 80 and 443 exposed. Once launched it calls mysql, php5-fpm, and nginx to start up, pausing between each for 5 seconds. In this manner you can call any service to start that you would want to run inside that container. Remember to add open ports for those services though.

This run command will also append "Hello World" into your log file every 100 seconds as a "pulse" that will let you know when it was running and when it was stopped.

Solution 5:[5]

I believe @ckeeney's answer above could be accepted as the correct answer but I was unable to get it working with my particular case.

use dumb-init

I have been able to proxy PID1 through dumb-init, and daemonize PHP-FPM with the following command : dumb-init /usr/sbin/php-fpm7.2 -F -R https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html

Solution 6:[6]

This works for me on Docker running Ubuntu 20.04 with nginx and php-fpm.

CMD /etc/init.d/php7.4-fpm start -F && nginx -g "daemon off;"

No supervisors or cron jobs are required.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ckeeney
Solution 2 helvete
Solution 3 Vahid Amiri
Solution 4 Gavin Tremlor
Solution 5
Solution 6 Andrey K