2015-05-23 200 views
7

虽然searching这个问题,我发现:cron -f应该启动服务。为什么Dockerfile中的cron服务没有运行?

所以我必须:
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的功能实际上应该是一个挂钩,但对我来说(也可能是替他人),这是学习如何与泊坞窗设置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"] 

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 
+0

重复:http://stackoverflow.com/questions/20545554/how-do-i-start-cron-on-docker-ubuntu-base – Armand

+0

嗨@Armand,我找到了这个答案,但我不'了解它是如何重复的?这实际上是使我添加'cron -f'添加'CMD'的末尾的问题,但这似乎没有运行 –

+0

Docker使用单个进程启动实例。你能详细说明你开始使用哪个命令吗?我所学到的是,如果你需要更多的进程在运行,你应该考虑从命令行运行管理程序,并让它负责启用你想要的系统进程。 – Armand

回答

4

已经开始crond的与主管,应该执行cron作业。以下是您可以采取的故障排除步骤,以确保cron正在运行

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

  2. supervisord运行?

  3. 在我的设置中,下面的管理员配置没有问题。图片是ubuntu:14.04。我在Dockerfile中有CMD ["/usr/bin/supervisord"]
[supervisord] 
nodaemon=true 
[program:crond] 
command = /usr/sbin/cron 
user = root 
autostart = true 
  • 尝试另一种简单的cron作业findout问题是否是你的cron项或cron守护程序。到容器登录时crontab -e补充一点:

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

  • 检查容器日志关于cron的任何进一步的信息:

    docker logs CONTAINERID | grep -i cron

  • 这些仅仅是一些故障排除您可以遵循的提示。

    6

    Cron未运行,因为只有最后一个CMD覆盖第一个(如@xuhdev所述)。它记录在这里:https://docs.docker.com/reference/builder/#cmd

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

    如果你想拥有nginxcron在同一个容器中运行,则需要使用某种类型的上司(如supervisord或其他),这将是你的容器的PID 1过程和管理屏幕过程。我认为这个项目应该有所帮助:https://github.com/nbraquart/docker-nginx-php5-cron(它似乎在做你正在努力实现的目标)。

    取决于你在做什么cron是这里,会有其他的解决方案到 - 像或者建立一个新的图像每次提交的每个标签,等等...

    +0

    谢谢,我要检查这一点,并尽快找回结果。 –

    +0

    我已经更新了我的dockerfile与supervisord,它部署得很好,但cron不运行,任何想法? (请参阅我的更新PPS) Thx –

    +0

    可能取决于您的supervisord的配置/内容(并且supervisord必须在前台运行,带有-n标志) –

    0

    我在CentOS用这个和它的作品:

    CMD service crond start ; tail -f /var/log/cron 
    

    我Dockerfile其余的只是百胜安装cronie和接触/var/log/cron文件,因此它会在那里的CMD运行时。