在Docker中处理cron作业? [英] Handling cron jobs in docker?

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

问题描述

人们通常如何使用docker处理cron作业?我看到的最常见的情况是有一个sidekick图像运行只是crond和代码库,但是使用cronie时,我无法读入在docker命令行传递的任何环境变量。

How are people generally handling cron jobs with docker? The most common case I've seen is there will be a sidekick image running just crond and the code base, however when using cronie I'm not able to read in any environmental variables that are passed in on the docker command line.

具体来说,我将这样做:

Specifically I'll do this:

docker run -d --name cron -e VAR1=val1 -e VAR2=val2 cron_image start

这个:

[root@dae7207bf10e /]# yum info cronie
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ash.fastserv.com
 * epel: mirror.cs.pitt.edu
 * extras: mirror.vcu.edu
 * updates: mirror.us.leaseweb.net
Installed Packages
Name        : cronie
Arch        : x86_64
Version     : 1.4.11
Release     : 13.el7
Size        : 211 k
Repo        : installed
Summary     : Cron daemon for executing programs at set times
URL         : https://fedorahosted.org/cronie
License     : MIT and BSD and ISC and GPLv2+
Description : Cronie contains the standard UNIX daemon crond that runs specified programs at
            : scheduled times and related tools. It is a fork of the original vixie-cron and
            : has security and configuration enhancements like the ability to use pam and
            : SELinux.

[root@dae7207bf10e /]# cat /usr/local/bin/start
#!/bin/bash
/usr/bin/env > /var/tmp/docker_env
/usr/sbin/crond -n

将如下所示:

SHELL=/bin/bash
5 16 * * * source /var/tmp/docker_env; /usr/local/bin/randomchallenge &> /var/log/randomchallenge.log

原来我没有源位,直接使用变量,但是它看起来不像cronie给他们称为工作(这在大多数使用情况下是有意义的)。我试图拉入这个env文件多种方式没有运气,我的程序永远不能读取变量。甚至包装整个东西在一个shell脚本拉env不能做这项工作。

Originally I didn't have the source bits at all and tried to use the variables directly however it doesn't look like cronie presents them to called jobs (which does make sense in the vast majority of use cases). I've tried pulling in this env file a variety of ways without luck, my program can never read the variables. Even wrapping the whole thing in a shell script that pulls in env doesn't do the job.

人们如何处理这种事情?硬编码值不是一个选项。

How are people handling this kind of thing? Hard coding values is not an option. I suppose I could make the start script generate the crontab on the fly but that seems really ugly.

推荐答案

Cron在docker world()中,由于任何原因)似乎在标准的Linux环境中比其他设施收到较少的爱。我发现它不是很明显如何正确做。

Cron in docker world (due to whatever reasons) seem to have received lesser love compared to other facilities in a standard Linux environment. I found it not very obvious how to do it correctly.

这是我的问题和解决方案。查看 docker-vixie-cron 及其docker图片 redmatter / cron ,看看它是否有助于您的场景。

Here is my take on the problem and a solution for it. Have a look at docker-vixie-cron and its docker image redmatter/cron to see if it helps your scenario. It did take a bit of trial and error to arrive at the current solution, but please feel free to air your thoughts.

这与你所做的有很大的不同, cronie ;这里是如何。在您的项目中,您必须添加具有以下行的 Dockerfile 和您的cron定义的 crontab.txt

It is quite different to what you have done with cronie; here is how. In your project you have to add the Dockerfile that has the below lines and a crontab.txt with your cron definition.

FROM redmatter/cron

ADD randomchallenge /usr/local/bin/



crontab.txt



crontab.txt

*/1 * * * * /usr/local/bin/randomchallenge >>/var/log/randomchallenge.log 2>&1

如果您要使用不同的用户 root (因为你有另一个容器共享 cron 容器),那么你可以另外定义 RUN_USER = another.user 然后使用内置脚本 cron-user add 添加用户;

If you want to use a different user to root (say because you have another container sharing the cron container), then you can additionally define RUN_USER=another.user and then add the user using a built-in script called cron-user add; as in the below version of Dockerfile.

FROM redmatter/cron

ENV RUN_USER=another.user
RUN cron-user add -u another.user

ADD randomchallenge /usr/local/bin/



运行



可以使用以下命令运行容器。

Run

In both cases you can run the container using the command as below.

docker run -d --name cron \
-e PRESERVE_ENV_VARS="VAR1 VAR2" \
-e VAR1=val1 -e VAR2=val2 \
cron_image start

这里必须指定 PRESERVE_ENV_VARS =VAR1 VAR2,以便 VAR1 VAR2 保留 randomchallenge 以查看

Here it is important to specify PRESERVE_ENV_VARS="VAR1 VAR2" so that VAR1 and VAR2 are preserved for randomchallenge to see.

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

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