Docker容器添加定时任务

from–https://stackoverflow.com/questions/37015624/how-to-run-a-cron-job-inside-a-docker-container

from–https://forums.docker.com/t/how-to-run-a-cron-job-inside-a-container-alpine/7759/8

from–https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container

这是我运行我的cron容器之一的方法。

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,然后构建映像并运行。由于它基于高山,因此图像非常小。

上一篇
下一篇