How can I run php-fpm in the default Docker container?

I have a 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 php-fpm to start when the docker container starts. I have php-fpm if I manually go into the container and enable it with /usr/sbin/php-fpm .

I tried this inside my file with this command (this did not work). I also tried using the service module, but no luck.

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

How can I run php-fpm with apache?

+5
source share
5 answers

You must use supervisor to run multiple services

In your docker file, install the dispatcher, then you run

 COPY ./docker/supervisord.conf /etc/supervisord.conf .... CMD ["/usr/bin/supervisord", "-n"] 

And your docker/supervisord.conf contains all the services you want to run, so you can have something like this

 [program:php-fpm] command=/opt/remi/php70/root/usr/sbin/php-fpm -c /etc/php-fpm.conf ;command=/usr/sbin/php70-fpm -c /etc/php-fpm.d stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 [program:nginx] command=/usr/sbin/nginx stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 

Of course, you should adapt to your php-fpm paths and versions and your services (nginx in my example, apache for you, etc.), but basically a supervisor is the best way to control the launch of several services from one starting point.

Here you can find the docker white paper on supervisor

https://docs.docker.com/engine/admin/using_supervisord/

+7
source
I needed a similar thing again. For alpine Linux images, it was enough to run both php-fpm and the web server as a container command. Defined in Dockerfile somewhat like this:
 CMD /usr/bin/php-fpm -D; nginx 

i.e. for daemonize php-fpm and then run nginx in the foreground.

In ubuntu / debian images, you must also enable the launch of recently installed packages by running the Dockerfile RUN command as follows:

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

and then restart php-fpm inside php-fpm command

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

More information about policy-rc.d can be found in this askubuntu question.

+4
source

I use rc.local to start services inside the container, and then run a command inside this container to execute rc.local. So, here is an example 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 means is to launch a container called mycontainer using the image "myimage / name" with open ports 80 and 443. After starting it, it calls mysql, php5-fpm and nginx to start, pausing between them for 5 seconds. Thus, you can call any service to start that you want to run inside this container. Remember to add open ports for these services.

This start command will also add โ€œHello Worldโ€ to your log file every 100 seconds as a โ€œboostโ€, which will let you know when it was started and when it was stopped.

+1
source

I came here to see how to run php-fpm in the foreground so that it can be PID 1 in the docker container. Decision

 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 use php-fpm in the php-fpm container, there is a good chance that you are running the process with root privileges. php-fpm does not start as root without an additional flag:

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

I believe that @ckeeney's answer above can be accepted as the correct answer, but I could not get it to work with my specific case.

use dumb-init

I managed to proxy PID1 via 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

0
source

All Articles