本文介绍了如何在Docker容器中运行Cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在docker容器中运行cron作业
I tried to run a cron job inside a docker container
但是对我没有任何作用
我的容器只有cron.daily和cron.weekly文件
my container have only cron.daily and cron.weekly file
crontab,cron.d,cron.hourly ...在我的容器中不存在
crontab,cron.d,cron.hourly ... are absent in my container
crontab -e也不起作用
crontab -e also not working
我的容器与/bin/bash一起运行
my container runs with /bin/bash
推荐答案
这是我运行我的cron容器之一的方法.
Here is how I run one of my cron containers.
Dockerfile:
FROM alpine:3.3
ADD crontab.txt /crontab.txt
ADD script.sh /script.sh
COPY entry.sh /entry.sh
RUN chmod 755 /script.sh /entry.sh
RUN /usr/bin/crontab /crontab.txt
CMD ["/entry.sh"]
crontab.txt
*/30 * * * * /script.sh >> /var/log/script.log
entry.sh
#!/bin/sh
# start cron
/usr/sbin/crond -f -l 8
script.sh
#!/bin/sh
# code goes here.
echo "This is a script, run by cron!"
像这样构建
docker build -t mycron .
像这样运行
docker run -d mycron
添加您自己的脚本并编辑crontab.txt,然后构建映像并运行.由于它是基于高山的,因此图像非常小.
Add your own scripts and edit the crontab.txt and just build the image and run. Since it is based on alpine, the image is super small.
这篇关于如何在Docker容器中运行Cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!