我想以分离模式在 docker 容器内运行 python cron 作业。我的设置如下:
我的python脚本是test.py
#!/usr/bin/env python
import datetime
print "Cron job has run at %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
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
这种方法有哪些潜在问题?还有其他方法吗?它们的优缺点是什么?
最佳答案
尝试在 docker 容器中运行 cron 作业时遇到的几个问题是:
- docker 容器中的时间是 UTC 而非本地时间;
- docker 环境没有传递给 cron;
- 正如 Thomas 所说,cron 日志记录还有很多不足之处,通过 docker 访问它需要基于 docker 的解决方案。
列表中有特定于 cron 的问题和特定于 docker 的问题,但无论如何都必须解决它们才能使 cron 正常工作。
为此,我目前对问题中提出的问题的工作解决方案如下:
创建一个 docker 卷,所有在 cron 下运行的脚本都将写入该卷:
# 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"]
将在 cron 下运行的脚本是 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)
为了将环境变量传递给我想在 cron 下运行的脚本,请遵循 Thomas 的建议,并在 /etc/cron.d
中为每个需要 docker 环境变量的脚本(或脚本组)放置一个 crontab 片段必须设置的占位符 XXXXXXX
。
# 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
与其直接调用 cron,不如将 cron 包装在一个 python 脚本中,该脚本执行以下操作: 1. 从 docker 环境变量中读取环境变量,并在 crontab 片段中设置环境变量。
#!/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)
运行cron作业的容器的Dockerfile
如下:
# 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"]
最后,创建容器并运行它们:
- 创建日志卷(test-logs)容器:
docker build -t test-logs .
- 运行日志量:
docker run -d -v /t-logs --name t-logs test-logs
- 创建 cron 容器:
docker build -t test-cron .
- 运行 cron 容器:
docker run --detach=true --volumes-from t-logs --name t-cron test-cron
- 检查在 cron 下运行的脚本的日志文件:
docker run -t -i --volumes-from t-logs ubuntu:latest /bin/bash
。日志文件位于/var/log
中。
关于python - 在 docker 中运行 cron python 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822067/