When it comes to scheduling jobs and programs that automatically run at set intervals or can be triggered by another event, you have plenty of options. You can use a general-purpose utility like cron, the built-in scheduler in macOS or Linux, or a specialized tool like AWS Lambda. Cron, though not as powerful as AWS Lambda, is a solid choice if you’re using containers because it’s designed to handle background tasks on Unix systems. With Docker, however, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to work.
How to Dockerize a Cron Job
To run a cron job inside a Docker container, you will need to use the cron service and run it in the foreground in your Docker container.
Here’s an example of how you can set this up:
- Create Cron File:
Create a file that contains all the cron jobs to be run under the Docker container. For example, let’s call this file
`cron`
. - Create Dockerfile
Next, create a `Dockerfile` that installs the cron service and copies the script into the container. Here we have provided 3 examples of Dockerfile using the different-2 operating system. The Alpine Linux is very lightweight and is sufficient for running cronjobs. But if you need to set up other environments Linux Ubuntu, Apache, and PHP. choose any of the below given Dockerfile templates based on your requirements.
- Dockerfile with Alpine Linux12345678910111213141516FROM alpine:3# Copy cron file to the containerCOPY cron /etc/cron.d/cron# Give the permissionRUN chmod 0644 /etc/cron.d/cron# Add the cron jobRUN crontab /etc/cron.d/cron# Link cron log file to stdoutRUN ln -s /dev/stdout /var/log/cron# Run the cron service in the foregroundCMD [ "crond", "-l", "2", "-f" ]
- Dockerfile with Apache and PHP1234567891011121314151617181920FROM php:8.0-apache# Install cronRUN apt update && \apt -y install cron# Copy cron file to the containerCOPY cron /etc/cron.d/cron# Give the permissionRUN chmod 0644 /etc/cron.d/cron# Add the cron jobRUN crontab /etc/cron.d/cron# Link cron log file to stdoutRUN ln -s /dev/stdout /var/log/cron# Start cron serviceRUN sed -i 's/^exec /service cron start\n\nexec /' /usr/local/bin/apache2-foreground
- Dockerfile with Ubuntu Linux12345678910111213141516171819FROM ubuntu:latest# Install cron deamonRUN apt update && apt install -y cron# Copy cron file to the containerCOPY cron /etc/cron.d/cron# Give the permissionRUN chmod 0644 /etc/cron.d/cron# Add the cron jobRUN crontab /etc/cron.d/cron# Link cron log file to stdoutRUN ln -s /dev/stdout /var/log/cron# Run the cron service in the foregroundCMD ["cron", "-f"]
- Dockerfile with Alpine Linux
- Build and Run Container
You have two files in your current directory. One is `cron` that contains the cronjobs and the second is Dockerfile that have the build instructions for Docker. Run the following command to build the Docker image using the Dockerfile:
docker build -t my_cron .
Once the image is successfully built, You can launch a container using the image:
docker run -d my_cron
This will start the cron daemon under the container, which will all the scheduled jobs defined in `cron` file.
- Test Setup
The cronjobs should be successfully configured with the docker containers. As we have linked cron log file (/var/log/cron) to the /dev/stdout. Show all the logs generated by the cron service can be seed in
`docker logs`
command.First, find the container id or name using
`docker ps`
command.docker ps
Then check the log files of the Docker container.
docker logs container_id
In the cronjobs, I have printed the current date and written them to logs. You can see the below image that shows the current date in logs every minute.
Running Cronjobs in Docker It means the cron jobs are running properly under the Docker container.
cat cron
Our example file looks like:
cron* * * * * echo "Current date is `date`" > /var/log/cron
Add all of the crontab jobs in the above file that needs to be run under the Docker container.
Wrapping Up
Cron jobs are a handy way to automate daily tasks on your computer, like backing up files. With Docker, though, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to run. When running a cron job in a docker container, you’ll run into a few challenges depending on how and where you want to run the container. The cron job interval is specified in seconds and can range from one minute to just over 100 years. The cron job frequency is specified in terms of the number of times the cron job should be executed every day.
When it comes to scheduling jobs and programs that automatically run at set intervals or can be triggered by another event, you have plenty of options. You can use a general-purpose utility like cron, the built-in scheduler in macOS or Linux, or a specialized tool like AWS Lambda. Cron, though not as powerful as AWS Lambda, is a solid choice if you’re using containers because it’s designed to handle background tasks on Unix systems. With Docker, however, things get a little trickier because you can’t just launch a new cron instance from your terminal and expect it to work.