使用docker和卷安装的crontab文件不起作用
我正在使用Docker测试一个CRON容器,目前正在工作,但是如果我想更改crontab文件,这是非常僵化的。我必须删除容器/图像,然后重建,以便拾取
我一直在播放的新的crontab更改,以将crontab文件安装在Windows主机上,但没有应用。如果我打开Docker CLI并键入“ crontab /etc/cron.d/crontab,
。
可以
# 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
则
cron:
build:
context: .
dockerfile: CRON.Dockerfile
# run crond as main process of container
entrypoint: [ "bash", "-c", "cron -f"]
volumes:
- ./app:/app
使用
- ./cron:/etc/cron.d
然后码头怪看起来像这样
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
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. I have to remove the container/image and then rebuild so it picks up the new crontab changes
I've been playing around with mounting the crontab file on my windows host but it doesn't get applied. If I open the docker cli and type "crontab /etc/cron.d/crontab then it works.
Any idea how to achieve this?
This is my 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
I would just add this to the docker-compose file to mount
- ./cron:/etc/cron.d
Then the dockerfile would look like this
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试编写一个脚本,在该脚本中,您将安装在容器外面的文件复制到
/etc/cron.d
内的文件中,授予该文件的权限,然后运行命令crontab copied_file .crontab
最后运行cron -f
。运行此脚本将是您的入口点。这是对我有用的解决方案,如果您想修改cron文件,只需运行该脚本的相同行,但是Skip
cron -f
因为Cron已经在运行。这将注册新文件并安装新的crontab。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 commandcrontab copied_file.crontab
and finally runcron -f
. Running this script would be your entrypoint. This is the solution which worked for meIf you want to modify the cron file, just run the same lines of that script but skip
cron -f
since cron is already running. This will register the new file and install the new crontab.