如何在默认情况下在Docker容器中启动php-fpm?

我有这个Docker镜像 –

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"] 

基本上,我希望这样,当docker集装箱启动时,php-fpm启动。 如果我手动进入容器,并使用/usr/sbin/php-fpm打开它,我有php-fpm工作。

我用这个命令在我的可执行文件里面试了一下(不起作用)。 我尝试使用服务模块以及没有运气。

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

我怎样才能让php-fpm和apache一起运行?

您应该使用supervisor以启动多项服务

在你的dockerfile中,安装supervisor,然后启动

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

而你的docker/supervisord.conf包含你想要启动的所有服务,所以你可以有这样的事情

 [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 

当然,你应该适应你的path和php-fpm版本和你的服务(在我的例子中为nginx,为你的apache等),但基本上pipe理员是从一个起点pipe理多个服务的最佳方式。

在这里你可以find关于主pipe的docker工人的官方文档

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

我最近需要类似的东西。 对于高山 linux映像,运行php-fpm和web服务器作为容器命令就足够了。 在Dockerfile定义的有点像这样:

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

即。 守护php-fpm ,然后在前台运行nginx

Ubuntu / Debian映像中,还需要通过运行Dockerfile RUN命令来启动最近安装的软件包,如下所示:

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

然后在CMD命令中重新启动php-fpm

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

有关policy-rc.d更多信息,请参阅此askubuntu问题

我使用rc.local在容器中启动服务,然后在该容器中运行一个命令来执行rc.local。 所以这是一个示例运行命令:

 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" 

这是/ etc的rc.local文件之一

 #!/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 

这个function是使用端口80和443暴露的映像“myimage / name”启动一个名为“mycontainer”的容器。 一旦启动,它会调用mysql,php5-fpm和nginx来启动,每次暂停5秒。 通过这种方式,您可以调用任何服务来启动您希望在该容器内运行的服务。 请记住为这些服务添加开放端口。

这个运行命令还会每隔100秒将“Hello World”附加到日志文件中,作为一个“脉冲”,它可以让你知道什么时候运行,什么时候停止。