Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the cron service in Dockerfile run?

While searching for this issue I found that: cron -f should start the service.

So I have:
RUN apt-get install -qq -y git cron

Next I have:
CMD cron -f && crontab -l > pullCron && echo "* * * * * git -C ${HOMEDIR} pull" >> pullCron && crontab pullCron && rm pullCron

My dockerfile deploys without errors but the cron doesn't run. What can I do to start the cron service with an added line?

PS:
I know that the git function in my cron should actually be a hook, but for me (and probably for others) this is about learning how to set crons with Docker :-)

PPS:
Complete Dockerfile (UPDATED):

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
like image 331
Bob van Luijt Avatar asked May 23 '15 20:05

Bob van Luijt


People also ask

Does Cron work in docker?

Since the definition of each cron job allows you to execute commands, you can use Docker Engine in the same way you would the command line.

Does docker run automatically pull?

We could have skipped the docker pull step; if you use the docker run command and you don't already have a copy of the Docker image, Docker will automatically pull the image first and then run it.

How do I run a Dockerized project?

The easiest way to deploy a Dockerized application on a remote server is to transfer the application's image with docker pull and then use docker run . This runs the application in a container similar to how you'd do it in your development environment.

What is run instruction in Dockerfile?

RUN - RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.


1 Answers

Cron is not running because only the last CMD overrides the first one (as @xuhdev said). It's documented here : https://docs.docker.com/reference/builder/#cmd.

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

If you want to have nginx and cron running in the same container, you will need to use some kind of supervisor (like supervisord or others) that will be the pid 1 process of your container and manage the chield processes. I think this project should help : https://github.com/nbraquart/docker-nginx-php5-cron (it seems to do what you're trying to achieve).

Depending on what you're cron is here for, there would be other solution to that — like building a new image for each commit or each tags, etc...

like image 156
Vincent Demeester Avatar answered Sep 20 '22 06:09

Vincent Demeester