我有一個dockerfile,可以自動創建映像。Docker,dockercloud和Cron
我使用碼頭雲,連接到數字海洋作爲服務器。
在我的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命令的要求是什麼? – jaxxstorm
謝謝Frap。我懂了。我正在使用cron,因爲我有一系列的網絡爬蟲,我想在不同的時間運行(每個小時,每天都在特定的時間等)。我認爲這將在Dockerfile中啓動'RUN crontab -u root cron_files.txt'其中cron_files.txt包含實際運行事件的cron命令 – LearningSlowly
'RUN'執行一次性操作,主要用於設置容器在運行操作之前。看到我的答案傳入。 – jaxxstorm