繁体   English   中英

如何在事件存储映像中运行cron作业?

[英]How do I run a cron job in my eventstore image?

我正在尝试在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更改为FROM eventstore/eventstore ,则我的cronjob停止工作。 eventstore区基于ubuntu:1604 ,因此它似乎应该继续工作。 有人有什么想法吗?

假设:您想在运行事件存储时在后台运行cron。

要知道的事情:在dockerfile中,“ CMD”部分作为参数附加到“ ENTRYPOINT”部分。 例如

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

将导致以下输出

running entrypoint echo running cmd

问题的解释:在Dockerfile中,cron作为CMD执行,当您的父映像为ubuntu:latest时,它工作正常,因为它没有定义任何ENTRYPOINT。 而事件存储区/事件存储区已定义ENTRYPOINT,这导致执行以下操作

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

这也可能导致事件存储库本身发生意外行为,具体取决于如何定义entrypoint.sh。 充其量它会忽略任何参数。

解决方案:定义脚本“ custom-entrypoint.sh”以运行cron,然后运行事件存储入口点脚本。

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

然后定义您的Dockerfile以添加custom-entrypoint.sh并将其作为ENTRYPOINT运行。 最终的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"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM