【问题标题】:Docker container with cron and flask app带有 cron 和烧瓶应用程序的 Docker 容器
【发布时间】:2018-03-23 07:24:31
【问题描述】:

我正在尝试使用 cron 作业和烧瓶应用程序配置 docker 容器。

它只是不工作..

我知道每个容器只能有 1 个 CMD 命令,但是

  • RUN 服务 cron 启动
  • CMD python hello.py

它不应该工作吗?

ps:出于其他原因,我正在避免为 cron 作业创建单独的图像...

FROM python:3

RUN apt-get update && apt-get install -qq -y cron

COPY . .

# Add crontab file in the cron directory
ADD ./cron_job/crontab /etc/cron.d/cron_job

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron_job

RUN service cron start

# hello.py => flask app
CMD python hello.py

【问题讨论】:

  • 和 CMD service cron start && python hello.py ??

标签: docker cron dockerfile


【解决方案1】:

Docker 镜像不保存正在运行的进程。当您的 RUN 命令执行时,它仅在 docker build 阶段执行,并在构建完成后停止。

您需要在 CMD(入口点)中指定服务 cron 启动。

我建议创建一个脚本来处理这些任务。因为容器被设计为只运行一个进程。但是,如果您将任务包装在单个脚本中,并且脚本是入口点,您可以绕过此限制。

CMD /start.sh 

start.sh 是一个脚本,它启动你的 cron 服务,然后运行你的 python 脚本。您也可以使用 supervisord,但我认为对于像这样的简单任务,不要打扰。在大多数情况下,不用理会supervisord。

以上解释参考:https://docs.docker.com/config/containers/multi-service_container/

【讨论】:

    猜你喜欢
    • 2017-10-18
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    相关资源
    最近更新 更多