在docker中运行cron python作业 [英] Running cron python jobs within docker

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

问题描述

我想在分离模式下在docker容器中运行python cron作业。我的设置如下:



我的python脚本是test.py

 #!/ usr / bin / env python 
import datetime
printCron作业已在%s运行%datetime.datetime.now()

我的cron文件是my-crontab

  * * * * * /test.py> / dev / console 

和我的Dockerfile

  FROM ubuntu:latest 
RUN apt-get update&& apt-get install -y software-properties-common python-software-properties&& apt-get update

运行apt-get install -y python cron
ADD my-crontab /
ADD test.py /
运行chmod a + x测试。 py

RUN crontab / my-crontab
ENTRYPOINT cron -f



<这种方法的潜在问题是什么?是否有其他方法,它们的优点和缺点是什么?

解决方案

在尝试获取cron作业时遇到的几个问题在docker容器中是:


  1. docker容器中的时间是UTC不是本地时间;

  2. Docker环境没有传递给cron;

  3. 如托马斯所说,cron日志记录有很多需要,通过docker访问它需要一个基于docker的解决方案。

有cron特定的问题,并在列表中是docker特定的问题,但在任何情况下,他们必须解决cron工作。 p>

为此,我当前的问题中提出的问题的工作解决方案如下:



创建docker cron下运行的所有脚本将写入的卷:

 #测试日志的Dockerfile 

# BUILD-USING:docker build -t test-logs。
#运行使用:docker run -d -v / t-logs --name t-logs test-logs
#INSPECT-USING:docker run -t -i --volumes-from t- logs ubuntu:latest / bin / bash

从stackbrew / busybox:latest

#创建日志卷
VOLUME / var / log
$ b b CMD [true]

将在cron下运行的脚本为 test.py

 #!/ usr / bin / env python 

#python脚本需要一个环境变量并作为cron作业运行
import datetime
import os

test_environ = os.environ [TEST_ENV]
打印Cron作业已在%s运行环境变量'%s'%(datetime.datetime.now(),test_environ)

为了将环境变量传递给我要在cron下运行的脚本,请遵循Thomas的建议,并为每个需要docker的脚本(或一组脚本)放置一个crontab片段环境变量 /etc/cron.d 中设置必须设置的占位符 XXXXXXX

 #放在/etc/cron.d 
#TEST_ENV是一个docker环境变量,脚本test.py需要

TEST_ENV = XXXXXXX

* * * * * root python /test.py>> /var/log/test.log

而不是直接调用cron,将cron包装在一个python脚本做事情:1.从docker环境变量读取环境变量并在crontab片段中设置环境变量。

 # !/ usr / bin / env python 

#run-cron.py
#设置环境变量crontab片段并运行cron

import os
子进程导入调用
import fileinput

#读取docker环境变量并将其设置在适当的crontab片段中
environment_variable = os.environ [TEST_ENV]

在fileinput.input(/ etc / cron.d / cron-python,inplace = 1)中的行:
print line.replace(XXXXXXX,environment_variable)

args = [cron, - f,-L 15]
call(args)

cron作业运行所在的容器的 Dockerfile 如下:

 #BUILD-USING:docker build -t test-cron。 
#RUN-USING docker run --detach = true --volumes-from t-logs --name t-cron test-cron

从debian:wheezy

#设置正确的环境变量。
ENV HOME / root
ENV TEST_ENV test-value

RUN apt-get update&& apt-get install -y software-properties-common python-software-properties&& apt-get update

#安装Python安装程序
RUN apt-get install -y python cron

运行apt-get purge -y python-software-properties软件 - 属性 - 常见&& apt-get clean -y&&& apt-get autoclean -y&&& apt-get autoremove -y&&& rm -rf / var / lib / apt / lists / * / tmp / * / var / tmp / *

ADD cron-python /etc/cron.d/
ADD test.py /
ADD run-cron.py /

RUN chmod a + x test.py run-cron.py

#将时区设置为本地时间zone
RUN echoAmerica / New_York> / etc / timezone&&& dpkg-reconfigure --frontend noninteractive tzdata

CMD [/run-cron.py]

最后,创建容器并运行它们:


  1. 创建日志卷(test-logs)容器: docker build -t test-logs。

  2. 运行日志卷: docker run -d -v / t-logs --name t-logs test-logs

  3. 创建cron容器: docker build -t test-cron。

  4. 运行cron容器: docker run --detach = true --volumes-from t-logs --name t-cron test-cron

  5. 检查在cron下运行的脚本的日志文件: docker run -t -i --volumes-从t-logs ubuntu :latest / bin / bash 。日志文件位于 / var / log 中。


I would like to run a python cron job inside of a docker container in detached mode. My set-up is below:

My python script is test.py

  #!/usr/bin/env python
  import datetime
  print "Cron job has run at %s" %datetime.datetime.now()

My cron file is my-crontab

* * * * * /test.py > /dev/console

and my Dockerfile is

FROM ubuntu:latest
RUN apt-get update && apt-get install -y software-properties-common python-software-properties && apt-get update

RUN apt-get install -y python cron
ADD my-crontab /
ADD test.py /
RUN chmod a+x test.py

RUN crontab /my-crontab
ENTRYPOINT cron -f

What are the potential problems with this approach? Are there other approaches and what are their pros and cons?

解决方案

Several issues that I faced while trying to get a cron job running in a docker container were:

  1. time in the docker container is in UTC not local time;
  2. the docker environment is not passed to cron;
  3. as Thomas noted, cron logging leaves a lot to be desired and accessing it through docker requires a docker-based solution.

There are cron-specific issues and are docker-specific issues in the list, but in any case they have to be addressed to get cron working.

To that end, my current working solution to the problem posed in the question is as follows:

Create a docker volume to which all scripts running under cron will write:

# Dockerfile for test-logs

# BUILD-USING:        docker build -t test-logs .
# RUN-USING:          docker run  -d -v /t-logs --name t-logs test-logs
# INSPECT-USING:      docker run -t -i  --volumes-from t-logs ubuntu:latest /bin/bash

FROM stackbrew/busybox:latest

# Create logs volume
VOLUME /var/log

CMD  ["true"]

The script that will run under cron is test.py:

#!/usr/bin/env python

# python script which needs an environment variable and runs as a cron job
import datetime
import os

test_environ = os.environ["TEST_ENV"]
print "Cron job has run at %s with environment variable '%s'" %(datetime.datetime.now(), test_environ)

In order to pass the environment variable to the script that I want to run under cron, follow Thomas' suggestion and put a crontab fragment for each script (or group of scripts) that has need of a docker environment variable in /etc/cron.d with a placeholder XXXXXXX which must be set.

# placed in /etc/cron.d 
# TEST_ENV is an docker environment variable that the script test.py need

TEST_ENV=XXXXXXX
#
* * * * * root python /test.py >> /var/log/test.log

Instead of calling cron directly, wrap cron in a python script that does does things: 1. reads the environment variable from the docker environment variable and sets the environment variable in a crontab fragment.

#!/usr/bin/env python

# run-cron.py
# sets environment variable crontab fragments and runs cron

import os
from subprocess import call
import fileinput

# read docker environment variables and set them in the appropriate crontab fragment
environment_variable = os.environ["TEST_ENV"]

for line in fileinput.input("/etc/cron.d/cron-python",inplace=1):
    print line.replace("XXXXXXX", environment_variable)

args = ["cron","-f", "-L 15"]
call(args)

The Dockerfile that for the container in which the cron jobs run is as follows:

# BUILD-USING:        docker build -t test-cron .
# RUN-USING docker run --detach=true --volumes-from t-logs --name t-cron test-cron

FROM debian:wheezy
#
# Set correct environment variables.
ENV HOME /root
ENV TEST_ENV test-value

RUN apt-get update && apt-get install -y software-properties-common python-software-properties && apt-get update

# Install Python Setuptools
RUN apt-get install -y python cron

RUN apt-get purge -y python-software-properties software-properties-common && apt-get clean -y && apt-get autoclean -y && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ADD cron-python /etc/cron.d/
ADD test.py /
ADD run-cron.py /

RUN chmod a+x test.py run-cron.py

# Set the time zone to the local time zone
RUN echo "America/New_York" > /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata

CMD ["/run-cron.py"]

Finally, create the containers and run them:

  1. Create the log volume (test-logs) container: docker build -t test-logs .
  2. Run log volume: docker run -d -v /t-logs --name t-logs test-logs
  3. Create the cron container: docker build -t test-cron .
  4. Run the cron container: docker run --detach=true --volumes-from t-logs --name t-cron test-cron
  5. To inspect the log files of the scripts running under cron: docker run -t -i --volumes-from t-logs ubuntu:latest /bin/bash. The log files are in /var/log.

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

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