When you want to schedule jobs and programs to run automatically at certain times or when something else happens, you have many options. You can use a common tool like cron, which is built into macOS and Linux, or a special tool like AWS Lambda. Cron is not as powerful as AWS Lambda, but it works well for background tasks on Unix systems, especially if you are using containers. However, with Docker, it’s a bit more complicated because you can’t simply start a new cron job 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 Linux
FROM alpine:3 # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Run the cron service in the foreground CMD [ "crond", "-l", "2", "-f" ]
- Dockerfile with Apache and PHP
FROM php:8.0-apache # Install cron RUN apt update && \ apt -y install cron # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Start cron service RUN sed -i 's/^exec /service cron start\n\nexec /' /usr/local/bin/apache2-foreground
- Dockerfile with Ubuntu Linux
FROM ubuntu:latest # Install cron deamon RUN apt update && apt install -y cron # Copy cron file to the container COPY cron /etc/cron.d/cron # Give the permission RUN chmod 0644 /etc/cron.d/cron # Add the cron job RUN crontab /etc/cron.d/cron # Link cron log file to stdout RUN ln -s /dev/stdout /var/log/cron # Run the cron service in the foreground CMD ["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.
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 useful way to automate daily tasks on your computer, like backing up files. However, using cron with Docker is more complicated because you can’t simply start a new cron job from your terminal and expect it to run. When running a cron job in a Docker container, you might face some challenges depending on how and where you want to run the container. The time interval for a cron job can be set in seconds and can range from one minute to just over 100 years. The frequency of the cron job is set by how many times it should run each day.
For scheduling jobs and programs to run automatically at specific times or when something else happens, you have many options. You can use a general tool like cron, which comes with macOS and Linux, or a special tool like AWS Lambda. Although cron is not as powerful as AWS Lambda, it works well for background tasks on Unix systems, especially if you are using containers. However, with Docker, it is a bit more complicated because you can’t just start a new cron job from your terminal and expect it to work.