如何在docker容器中运行cron作业? [英] How to run a cron job inside a docker container?

查看:793
本文介绍了如何在docker容器中运行cron作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在调用shell脚本的docker容器中运行cronjob。



昨天我一直在搜索整个网络和堆栈溢出,但我无法真正找到一个有效的解决方案。

我该如何做?



编辑:

我创建了一个(已注释)github存储库

解决方案

您可以将crontab复制到一个图像中,



请参阅使用Docker运行cron作业,来自 Julien Boulay 在他的 Ekito / docker-cron


code>来描述我们的工作。




  * * * * * root echo Hello world>> /var/log/cron.log 2>& 1 
#对于有效的cron文件,在此文件的结尾处需要一个空行。




以下DockerFile描述了构建图像的所有步骤




  FROM ubuntu:latest 
MAINTAINER docker@ekito.fr

RUN apt-get update&& apt-get -y install cron

#在cron目录中添加crontab文件
ADD crontab /etc/cron.d/hello-cron

#执行cron作业的权限
RUN chmod 0644 /etc/cron.d/hello-cron

#创建能够运行尾的日志文件
RUN touch / var / log / cron.log

#在容器启动时运行命令
CMD cron&& tail -f /var/log/cron.log

(请参阅 Gaafar 注释如何使 apt-get 安装更少的噪音?

apt-get -y install -qq --force-yes cron 也可以工作)



构建并运行它:

  sudo docker build --rm -t ekito / cron-example。 
sudo docker run -t -i ekito / cron-example




请耐心等待2分钟,您的命令行应显示:




  Hello world 
Hello world


I am trying to run a cronjob inside a docker container that invokes a shell script.

Yesterday I have been searching all over the web and stack overflow, but I could not really find a solution that works.
How can I do this?

EDIT:

I've created a (commented) github repository with a working docker cron container that invokes a shell script at given interval.

解决方案

You can copy your crontab into an image, in order for the container launched from said image to run the job.

See "Run a cron job with Docker" from Julien Boulay in his Ekito/docker-cron:

Let’s create a new file called "crontab" to describe our job.

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

The following DockerFile describes all the steps to build your image

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# 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

(see Gaafar's comment and How do I make apt-get install less noisy?:
apt-get -y install -qq --force-yes cron can work too)

Build and run it:

sudo docker build --rm -t ekito/cron-example .
sudo docker run -t -i ekito/cron-example

Be patient, wait for 2 minutes and your commandline should display:

Hello world
Hello world

这篇关于如何在docker容器中运行cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆