How to Run Cron Jobs inside Docker Containers

Update September 2017:

You are way better off running Docker cron jobs from your host system, not from inside your Docker containers. Your Docker containers should only have one concern only, and not be saddled with the weight of cron jobs.

Use this instead on your host system’s crontab:

* * * * * docker run --rm your-container /some/cool/

Original article:

In the same folder as your Dockerfile, create a file called crontab:

* * * * * /some/cool/
# Mandatory blank line

Then add the following to your Dockerfile:

COPY crontab /etc/cron.d/cool-task
RUN chmod 0644 /etc/cron.d/cool-task
RUN service cron start

Rebuild the Docker image, and you’re all set!

I have a quick question for you now, since you’re probably a developer if you’re reading this.

Are you making the most common mistakes that most developers make in SQL queries? Want my 2-step algorithm on the perfect index?

Download my free report, MySQL Worst Practices.