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

问题描述



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

我该怎么做?



编辑:



我创建了一个一个工作的docker cron容器以给定的间隔调用一个shell脚本。

解决方案

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



请参见从在他的:



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





  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

(请参阅的和:

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



构建并运行它:

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





  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:

* * * * * 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.
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
Hello world
Hello world

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

08-04 00:01
查看更多