How to configure a script as cronjob in Docker container

Docker container required some specific configurations to run cronjobs. This article explains the detailed steps to configure a script as cron in Docker container

Step1: Create the script file that need to run periodically. Here we are taking the example file name as

This is a sample script to print the system date.

echo `date`

Step2: Create file.

Entrypoint file is a script file that comes into action when a docker run command is issued. So, all the steps that we want to run can be put in this script file.

echo "* * * * * / >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > scheduler.txt
crontab scheduler.txt
cron -f

This cronjob will run every minutes and we need to change that schedule according to the requirements. The file scheduler.txt handling all the cronjobs within Docker container. Don't forget to add an extra new line, as it makes it a valid cron just like in the above example.

Step3: Create Docker file.

FROM ubuntu:16.04

# Install cron
RUN apt-get update && apt-get install -y cron

# Add files

RUN chmod +x / /


Note that this is a basic sample Docker file. This need to be changed according to the requirements.

Step4: Build the Docker image and run the container.

That's all…

Leave a Reply

Your email address will not be published. Required fields are marked *