【问题标题】:How do I run a cron job in my eventstore image?如何在我的 eventstore 映像中运行 cron 作业?
【发布时间】:2018-06-04 06:34:23
【问题描述】:

我正在尝试在我的 Docker 映像中运行 cron 作业。当我使用这个Dockerfile

FROM ubuntu:latest

# Install cron
RUN apt-get update
RUN apt-get install cron

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

# Add shell script and grant execution rights
ADD script.sh /script.sh
RUN chmod +x /script.sh

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

# 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

然后它工作正常。如果我将FROM 更改为 FROM eventstore/eventstore,然后我的 cronjob 停止工作。 eventstore 是基于 ubuntu:1604 的,所以它似乎应该继续工作。有人有什么想法吗?

【问题讨论】:

    标签: docker cron


    【解决方案1】:

    假设:您希望在运行 eventstore 的同时在后台运行 cron。

    须知: 在 dockerfile 中,“CMD”部分作为参数附加到“ENTRYPOINT”部分。例如

    ENTRYPOINT ["echo","running entrypoint"]
    CMD ["echo","runnning cmd"]
    

    将产生以下输出

    running entrypoint echo running cmd
    

    您的问题的解释: 在您的 Dockerfile 中,cron 作为 CMD 执行,当您的父映像是 ubuntu:latest 时它工作正常,因为它没有定义任何入口点。而 eventstore/eventstore 定义了 ENTRYPOINT 导致执行以下操作

    /entrypoint.sh cron && tail -f /var/log/cron.log
    

    这也可能导致 eventstore 本身的意外行为,具体取决于 entrypoint.sh 的定义方式。充其量它会忽略任何参数。

    解决方案: 定义一个脚本“custom-entrypoint.sh”来运行 cron,然后是 eventstore 入口点脚本。

    #!/bin/bash
    cron && /entrypoint.sh
    

    然后定义您的 Dockerfile 以添加 custom-entrypoint.sh 并将其运行为 入口点。最终的 Dockerfile 应该类似于

    FROM eventstore/eventstore
    
    # Install cron
    RUN apt-get update
    RUN apt-get install cron
    
    # Add crontab file in the cron directory
    ADD crontab /etc/cron.d/simple-cron
    
    # Add shell script and grant execution rights
    ADD script.sh /script.sh
    RUN chmod +x /script.sh
    
    # Add custom entrypoint shell script
    ADD custom-entrypoint.sh /custom-entrypoint.sh
    RUN chmod +x /custom-entrypoint.sh
    
    # Give execution rights on the cron job
    RUN chmod 0644 /etc/cron.d/simple-cron
    
    # Create the log file to be able to run tail
    RUN touch /var/log/cron.log
    
    # Run the command on container startup
    ENTRYPOINT ["/custom-entrypoint.sh"]
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 2021-05-10
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2013-11-18
      • 2019-06-30
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多