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

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

问题描述



昨天我一直在搜索整个网络和堆栈溢出,但是我找不到解决方案。

我该怎么做?



编辑:



我创建了一个(已注释)github存储库一个工作的docker cron容器以给定的间隔调用一个shell脚本。

解决方案

你可以将你的crontab复制到一个图像中命令从该图像发出的容器运行作业。



请参见使用Docker运行cron作业 Julien Boulay 在他的 Ekito / docker-cron


让我们创建一个名为 crontab 的新文件来描述我们的工作。




  * * * * * root echoHello world>> /var/log/cron.log 2& 1 
#在有效的cron文件的文件末尾需要空行。




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




  FROM ubuntu:最新
MAINTAINER docker@ekito.fr

运行apt-get update&& apt-get -y安装cron

#在cron目录中添加crontab文件
添加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运行-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

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

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