-1

Vote down!

How to run cron from docker

tl;dr: bind the cron definition file as a volume, and wire running it, as part of the image (in your Dockerfile).

Here is frmo a Stackoverflow answer:

Let's say you have a file hello-cron:

# must be ended with a new line "LF" (Unix) and not "CRLF" (Windows)
* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

Then you can build the image:

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron
 
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

Note: there is wayy too much detail about how to run a background (daemon) process in docker. it is not simple, since docker monitors only the foreground process, and a background process can fail without notice. 

Oftentimes my foreground process is apache2, so I want cron to run in the background. Hm, I do not monitor if it fails, but if I had to monitor that, additional work would be required and the above solution is not sufficient

With that said, overall it's a pretty easy and working solution, at least in a development environment, it should answer most people's needs. Cron daemon rarely fails. Besides, if you're looking for a resilient production environment, you have probably already implemented a solution that allows you monitoring processes, including background processes like cron.

Please login or register to post a comment.