I have a Ruby on Rails app in a Docker container on Ubuntu 14.04. I set up my deployments using Dokku but I'm unsure how to get my cron jobs working correctly.
Currently I'm using the whenever gem which allows me to do something simple like:
every 5.minutes do
runner 'MyModel.run_something'
end
The problem is I think that every time I deploy using git push dokku master
it resets the container and setting it back to it's default thus removing all my cron schedules.
So then I thought maybe the cron scheduling needs to be outside the container and at the VM level instead.
I currently don't see any cron jobs running no matter what I do. Here's what happens when I run crontab -l
when ssh'd:
root@dashboard:~# crontab -l
no crontab for root
I'm pretty new to container virtualization so I apologize if I've skipped over a critical part of this but I'm a stumped.
Since containers use Linux-based images, you can use cron to assign scheduled tasks to them. To do this, you need to create a crontab that contains the necessary information for the cron daemon to know when to run cron jobs.
The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.
* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute of every hour of every day of every month, each day of the week.
Took me forever to work this one out - ended up call the rails command instead via crontabs. I've also got a rails app uploaded on dokku with ubuntu on the digital ocean server. Trying to get the Whenever gem to work... it just doesn't. whenever -i
doesn't work.
Whenever doesn't actually create any new crontabs for the dokku environment. It's good for figuring out the Cron syntax though!
So this is how I got scheduled tasks to work in dokku:
sudo crontab -e
which will open it up in vi/vimYou can use sudo crontab -r
to remove it, or sudo crontab -l
to view current crontabs
The below code will execute every 1 minute.
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
* * * * * /bin/bash -c 'dokku run appname rails r MyModel.run_something'
Make your environmental variables are equivalent to ones you have in the env
command
grep CRON /var/log/syslog
to see the output log for trouble shooting. You may have to install postfix via sudo apt-get install postfix
in order for Cron to send mail notification of errors otherwise you may get “(CRON) info (No MTA installed, discarding output)”
error from the syslog.
cat /var/mail/root
to view the mail received from Cron - stating errors if a cronjob fails to work.
Hopefully that's helpful. That's what got me through at least!
Whenever probably isn't working because the cron daemon isn't running in your Docker/Dokku container. Docker will only run the processes it is told to, using either a CMD
or RUN
directive or in a script executed by one of those directives.
The Dokku guys have explicitly said that cron is not supported in Dokku, though without saying why. A quick search for cron
in the Dokku, Buildstep and Dokku base image repos brings up no results, so it seems to be the case that Dokku never starts a cron service when building/running an app.
The solutions they suggest are to either set up the cron job on the host machine (as you've already figured out), use a web_based scheduling service, or try Heroku's Scheduler.
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