Igor's Techno Club

submited by
Style Pass
2024-05-07 21:00:17

While Docker is mainly used to containerize stateless applications, there might be cases where you need scheduled tasks inside your container. For example, quite often I use it for small automated tasks, such as site updates, database cleanups, and so on. The main reason I prefer this approach is that it becomes really easy to move it to another hosting provider whenever you find a cheaper one.

This sets /app as the working directory. The files run.sh and run-cron are copied into /app. run.sh contains the script to be executed by Cron, and run-cron is the crontab file which schedules when run.sh will be run.

The Cron configuration file is moved to /etc/cron.d and set with the correct permissions. The crontab command adds the schedule from run-cron into the Crontab.

An empty log file is created and the CMD command starts the Cron daemon along with tailing the log file. This enables the logs to be streamed to the Docker container logs, which can be very useful for debugging.

Leave a Comment