繁体   English   中英

使用 docker 和卷安装的 crontab 文件不起作用

[英]Using docker and volume mounted crontab file not working

我正在使用 docker 测试一个 cron 容器,目前它正在工作,但如果我想更改 crontab 文件,它会非常不灵活。 我必须删除容器/图像然后重建以便它获取新的 crontab 更改

我一直在尝试在我的 windows 主机上安装 crontab 文件,但它没有得到应用。 如果我打开 docker cli 并输入“crontab /etc/cron.d/crontab 然后它工作。

知道如何实现这一目标吗?

这是我的 dockerfile:

# installing cron package
RUN apt-get update && apt-get -y install cron dos2unix tzdata && \
    find /etc/cron.d -type f -print0 | xargs -0 dos2unix

# Set Timezone
ENV TZ="Europe/London"

# installing PHP mysqli extension to talk to MySQL
RUN docker-php-ext-install mysqli

# creating the log file that will be written to at each cron iteration
RUN touch /var/log/cron.log

# copy the crontab in a location where it will be parsed by the system
COPY /cron/crontab /etc/cron.d/crontab
# owner can read and write into the crontab, group and others can read it
RUN chmod 0644 /etc/cron.d/crontab

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

docker-compose

    cron:
        build:
            context: .
            dockerfile: CRON.Dockerfile
        # run crond as main process of container
        entrypoint: [ "bash", "-c", "cron -f"]                    
        volumes:
            - ./app:/app

我只是将其添加到 docker-compose 文件中以挂载

- ./cron:/etc/cron.d

然后 dockerfile 看起来像这样

FROM php:fpm

# installing cron package
RUN apt-get update && apt-get -y install cron dos2unix tzdata && \
    find /etc/cron.d -type f -print0 | xargs -0 dos2unix

# Set Timezone
ENV TZ="Europe/London"

# installing PHP mysqli extension to talk to MySQL
RUN docker-php-ext-install mysqli

# creating the log file that will be written to at each cron iteration
RUN touch /var/log/cron.log

您可以尝试编写一个脚本,将安装在容器外部的文件复制到/etc/cron.d中的文件,授予此类文件权限,然后运行命令crontab copied_file.crontab最后运行cron -f 运行此脚本将是您的入口点。 这是对我有用的解决方案

如果您想修改 cron 文件,只需运行该脚本的相同行,但跳过cron -f ,因为 cron 已经在运行。 这将注册新文件并安装新的 crontab。

暂无
暂无

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

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