【发布时间】:2019-04-13 13:19:42
【问题描述】:
我在尝试通过 Docker 容器中的 Crontab 运行 Python 脚本时遇到了一个奇怪的问题。这是设置。
我有一个像这样的 Dockerfile:
FROM python:3-onbuild
# Install cron and dependencies
RUN apt-get update && apt-get -y install cron
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/simple-cron
WORKDIR /src
COPY . /src
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/simple-cron
RUN chmod 777 /usr/local/lib/python3.6/site-packages
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD ["cron", "-f"]
还有一个像这样的 crontab 文件:
* * * * * root python /src/test.py > /proc/1/fd/1 2>/proc/1/fd/2
当我启动容器时,它可以正确执行,没有问题。我的Python文件第一行是import redis,导致脚本失败,说找不到模块。
有趣的是,当我手动执行docker exec -it [container_name] python test.py 时,一切都按预期运行。
这可能是什么问题?我认为需求文件没有安装到正确的位置?
【问题讨论】:
-
你在哪里安装你的需求?