簡體   English   中英

Cron 不在 PHP Docker 容器中運行

[英]Cron does not run in a PHP Docker container

我正在使用php:7.4-fpm Docker 圖像,我正在嘗試設置 cron 以運行但它沒有運行。

這是我的 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的內容時,它是正確的

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

但它沒有運行。 我不確定這里發生了什么..

當我運行service cron status時,它顯示[ ok ] cron is running. 但什么都沒有發生。

我有同樣的錯誤,通過在托管 docker 容器而不是容器本身的機器上設置 cron 來修復。

所以 cron 條目翻譯成如下內容:

* * * * * docker exec web php artisan schedule:run >> storage/logs/cron.log 2>&1

如果您遇到 tty 分配問題,請嘗試使用docker exec -it

所以我終於設法解決了。 我不知道為什么復制 cron 文件不起作用。 我仍然不知道為什么。 (也許比我聰明的人可以解釋)。 但是我通過將命令附加到/etc/crontab文件非常簡單地解決了它,現在它可以工作了。

PS crontab 文件末尾需要一個換行符,因此使用echo會自動添加它。

這是我更新的 Dockerfile (我刪除了復制 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM