How to Run Cron Jobs inside Docker Containers

Jonathan Baldie
Oct 16, 2016 · 1 min read

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/task.sh

While you’re here, please check out my new book Social Intelligence, now available for preorder. Learn how to solve disagreements with people, defeat bullies, and become a beloved leader. Gain new insights into human nature and group psychology.

Original article:

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

* * * * * /some/cool/task.sh
# 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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store