【问题标题】:Docker, dockercloud and CronDocker、dockercloud 和 Cron
【发布时间】: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 ;)
  • 你有没有看过这个问题How to run a cron job inside a docker container?

标签: python docker cron docker-cloud


【解决方案1】:

您可能希望将 cron 设置为 CMD:

为此,只需在 alpine linux 上使用 crond 命令即可。

看看这个例子以获得想法:

https://gist.github.com/mhubig/a01276e17496e9fd6648cf426d9ceeec

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 2015-12-16
    • 2021-05-14
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2018-05-11
    相关资源
    最近更新 更多