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

在search这个问题时,我发现: 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"] 

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 

开始与监督员一起,你的cron作业应该被执行。 以下是可以确保cron运行的故障排除步骤

  1. cron守护进程在容器中运行吗? login到容器并运行ps a | grep cron ps a | grep cron找出来。 使用docker exec -ti CONTAINERID /bin/bashlogin到容器。

  2. supervisord运行吗?

  3. 以我的设置为例,以下pipe理员configuration没有问题。 图像是ubuntu:14.04 。 我在Dockerfile中有CMD ["/usr/bin/supervisord"]
 [supervisord] nodaemon=true [program:crond] command = /usr/sbin/cron user = root autostart = true 
  1. 尝试另一个简单的cron作业来查明问题是你的cron入口还是cron守护进程。 使用crontab -elogin容器时添加以下内容:

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

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

    docker logs CONTAINERID | grep -i cron

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

Cron没有运行,因为只有最后一个CMD覆盖了第一个CMD (就像@xuhdev所说的那样)。 这里logging: https : //docs.docker.com/reference/builder/#cmd 。

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

如果你想让nginxcron在同一个容器中运行,你将需要使用某种types的supervisor(比如supervisord或者其他),这个supervisord将会是容器的pid 1进程并且pipe理chield进程。 我认为这个项目应该有所帮助: https : //github.com/nbraquart/docker-nginx-php5-cron (它似乎正在做你想要达到的目标)。

取决于你的cron在这里,还有其他的解决scheme – 比如为每个提交或每个标签build立一个新的映像等。

我在CentOS上使用了它,它可以工作:

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

我的Dockerfile的其余部分只是yum安装cronie并触及/var/log/cron文件,所以当CMD运行时它将在那里。