默认情况下,如何在 Docker 容器中启动 php-fpm?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37313780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I start php-fpm in a Docker container by default?
提问by J. Doe
I have this Docker image -
我有这个 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"]
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
.
基本上,我想要它以便 php-fpm 在 docker 容器启动时启动。如果我手动进入容器并使用/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.-
我用这个命令在我的 ansible 文件中尝试了它(它没有用)。我也尝试使用服务模块,但没有运气。-
- name: Start php fpm
command: /usr/sbin/php-fpm
How can I have php-fpm running along with apache?
如何让 php-fpm 与 apache 一起运行?
回答by olibiaz
You should use supervisor
in order to launch several services
您应该使用supervisor
以启动多项服务
In your dockerfile, install supervisor, then you launch
在您的 dockerfile 中,安装 supervisor,然后启动
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
并且您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
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.
当然,您应该适应您的路径和 php-fpm 版本以及您的服务(在我的示例中为 nginx,为您提供 apache 等...),但基本上 supervisor 是从一个起点管理多个服务启动的最佳方式。
Here you can find the official doc of docker about supervisor
在这里你可以找到docker关于supervisor的官方文档
回答by helvete
I needed similar thing recently. For alpinelinux images it sufficed to run both php-fpm
and a web server as the container command. Defined in Dockerfile
somewhat like this:
我最近需要类似的东西。对于alpinelinux 映像php-fpm
,将 Web 服务器和 Web 服务器作为容器命令运行就足够了。定义Dockerfile
有点像这样:
CMD /usr/bin/php-fpm -D; nginx
ie. to daemonize php-fpm
and then run nginx
in foreground.
IE。守护进程php-fpm
,然后nginx
在前台运行。
On ubuntu/debianimages there is also necessary to allowstarting recently installed packages by running a Dockerfile
RUN
command like this:
在ubuntu/debian映像上,还需要通过运行如下命令来允许启动最近安装的软件包Dockerfile
RUN
:
RUN echo "exit 0" > /usr/sbin/policy-rc.d
and then restart the php-fpm
within a CMD
command
然后php-fpm
在CMD
命令中重新启动
CMD /etc/init.d/php7.0-fpm restart && nginx -g "daemon off;"
More on policy-rc.d
to be found in this askubuntu question
在这个 askubuntu 问题中policy-rc.d
可以找到更多信息
回答by ckeeney
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
在前台运行,因此它可能是 docker 容器中的 PID 1。解决办法是
php-fpm -F -R
Explanation
解释
We can check the available options with php-fpm --help
我们可以检查可用的选项 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:
如果您php-fpm
在 docker 容器中运行,则很有可能以 root 身份运行该进程。php-fpm 不会在没有额外标志的情况下以 root 身份启动:
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)
回答by Gavin Tremlor
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:
我使用 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"
And this is one of the rc.local files from /etc
这是来自 /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
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.
这样做是使用图像“myimage/name”启动一个名为“mycontainer”的容器,并暴露端口 80 和 443。一旦启动,它就会调用 mysql、php5-fpm 和 nginx 来启动,在每个之间暂停 5 秒钟。通过这种方式,您可以调用要在该容器内运行的任何服务来启动。不过请记住为这些服务添加开放端口。
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.
此运行命令还会每 100 秒将“Hello World”作为“脉冲”附加到您的日志文件中,让您知道它何时运行以及何时停止。
回答by bxbxckx
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.
我相信上面@ckeeney 的答案可以被接受为正确答案,但我无法在我的特定情况下使用它。
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
我已经能够通过哑 init 代理 PID1,并使用以下命令守护 PHP-FPM:https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.htmldumb-init /usr/sbin/php-fpm7.2 -F -R