带有 python 和 crontab 的 Docker 镜像

带有 python 和 crontab 的 Docker 镜像

我正在尝试使用 python 代码和 crontab(调度程序)运行一个 docker 容器,下面是我的脚本:

Docker 文件

FROM ubuntu:latest
MAINTAINER [email protected]
RUN apt-get update && apt-get install -y software-properties-common && apt-get install -y python cron vim

# Copy hello-cron file to the cron.d directory
COPY my-crontab /etc/cron.d/my-crontab

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/my-crontab

ADD config.py /
ADD index.py /

RUN chmod a+x config.py index.py
# Apply cron job
RUN crontab /etc/cron.d/my-crontab

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
#CMD cron && tail -f /var/log/cron.log
CMD ["cron", "-f"]

我的 crontab

*/2 * * * * /index.py > /dev/console

当我创建 docker 文件并运行代码时,它没有任何响应。有人能建议如何修改 docker 文件来运行脚本吗?

答案1

您可以尝试使用“docker container exec -it'container-id'bash”进入容器,然后从那里尝试执行python脚本,以此方式检查它是否有效。

从那里开始有多个场景,这取决于您在脚本中输入了什么类型的shebang(例如'#!/usr/bin/env python','#!/usr/bin/python'等),哪个版本的python安装在容器中以及脚本是为哪个版本编写的等等。没有更多信息就无法提供更多帮助。

相关内容