Solution for running scheduled cron job within a Docker container? Solution for running scheduled cron job within a Docker container? docker docker

Solution for running scheduled cron job within a Docker container?


Scheduled tasks won't run inside of a normal container since there is no scheduler running. The only active task will be that which you have elected to run via the CMD keyword or the Entrypoint.

In order to execute schedule tasks, it's more prudent to utilize the host scheduler and docker exec commands:

docker exec <container> <command>docker exec <container> /data/myscript.sh

So you would end up with a cron on your host something like :

(Crontab Style) 0 * * * * root docker exec mycontainer /data/myscript.sh

If you have a cluster, you would have to query the cluster first to locate the container, or even have a script do it for you.


A container is meant to only one run main process. You either need to run crond as the main process for a container, or ensure that crond is running alongside your main process. This kind of breaks the contracts / point of containers, but sometimes it's easier to set it up this way. Instructions below:

My Dockerfile has the following ENTYPOINT:

ENTRYPOINT ["/startup.sh"]

And then within startup.sh I do a couple of things to spin up the container, but most importantly before executing the last command, I have this:

crondexec start_my_service

crond starts the daemon that executes the crons, and start_my_service then becomes the primary process for my container.