Search code examples
crondockerdockerfile

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

Solution

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