Docker + Cron + Application (PHP, Python, Java etc)

Rishabh Dua
The Desk of Rishabh
Nov 16, 2020

There are multiple approaches to dockerize your cron along with your application code. Let us first start with the problem statement.

Problem Statement

You decide to Dockerize your application, to ease deployments, development etc etc (benefits of docker). Great! But your application uses a cron job. You want to just continue running your cron but bundle it with your docker image and think of distributing this later!

Now you have multiple processes — cron process & your application process running in docker reliably.

Assumptions

  1. Assuming you have basic understanding docker constructs
  2. You have cron file
  3. You are ok with single point of failure i.e. the docker process where cron runs.

Solution

  1. We need to ensure that cron & your application process both are always running.
  2. If docker process restarts, the cron & application process starts with it.

Using Supervisor to manage processes

In this supervisor configuration, we have two programs configured “cron” & “php-fpm”. You can replace the php-fpm with your application command.

Adding Supervisor to Docker Compose file

You can use any image your choice. Few things to look for are:

  1. supervisor & cron are installed.
  2. Entrypoint is pointed to the supervisord command.

There are other alternatives as well, in which you add the cron & your process applications as part of a bash file and put that in Entrypoint.

The problem with that is uptime monitoring is not guaranteed and you don’t have levers to restart just one process.

--

--