11

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
Community
  • 1
  • 1
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • Duplicate to : http://stackoverflow.com/questions/20545554/how-do-i-start-cron-on-docker-ubuntu-base – Armand May 23 '15 at 20:27
  • Hi @Armand, I did find this answer, but I don't understand how it's a duplicate? This was actually the question that made me add `cron -f` add the end of the `CMD` but this doesn't seem to run – Bob van Luijt May 23 '15 at 20:31
  • Docker initiates an instance using a single process. Can you elaborate as in which command you started you instance with? What I learned is that if you need more processes running, you should consider running supervisor from the command line and make it responsible for enabling system processes you wish to have available. – Armand May 23 '15 at 20:34
  • mmm I'm sorry about my duplicate message, cron -f should indeed work. what do you get when running "docker logs CONTAINER" ? i'm curiouse if the cron is writing something inside the syslog. – Armand May 23 '15 at 20:55
  • won't your second CMD overrides your first one? – xuhdev May 24 '15 at 03:17
  • Aha, thanks @xuhdev that makes sense, I'm going to check it out and get back to you asap. – Bob van Luijt May 24 '15 at 08:16
  • @xuhdev I've updated my dockerfile with supervisord, it deploys well but the cron doesn't run, any thoughts? (see my update PPS in question) Thx – Bob van Luijt May 24 '15 at 20:05
  • @bvl I don't really use supervisord. I would suggest you to run cron and nginx in two containers and use a data container to share data between them. It's easier to maintain in many cases and easier to debug. – xuhdev May 24 '15 at 21:05

5 Answers5

10

Having started crond with supervisor, your cron jobs should be executed. Here are the troubleshooting steps you can take to make sure cron is running

  1. Is the cron daemon running in the container? Login to the container and run ps a | grep cron to find out. Use docker exec -ti CONTAINERID /bin/bash to login to the container.

  2. Is supervisord running?

  3. In my setup for instance, the following supervisor configuration works without a problem. The image is ubuntu:14.04. I have CMD ["/usr/bin/supervisord"] in the Dockerfile.
[supervisord]
 nodaemon=true
[program:crond]
 command = /usr/sbin/cron
 user = root
 autostart = true
  1. Try another simple cron job to findout whether the problem is your cron entry or the cron daemon. Add this when logged in to the container with crontab -e :

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

  2. Check the container logs for any further information on cron:

    docker logs CONTAINERID | grep -i cron

These are just a few troubleshooting tips you can follow.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Daniel t.
  • 965
  • 11
  • 18
9

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...

Vincent Demeester
  • 6,927
  • 3
  • 22
  • 17
1

I've used this with CentOS and it works:

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

The rest of my Dockerfile just yum installs cronie and touches the /var/log/cron file so it will be there when the CMD runs.

MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
MrMackey
  • 121
  • 2
0

On centos 7 this works for me

[program:cron]
command=/usr/sbin/crond -n -s
user = root
autostart = true
stderr_logfile=/var/log/cron.err.log
stdout_logfile=/var/log/cron.log

-n is for foreground -s is to log to stdout and stderr

pwaterz
  • 658
  • 5
  • 11
0

In my case, it turns out I needed to run cron start at run time. I can't put it in my Dockerfile nor docker-compose.yml, so I ended up placing in the Makefile I use for deploy.

Something like:

task-name:
     @ docker-compose down && docker-compose build && docker-compose up -d
     docker exec CONTAINERNAME /bin/bash -c cron start
Othaner
  • 11
  • 1