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 script.sh.
This is a sample script to print the system date.
#!/bin/bash echo `date`
Step2: Create entrypint.sh 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.
#!/bin/bash echo "* * * * * /script.sh >> /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 ADD run.sh /script.sh ADD entrypoint.sh /entrypoint.sh RUN chmod +x /script.sh /entrypoint.sh ENTRYPOINT /entrypoint.sh
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.