简体   繁体   English

如何在 php-fpm-alpine docker 容器中运行 cron 作业?

[英]How to run cron jobs inside php-fpm-alpine docker container?

Hi I don't know how can I run a cron job inside this container.嗨,我不知道如何在容器内运行 cron 作业。

I've found this: How to run a cron job inside a docker container我发现了这个: How to run a cron job inside a docker container

But that overrides the CMD, I don't know hot to keep php-fpm working但这会覆盖 CMD,我不知道如何保持 php-fpm 正常工作

When you need to run multiple processes in your docker container a solution is to use supervisord as the main instruction. 当您需要在Docker容器中运行多个进程时,一种解决方案是使用超级用户作为主要指令。 Docker will start and monitor supervisord which in turn will start your other processes. Docker将启动并监视supervisor ,后者将反过来启动您的其他进程。

Docker File Example: Docker文件示例:

FROM debian:9
...
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/my.conf"]

Supervisord config example (/etc/supervisor/my.conf): Supervisord配置示例(/etc/supervisor/my.conf):

[supervisord]
nodaemon=true

[program:cron]
command=/usr/sbin/crond -f -l 8
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
autorestart=true

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

Note that it is desirable to configure supervisord to output the logs to /dev/stdout and /dev/stderr to allow docker to handle these logs. 请注意,最好配置超级用户以将日志输出到/ dev / stdout/ dev / stderr,以允许docker处理这些日志。 Otherwise you risk your container to slow down over time as the amount of file writes increases. 否则,随着文件写入量的增加,您可能会冒险使容器随着时间的流逝而变慢。

The main question here is how to make PHP work kind of "in parallel" with the cron.这里的主要问题是如何使 PHP 与 cron “并行”工作。 And another answer, besides using supervisor , is to use bash's ability to manage tasks.除了使用supervisor之外,另一个答案是使用bash 管理任务的能力 This is generally mentioned here . 这里一般都会提到这个。
For the Alpine PHP-FPM container and the Cron , the startup script would look like this:对于Alpine PHP-FPM容器和Cron ,启动脚本如下所示:

Docker File: Docker 文件:

FROM php:8.1-fpm-alpine
RUN apk --update add --no-cache bash
COPY ./crontasks /var/spool/cron/crontabs/root
COPY entrypoint.bash /usr/sbin
RUN chmod a+x /usr/sbin/entrypoint.bash
ENTRYPOINT /usr/sbin/entrypoint.bash

entrypoint.bash file ( the magic is here ) entrypoint.bash 文件(魔法就在这里

#!/bin/bash
# turn on bash's job control
set -m
# Start the "main" PHP process and put it in the background
php-fpm &
# Start the helper crond process
crond
# now we bring the primary process back into the foreground
fg %1

It is important to keep in mind that the cron job syntax in Alpine is different from Debian.请务必记住,Alpine 中的 cron 作业语法与 Debian 不同。 And different folders are used for tasks.不同的文件夹用于任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM