如何在 docker 容器内运行 cron 作业 [英] How to run a cron job inside a docker container
问题描述
我尝试在 docker 容器中运行 cron 作业,但对我没有任何作用.
我的容器只有 cron.daily
和 cron.weekly
文件.crontab,cron.d,cron.hourly
在我的容器中不存在.crontab -e
也不起作用.
我的容器使用 /bin/bash
运行.
I tried to run a cron job inside a docker container but nothing works for me.
My container has only cron.daily
and cron.weekly
files.
crontab,cron.d,cron.hourly
are absent in my container.
crontab -e
is also not working.
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 并构建映像并运行.由于是基于alpine的,所以图片超小.
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 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!