Cron不在PHP Docker容器中运行 [英] Cron does not run in a PHP Docker container

查看:76
本文介绍了Cron不在PHP Docker容器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php:7.4-fpm Docker映像,并且试图将cron设置为可运行,但未运行.

I am using the php:7.4-fpm Docker image and I'm trying to set up cron to run but it's not running.

这是我的Dockerfile:

FROM php:7.4-fpm

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    cron \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    libzip-dev \
    libmcrypt-dev \
    libonig-dev \
    zlib1g-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    graphviz \
    curl \
    supervisor

# Install Imagick
RUN apt-get update && \
    apt-get install -y libmagickwand-dev --no-install-recommends && \
    pecl install imagick && \
    docker-php-ext-enable imagick

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl

# Permissions for Laravel
RUN chown -R www-data:www-data /var/www
RUN chmod -R 777 /var/www

# Copy crontab file to the cron.d directory
COPY ./docker/php-server/crontab /etc/cron.d/crontab

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab

# Apply cron job
RUN crontab /etc/cron.d/crontab

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

EXPOSE 9000

CMD bash -c "cron && php-fpm"

当我进入容器并检查/etc/cron.d/crontab 的内容时,它是正确的

When I enter the container and check the contents of /etc/cron.d/crontab it is correct

* * * * * php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1
# An empty line

但是它没有被运行.我不确定这是怎么回事.

But it's not being run. I'm not sure what's going on here..

当我运行 service cron status 时,它说 [ok] cron正在运行.但是什么也没发生.

When I run service cron status it says [ ok ] cron is running. But nothing is happening.

推荐答案

所以我终于设法解决了.我不知道为什么无法复制cron文件.我仍然不知道为什么.(也许比我更聪明的人可以解释这一点).但是我通过将命令附加到/etc/crontab 文件中来非常简单地解决了问题,现在它可以工作了.

So I finally managed to solve it. I have no idea why COPYing the cron file wasn't working. I still don't know why. (maybe someone smarter than me can explain it). But I solved it very simply by appending my commands to the /etc/crontab file and now it works.

P.S. crontab文件的末尾需要换行符,因此使用 echo 会自动添加它.

P.S. crontab file requires a new line character at the end so using echo adds it automatically.

这是我更新的Dockerfile (我删除了复制crontab的所有其他行):

Here is my updated Dockerfile (I deleted all the other lines where I copied the crontab):

RUN echo "* * * * * root php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1" >> /etc/crontab

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

这篇关于Cron不在PHP Docker容器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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