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:
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. Add all of the crontab jobs in the above file that needs to be run under the Docker container.
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” ]12345678910111213141516FROM 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 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-foreground1234567891011121314151617181920FROM 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 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”]12345678910111213141516171819FROM ubuntu:latest # Install cron deamonRUN apt update && apt install -y cron # Copy cron file to the containerCOPY cron /etc/cron.d/cron # Give the permission RUN 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”]
Once the image is successfully built, You can launch a container using the image:
This will start the cron daemon under the container, which will all the scheduled jobs defined in cron
file.
First, find the container id or name using docker ps
command.
Then check the log files of the Docker container.
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.
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.