Ofelia - cron tasks on steroids in Docker
If your looking for a simple crontab replacemnt using Docker
ofelia is “the
girl for you"with 734 stars and very little README.md.
If we were using
Kubernetes there are probebly other (or more native) solutions you may consider such as using Kubernetes Cron tasks … but if you need a small & cron replacement, without getting to much suffistacation into it + be able to migrate your
cron tasks with 1 small git repo - she’s defintelly the girl for you ;),
Side note: what I’ve seen so far is many solutions like Chronos (for apache Mesos) and AeroSpike and other tools which do batch processing which people setup to do just crons … which seems like using a 10kg hammer to do a 1kg hammers work.
So for this simple use case, my customer wanted to be able to lauch a container, which will manage all the cron definitions in a variable file (via Ansible), which of course resides in a git repo. So just under 30 minuets of reading you find a ton of utilities written in Go/Python you name it, which are blazing fast and do that job exactly … see Go as an example -> https://golanglibs.com/search?q=crontab&page=3.
Putting Ofelia to work
As gently mentioned above IMO the documentation is quite poor, but considering it is quite intuitive I can understand why this is the case.
Ofeila in is an OpenSource MIT lisenced crontab replacement container written by Máximo Cuadros (Thank you Maximo), the Ofelia container is based on
golang:1.7 and can run arbitary commands in / out of docker by configuring a simple ini file for scheduling.
As an example (taken from the repo readme) the ini file looks like the following:
As you can obviously see in the example above there are 4 job types (iv’e test only the first 3 …)
job-exec- executed and command inside of an existing & running container.
job-run- runs a command inside of a new container, using a specific image. In order to use this option your ofelia container needs to have the docker socket mapped as a volume … e.g.
docker run -d ofelia -v /var/run/docker.sock:/var/run/docker.sock
And in order to be able to pull your images from a private repo you probebly want your docker config by adding a private registry and it’s credentials add a
job-local- runs the command inside of the host/container running ofelia.
job-service-run- this was an addition from one of the contrinutors which I haent tested yet myself but it’s purpose is to run a command inside a new “temporary” service, for running inside a swarm.
In order to use the
job-exec I wipped up a small docker-compose.yml which looks like the following:
I originaly thought I should build a new image so ofelia can launch - execute then destroy, but once I got to testing this scenario I found myself pushing ‘n’ pulling more & more images (or layers) against the registry.
The limitation was, I couldn’t run a container and map volumes to it (which would have made this very intuitive …). Using
job-exec on one had leaves a container up and running when no cron tasks are, but allows me to map volumes and other stuff I might need on the other (via docker-compose).
So if you need a simple
cron replacement looks like Ofelia +
job-exec can do that for you in a few minuets you should have converted your cron scheul from e.g
* * * * * * to
schedule = * * * * *
you commnad to
command = echo "Hello World" and
container = <your_custom_container>.
As always hope you find this useful, HP