使用cron,Docker和Supervisor运行python程序

时间:2016-10-07 20:16:14

标签: python docker cron supervisord

当我运行构建容器时,CMD为CMD /usr/bin/python3 /app/test.py,一切运行正常,我看到一个输出,但当我运行CMD为CMD ["/usr/bin/supervisord", "-n"]运行supervisor和cron时,我看不到输出。好像python文件没有运行,或者我没有正确设置以查看日志记录。

如何确保python文件正在运行?如何查看输出?

我的Dockerfile是:

FROM ubuntu:16.04

RUN apt-get update
RUN apt-get install -y apt-utils -y cron -y liblapack3 -y build-essential \
                    -y python3-dev -y python3-setuptools -y python3-numpy \
                    -y python3-scipy -y python3-pip -y libatlas-dev \
                    -y supervisor


COPY . /app

# install requirements (early and on their own to leverage caching)
COPY ./requirements.txt /app/requirements.txt
RUN pip3 install -r /app/requirements.txt

# make script executable
RUN chmod +x /app/test.py

# do cron stuff
COPY ./crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab

CMD cron && tail -f /var/log/cron.log


COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# CMD ["/usr/bin/supervisord", "-n"]
CMD /usr/bin/python3 /app/test.py

supervisord.conf是:

[supervisord]
nodaemon=true
loglevel=debug

[program:cron]
command = cron -f -L 15
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

crontab是:

* * * * * root /usr/bin/python3 /app/test.py
* * * * * root echo "hello world" >> /var/log/cron.log 2>&1

和test.py是:

#!/usr/bin/env python
import logging

from lib.rig import settings

#set logging level to INFO
logging.getLogger().setLevel(settings.get('logging_level'))

logging.info('LOLOLOL')
print('this is a print')

1 个答案:

答案 0 :(得分:0)

您错过了PATH环境变量声明,导致您的Python应用无法运行。将Dockerfile编辑为以下内容可以解决您的问题:

FROM ubuntu:16.04

ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# This is the line that matters ^^

RUN apt-get update
...

详情请参阅ENV变量here