[英]How can I start php-fpm in a Docker container by default?
我有这个 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 工作正常。
我用这个命令在我的 ansible 文件中尝试了它(它没有用)。 我也尝试使用服务模块,但没有运气。-
- name: Start php fpm
command: /usr/sbin/php-fpm
如何让 php-fpm 与 apache 一起运行?
您应该使用supervisor
来启动多项服务
在您的 dockerfile 中,安装主管,然后启动
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
当然,您应该适应您的路径和 php-fpm 版本以及您的服务(在我的示例中为 nginx,为您提供 apache 等),但基本上,supervisor 是从一个起点管理多个服务的启动的最佳方式。
在这里你可以找到docker关于supervisor的官方文档
我来这里是为了寻找如何在前台运行php-fpm
,以便它可以是 docker 容器中的 PID 1。 解决方案是
php-fpm -F -R
我们可以使用php-fpm --help
检查可用的选项
-F, --nodaemonize
force to stay in foreground, and ignore daemonize option from config file
如果您在 docker 容器中运行php-fpm
,那么您很有可能以 root 身份运行该进程。 如果没有额外的标志,php-fpm 不会以 root 身份启动:
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)
我最近需要类似的东西。 对于alpine linux 镜像,运行php-fpm
和 web 服务器作为容器命令就足够了。 在Dockerfile
中定义有点像这样:
CMD /usr/bin/php-fpm -D; nginx
IE。 守护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 问题
如果您想在同一个容器中运行php-fpm
和Apache
(通常使用 MPM 事件),您可能会发现此配置很有用:
[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
我使用 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
这样做是使用图像“myimage/name”启动一个名为“mycontainer”的容器,并暴露端口 80 和 443。 一旦启动,它会调用 mysql、php5-fpm 和 nginx 来启动,每个之间暂停 5 秒。 通过这种方式,您可以调用任何您希望在该容器内运行的服务来启动。 请记住为这些服务添加开放端口。
此运行命令还将每 100 秒将“Hello World”作为“脉冲”附加到您的日志文件中,让您知道它何时运行以及何时停止。
我相信上面@ckeeney 的答案可以被接受为正确答案,但我无法让它与我的特定案例一起工作。
我已经能够通过dumb-init代理PID1,并使用以下命令守护PHP-FPM: dumb-init /usr/sbin/php-fpm7.2 -F -R
https://engineeringblog.yelp.com/2016 /01/dumb-init-an-init-for-docker.html
这适用于我在 Docker 上使用 nginx 和 php-fpm 运行 Ubuntu 20.04。
CMD /etc/init.d/php7.4-fpm start -F && nginx -g "daemon off;"
不需要主管或 cron 作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.