使cron在php:7-fpm映像上运行 [英] Getting cron to run on php:7-fpm image

查看:62
本文介绍了使cron在php:7-fpm映像上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手。我已经使用php:7-fpm映像设置了一个dockerfile。除了要用于运行我的网站的这张图片之外,我还想添加一个cron以便能够执行常规任务。

I'm pretty new to docker. I've set up a dockerfile using the php:7-fpm image. As well as this image being used to run my site, I want to add a cron to be able to perform regular tasks.

我创建了一个cron,放入了它在正确的文件夹中,然后运行 docker exec -ti myimage_php_1 / bin / bash 然后运行 cron 或如果我 tail 日志文件都可以正常工作。但是创建容器时我无法执行此操作,我不想明显地手动启动cron。

I've created a cron, put it in the correct folder, and running docker exec -ti myimage_php_1 /bin/bash then cron or if I tail the log file all works fine. But I can't get this to work when the container is created, I don't want to have to manually start the cron obviously.

据我了解,我需要使用 CMD ENTRYPOINT 来运行 cron 命令在启动时,但是每次执行此操作都会由于我覆盖原始php:7-fpm映像的必要 CMD / ENTRYPOINT 功能而停止我的网站。有没有办法同时触发cron命令并继续使用php:7-fpm CMD / ENTRYPOINT s?

From what I understand, I need to use CMD or ENTRYPOINT to run the cron command on startup, but every time I do this it stops my site working due to me overriding the necessary CMD/ENTRYPOINT functionality of the original php:7-fpm image. Is there a way to trigger both the cron command and continue as before with the php:7-fpm CMD/ENTRYPOINTs?

推荐答案

方法1

创建自定义entrypoint.sh,如下所示:

Create your custom entrypoint.sh, something like this:

#!/bin/bash

cron -f &
docker-php-entrypoint php-fpm

请注意& ; ,表示发送到后台。

Note the &, it means "send to background".

然后:

COPY ./entrypoint.sh /
ENTRYPOINT /entrypoint.sh






方法#2

但是,有一种更复杂的安装方法主管,请参见文档(用于docker):

But, there is a more sophisticated way that is installing supervisor, see docs (a demons manager used in docker):

在Dockerfile中:

In Dockerfile:

RUN apt-get update && apt-get install supervisor
COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
...
CMD ["/usr/bin/supervisord"]

supervisord.conf

supervisord.conf

[program:cron]
command = cron -f

[program:php]
command = docker-php-entrypoint php-fpm

一些故障排除命令:

docker exec <container-id> supervisorctl status
docker exec <container-id> supervisorctl tail -f php
docker exec <container-id> supervisorctl tail -f cron
docker exec <container-id> supervisorctl restart php

这篇关于使cron在php:7-fpm映像上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆