简体   繁体   English

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

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

I'm testing a cron container using docker, this is working at the moment but it's quite inflexible if I want to change the crontab file.我正在使用 docker 测试一个 cron 容器,目前它正在工作,但如果我想更改 crontab 文件,它会非常不灵活。 I have to remove the container/image and then rebuild so it picks up the new crontab changes我必须删除容器/图像然后重建以便它获取新的 crontab 更改

I've been playing around with mounting the crontab file on my windows host but it doesn't get applied.我一直在尝试在我的 windows 主机上安装 crontab 文件,但它没有得到应用。 If I open the docker cli and type "crontab /etc/cron.d/crontab then it works.如果我打开 docker cli 并输入“crontab /etc/cron.d/crontab 然后它工作。

Any idea how to achieve this?知道如何实现这一目标吗?

This is my dockerfile:这是我的 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 docker-compose

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

I would just add this to the docker-compose file to mount我只是将其添加到 docker-compose 文件中以挂载

- ./cron:/etc/cron.d

Then the dockerfile would look like this然后 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

You can try writing a script where you copy the file that you mounted outside the container to a file inside /etc/cron.d , give permissions to such file, then run the command crontab copied_file.crontab and finally run cron -f .您可以尝试编写一个脚本,将安装在容器外部的文件复制到/etc/cron.d中的文件,授予此类文件权限,然后运行命令crontab copied_file.crontab最后运行cron -f Running this script would be your entrypoint.运行此脚本将是您的入口点。 This is the solution which worked for me这是对我有用的解决方案

If you want to modify the cron file, just run the same lines of that script but skip cron -f since cron is already running.如果您想修改 cron 文件,只需运行该脚本的相同行,但跳过cron -f ,因为 cron 已经在运行。 This will register the new file and install the new crontab.这将注册新文件并安装新的 crontab。

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

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