Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron job not running inside docker container on ubuntu

I have a simple Dockerfile as follows

FROM ubuntu:latest  ADD crontab /etc/cron.d/test-cron  RUN chmod a+x /etc/cron.d/test-cron RUN touch /var/log/cron.log  CMD cron && tail -f /var/log/cron.log 

and the content of crontab file is as simple as

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1 # empty line 

When I run this on my local OS X machine (with docker-machine running), it works fine ("Hello world" is printed to log file every minute). However, when I try to run it on an Ubuntu machine, the cron job does not run (empty log file).

Here's the command I use to run the container

docker build -t crontest . docker run --name cron crontest 

I am not sure why this would be the case. I wonder if something is wrong with the Ubuntu box that I have (wrong time setting?). I have tried to restart that machine to no effect. I currently do have other docker containers running on the Ubuntu box and they're running fine.

Any suggestion on what I could do to debug/ fix this would be hugely appreciated.

EDIT:

After going inside the container (docker exec -it cron /bin/bash), I can verify that cron is running there:

root@a2ad451af8d9:/# ps -ef | grep cron root         1     0  0 20:15 ?        00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log root         6     1  0 20:15 ?        00:00:00 cron root         7     1  0 20:15 ?        00:00:00 tail -f /var/log/cron.log root        25    11  0 20:21 ?        00:00:00 grep --color=auto cron 
like image 437
Tri Nguyen Avatar asked Sep 30 '15 17:09

Tri Nguyen


People also ask

Can you run cron in a Docker container?

One way to create scheduled tasks for your containers is by using the host's crontab. 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.

Why cron job is not working?

Why is crontab not working in your system? Crontab might fail for a variety of reasons: The first reason is that your cron daemon might not be working for any reason, resulting in your crontab failing. There also exists a possibility that your system's environment variables are not settled correctly.


2 Answers

Install rsyslog inside the container with apt-get install rsyslog and launch it with the command rsyslogd before starting cron with cron -L15 (maximum logging). Then watch the file /var/log/syslog inside the container to see the cron daemon's own log output. It will tell you if there was a problem parsing your crontab and it will, in your case, log an entry every minute similar to the below if it has registered and is trying to run your job.

CRON[16]: (root) CMD (echo "Hello world" >> /var/log/cron.log 2>&1)

like image 110
Dzamo Norton Avatar answered Oct 10 '22 14:10

Dzamo Norton


I had similar issue, specifically with Ubuntu 14.04. To debug, I tried running cron in foreground, and found it to emit System error messages while trying to run the scheduled jobs.

Apparently, its a known issue with --net=host parameter (ref: https://github.com/moby/moby/issues/5899). I tried passing --pid=host as suggested, and with that, the cron jobs started running fine.

like image 38
Ameya Avatar answered Oct 10 '22 15:10

Ameya