Docker 如何在eventstore映像中运行cron作业?
我试图在Docker映像中运行cron作业。当我使用这个Dockerfile时 那么它工作得很好。如果我从更改为 从eventstore/eventstore,则我的cronjob停止工作。eventstore基于ubuntu:1604,因此它似乎应该继续工作。有人有什么想法吗?假设:您希望在运行eventstore时在后台运行cron 需要知道的事情: 在dockerfile中,CMD部分作为参数附加到ENTRYPOINT部分。比如说Docker 如何在eventstore映像中运行cron作业?,docker,cron,Docker,Cron,我试图在Docker映像中运行cron作业。当我使用这个Dockerfile时 那么它工作得很好。如果我从更改为 从eventstore/eventstore,则我的cronjob停止工作。eventstore基于ubuntu:1604,因此它似乎应该继续工作。有人有什么想法吗?假设:您希望在运行eventstore时在后台运行cron 需要知道的事情: 在dockerfile中,CMD部分作为参数附加到ENTRYPOINT部分。比如说 ENTRYPOINT ["echo","running e
ENTRYPOINT ["echo","running entrypoint"]
CMD ["echo","runnning cmd"]
将导致以下输出
running entrypoint echo running cmd
对你的问题的解释:
在Dockerfile中,cron作为CMD执行,当父映像为ubuntu:latest时,它可以正常工作,因为它没有定义任何入口点。然而,eventstore/eventstore定义了入口点,这将导致执行以下操作
/entrypoint.sh cron && tail -f /var/log/cron.log
根据entrypoint.sh的定义,这也可能导致eventstore本身出现意外行为。充其量它会忽略任何论点
解决方案:
定义一个脚本custom-entrypoint.sh以运行cron,后跟eventstore entrypoint脚本
#!/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"]
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"]