【发布时间】:2016-11-22 03:14:45
【问题描述】:
我有一个 dockerfile 可以自动构建图像。
我正在使用 docker cloud,连接到 Digital Ocean 作为服务器。
在我的 dockerfile 中,我得到了我需要的软件,添加包含我希望运行的 python 脚本的相关 GitHub 存储库。然后我启动 cron 调度程序并添加适当时间的脚本。例如:
cron_files.txt 文件如下所示:
0 12 * * * /usr/bin/python /home/dir/run_check.py
0 15 * * * /usr/bin/python /home/dir/run_push.py
在我的 dockerfile 中,我执行以下操作:
RUN service cron start
RUN service cron status
RUN crontab -u root cron_files.txt
在日志文件中,我可以看到 cron 已成功启动。
编辑,感谢 r0manarmy - How to run a cron job inside a docker container?
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
如何编辑以上内容以从cron_files.txt 而不是上面的示例创建 crontab 文件?
我试过ADD crontab cron_files.txt /etc/cron.d/feeds
但这会返回:
ADD crontab cron_files.txt /etc/cron.d/feeds
lstat crontab: no such file or directory
附言。我正在使用FROM debian:jessie
【问题讨论】:
-
如果你的 Dockerfile 中没有运行 cmd,容器将永远不会真正运行。你需要 something 来运行
CMD指令。从 cron 运行命令有什么要求? -
谢谢Frap。我懂了。我正在使用 cron,因为我有一系列网络爬虫,我想在不同时间运行(每小时、每天在特定时间等)。我认为这会在 Dockerfile
RUN crontab -u root cron_files.txt中启动,其中 cron_files.txt 包含实际运行的 cron 命令 -
RUN执行一次性操作,主要用于在运行操作之前设置容器。看看我的回答。 -
谢谢。顺便提一句。检查您的个人资料。您的
.co.uk站点已降为.vo.uk;)
标签: python docker cron docker-cloud