Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 879
J. Doe Avatar asked May 19 '16 04:05

J. Doe


People also ask

What is FPM docker?

FPM (effing package manager) Docker Images cdrx/fpm-{$distro} are a series of Docker images to quickly get packages building in fpm on different Linux distributions. Each distro has fpm installed and all the required dependencies to build packages for that platform. All images are 64bit.

Which command can be used to start a docker container from an image?

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let's start our image and make sure it is running correctly.


2 Answers

You should use supervisor in order to launch several services

In your dockerfile, install supervisor, then you launch

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

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

[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 with your path and php-fpm versions and your services (nginx in my example, apache for you etc...), but basically supervisor is the best way to manage the start of several services from one start point.

Here you can find the official doc of docker about supervisor

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

like image 74
olibiaz Avatar answered Oct 05 '22 08:10

olibiaz


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)
like image 26
ckeeney Avatar answered Oct 05 '22 09:10

ckeeney