为什么Dockerfile中的cron服务不运行?

Bob*_*ijt 9 cron docker dockerfile

搜索此问题时,我发现:cron -f应该启动该服务.

所以我有:
RUN apt-get install -qq -y git cron

接下来我有:
CMD cron -f && crontab -l > pullCron && echo "* * * * * git -C ${HOMEDIR} pull" >> pullCron && crontab pullCron && rm pullCron

我的dockerfile没有错误地部署,但是cron没有运行.如何使用添加的行启动cron服务?

PS:
我知道我的cron中的git函数实际上应该是一个钩子,但对我来说(可能对其他人来说)这是关于学习如何用Docker设置crons :-)

PPS:
完整的Dockerfile(更新):

RUN apt-get update && apt-get upgrade -y
RUN mkdir -p /var/log/supervisor
RUN apt-get install -qq -y nginx git supervisor cron wget
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN wget -O ./supervisord.conf https://raw.githubusercontent.com/..../supervisord.conf
RUN mv ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN apt-get install software-properties-common -y && apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449 && add-apt-repository 'deb http://dl.hhvm.com/ubuntu utopic main' && apt-get update && apt-get install hhvm -y
RUN cd ${HOMEDIR} && git clone ${GITDIR} && mv ./tybalt/* ./ && rm -r ./tybalt && git init
RUN echo "* * * * * 'cd ${HOMEDIR} && /usr/bin/git pull origin master'" >> pullCron && crontab pullCron && rm pullCron
EXPOSE 80
CMD ["/usr/bin/supervisord"]
Run Code Online (Sandbox Code Playgroud)

PPPS:
Supervisord.conf:

[supervisord]
autostart=true
autorestart=true
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx -c /etc/nginx/nginx.conf

[program:cron]
command = cron -f -L 15
autostart=true
autorestart=true
Run Code Online (Sandbox Code Playgroud)

Vin*_*ter 8

Cron没有运行,因为只有最后一个CMD覆盖了第一个(如@xuhdev所说).它在此处记录:https://docs.docker.com/reference/builder/#cmd.

Dockerfile中只能有一条CMD指令.如果列出多个CMD,则只有最后一个CMD才会生效.

如果您想拥有nginxcron在同一容器中运行,您将需要使用某种类型的管理程序(如supervisord或其他),这些管理程序将成为容器的pid 1进程并管理chield进程.我认为这个项目应该有所帮助:https://github.com/nbraquart/docker-nginx-php5-cron(它似乎正在做你想要实现的目标).

根据您的cron所针对的内容,还有其他解决方案 - 比如为每个提交或每个标签构建新图像等...


小智 7

与主管一起开始crond,你的cron工作应该被执行.以下是确保cron正在运行的故障排除步骤

  1. cron守护程序是否在容器中运行?登录到容器并运行ps a | grep cron以查找.使用docker exec -ti CONTAINERID /bin/bash登录到容器中.

  2. 监督是否正在运行?

  3. 例如,在我的设置中,以下管理程序配置可以正常运行.图像是ubuntu:14.04.我CMD ["/usr/bin/supervisord"]在Dockerfile中.
[supervisord]
 nodaemon=true
[program:crond]
 command = /usr/sbin/cron
 user = root
 autostart = true
Run Code Online (Sandbox Code Playgroud)
  1. 尝试另一个简单的cron作业来找出问题是你的cron条目还是cron守护进程.使用以下命令登录容器时添加此项crontab -e:

    * * * * * echo "hi there" >> /tmp/test

  2. 检查容器日志以获取有关cron的更多信息:

    docker logs CONTAINERID | grep -i cron

这些只是您可以遵循的一些故障排除提示.