Docker的Cron容器 - 他们是如何工作的? [英] Cron containers for docker - how do they actually work?

查看:234
本文介绍了Docker的Cron容器 - 他们是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用docker几个月了,我正在处理各种不同的服务器镜像。一个一致的问题是许多服务器需要运行cron作业。有很多关于在线(包括在Stackoverflow)的讨论,但我不完全理解它的机制。

I have been using docker for a couple of months now, and am working on dockerizing various different server images. One consistent issue is that many servers need to run cron jobs. There is a lot of discussion about that online (including on Stackoverflow), but I don't completely understand the mechanics of it.

目前,我使用主机的cron并将docker exec放入每个容器中运行一个脚本。我创建了一个关于脚本的名称和位置的约定;所有的容器都有相同的脚本。

Currently, I am using the host's cron and docker exec into each container to run a script. I created a convention about the script's name and location; all my containers have the same script. This avoids having the host's cron depending on the containers.

基本上,每分钟一次,主机的cron会这样做:

Basically, once a minute, the host's cron does this:

for each container
   docker exec -it <containername> /cronscript/minute-script

这很有效,但是使容器依赖主机。

That works, but makes the containers depend on the host.

我想要做的是创建一个cron容器,启动每个其他容器中的脚本 - 但我不知道等同于docker exec的工作从一个容器到另一个。

What I would like to do is create a cron container that kicks off a script within each of the other containers - but I am not aware of an equivalent to "docker exec" that works from one container to the other.

我现在的具体情况是在MySQL容器中运行备份,并运行cron作业Moodle需要运行每分钟。最终,还需要通过cron做更多的事情。 Moodle使用命令行PHP脚本。

The specific situations I have right now are running a backup in a MySQL container, and running the cron jobs Moodle requires to be run every minute. Eventually, there will be additional things I need to do via cron. Moodle uses command-line PHP scripts.

从另一个容器中的一个容器中启动脚本的正确docker化方法是什么?

What is the "proper" dockerized way to kick off a script from one container in another container?

更新:也许有助于提及我的具体使用情况,但随着时间的推移,会更多。

Update: maybe it helps to mention my specific use cases, although there will be more as time goes on.

目前,cron需要执行以下操作:

Currently, cron needs to do the following:


  • 从MySQL执行数据库转储。我可以通过mysqldump TCP链接从cron容器;这里的缺点是我不能限制备份用户主机127.0.0.1。我也可以通过一个卷以某种方式finagle MySQL套接字到cron容器。

  • 在Moodle安装上执行定期维护。 Moodle包括一个运行所有维护任务的php命令行脚本。这是我的biggie。我可以 通过一个卷来运行这个脚本,但是Moodle并没有考虑到这种情况,我不排除竞争条件。此外,我不希望我的moodle安装在一个卷,因为它使更新容器更难(记住,在Docker中,卷更新容器时,不会重新初始化一个新的图像)。

  • 未来:在其他多个服务器上执行例行维护,例如清除电子邮件队列等。

  • Perform a database dump from MySQL. I can do that via mysqldump TCP link from a cron container; the drawback here is that I can't limit the backup user to host 127.0.0.1. I might also be able to somehow finagle the MySQL socket into the cron container via a volume.
  • Perform regular maintenance on a Moodle installation. Moodle includes a php command line script that runs all of the maintenance tasks. This is the biggie for me. I can probably run this script through a volume, but Moodle was not designed with that situation in mind, and I would not rule out race conditions. Also, I do not want my moodle installation in a volume because it makes updating the container much harder (remember that in Docker, volumes are not reinitialized when you update the container with a new image).
  • Future: perform routine maintenance on a number of other of my servers, such as cleaning out email queues, etc.

推荐答案

我的解决方案是:


  • 在容器中安装crond


  • 运行cron作为守护进程

  • 运行您的软

  • install crond inside container
  • install Your soft
  • run cron as a daemon
  • run Your soft

我的 Dockerfile的一部分

FROM debian:jessie

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY .crontab /usr/src/app

# Set timezone
RUN echo "Europe/Warsaw" > /etc/timezone \
    && dpkg-reconfigure --frontend noninteractive tzdata

# Cron, mail
RUN set -x \
    && apt-get update \
    && apt-get install -y cron rsyslog mailutils --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

CMD rsyslogd && env > /tmp/crontab && cat .crontab >> /tmp/crontab && crontab /tmp/crontab && cron -f



说明



Description


  1. 设置时区,因为cron需要正确的运行任务

  2. 安装 cron li>
  3. 安装 rsyslog 包以记录cron任务输出

  4. 安装 mailutils package如果您要从cron任务发送电子邮件

  5. 运行 rsyslogd

  6. 将ENV变量复制到tmp文件,因为cron使用最少ENV和您的任务运行任务可能需要访问容器ENV变量

  7. 附加您的 .crontab
  8. code 运行cron守护程序 >
  1. Set timezone, because cron need this to proper run tasks
  2. Install cron package - package with cron daemon
  3. Install rsyslog package to log cron task output
  4. Install mailutils package if You want to send e-mails from cron tasks
  5. Run rsyslogd
  6. Copy ENV variables to tmp file, because cron run tasks with minimal ENV and You tasks may need access to containers ENV variables
  7. Append Your .crontab file (with Your tasks) to tmp file
  8. Set root crontab from tmp file
  9. Run cron daemon

我在我的容器中使用它,工作得很好。

I use this in my containers and work very well.

如果你喜欢这个范例,那么对每个cron任务做一个 Dockerfile 。例如

If You like this paradigm, then make one Dockerfile per cron task. e.g.


  • Dockerfile - 主程序

  • Dockerfile_cron_task_1 - cron任务1

  • li>
  • Dockerfile - main program
  • Dockerfile_cron_task_1 - cron task 1
  • Dockerfile_cron_task_1 - cron task 2

并构建所有容器:

docker build -f Dockerfile_cron_task_1 ...

这篇关于Docker的Cron容器 - 他们是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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