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
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 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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With