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

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

问题描述

我正在尝试在调用 shell 脚本的 docker 容器中运行 cronjob.

昨天我一直在网上搜索和堆栈溢出,但我真的找不到有效的解决方案.
我该怎么做?

我已经创建了一个 (已注释) github 存储库,其中包含调用的 docker cron 容器给定间隔的 shell 脚本.

解决方案

您可以将您的 crontab 复制到一个镜像中,以便从该镜像启动的容器运行该作业.

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

<块引用>

让我们创建一个名为hello-cron"的新文件.描述我们的工作.

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

如果您想知道什么是 2>&1,Ayman Hourieh 解释.

<块引用>

以下 Dockerfile 描述了构建映像的所有步骤

来自 ubuntu:latest维护者 docker@ekito.fr运行 apt-get 更新 &&apt-get -y 安装 cron# 复制hello-cron文件到cron.d目录复制你好-cron/etc/cron.d/hello-cron# 授予 cron 作业的执行权限运行 chmod 0644/etc/cron.d/hello-cron# 申请 cron 任务运行 crontab/etc/cron.d/hello-cron# 创建能够运行tail的日志文件运行触摸/var/log/cron.log# 在容器启动时运行命令CMD cron &&尾 -f/var/log/cron.log

(参见 Gaafar评论如何让 apt-get 安装噪音更小?:
apt-get -y install -qq --force-yes cron也可以)

Nathan Lloyd评论:

<块引用>

关于一个陷阱的快速说明:
如果您要添加脚本文件并告诉 cron 运行它,请记住
运行 chmod 0744/the_script
如果你忘记了,Cron 会静默失败.


或者,请确保您的作业本身直接重定向到 stdout/stderr 而不是日志文件,如 hugoShaka 中所述的答案:

 * * * * * root echo hello >/proc/1/fd/1 2>/proc/1/fd/2

将 Dockerfile 的最后一行替换为

CMD ["cron", "-f"]

另见(关于cron -f,即cron前台")docker ubuntu cron -f 不工作"


构建并运行它:

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

<块引用>

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

你好世界你好世界


Eric 添加 在评论中:

<块引用>

请注意,如果 tail 在映像构建期间创建,则可能无法显示正确的文件.
如果是这种情况,您需要在容器运行时创建或触摸文件,以便 tail 拾取正确的文件.

请参阅在 docker CMD没有显示".


在 "在 Docker 中运行 Cron 中查看更多信息"(2021 年 4 月)来自 Jason Kulatunga,因为他 在下方评论

查看 Jason 的图片 AnalogJ/docker-cron,基于:

  • Dockerfile 安装 cronie/crond,取决于发行版.

  • 初始化/etc/environment然后调用的入口点

    cron -f -l 2

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 "hello-cron" to describe our job.

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

If you are wondering what is 2>&1, Ayman Hourieh explains.

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

# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron
 
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /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)

As noted by Nathan Lloyd in the comments:

Quick note about a gotcha:
If you're adding a script file and telling cron to run it, remember to
RUN chmod 0744 /the_script
Cron fails silently if you forget.


OR, make sure your job itself redirect directly to stdout/stderr instead of a log file, as described in hugoShaka's answer:

 * * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2

Replace the last Dockerfile line with

CMD ["cron", "-f"]

See also (about cron -f, which is to say cron "foreground") "docker ubuntu cron -f is not working"


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


Eric adds in the comments:

Do note that tail may not display the correct file if it is created during image build.
If that is the case, you need to create or touch the file during container runtime in order for tail to pick up the correct file.

See "Output of tail -f at the end of a docker CMD is not showing".


See more in "Running Cron in Docker" (Apr. 2021) from Jason Kulatunga, as he commented below

See Jason's image AnalogJ/docker-cron based on:

  • Dockerfile installing cronie/crond, depending on distribution.

  • an entrypoint initializing /etc/environment and then calling

    cron -f -l 2
    

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

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