Skip to content

Running a Cronjob Under Docker Container: A Beginner’s Guide

Advertisement

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:

  1. Create Cron File
  2. Create a file that contains all the cron jobs to be run under the Docker container. For example, let’s call this file cron.

    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.

  3. Create Dockerfile
  4. 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"]
      
      

  5. Build and Run Container
  6. 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.

  7. Test Setup
  8. 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
    Running Cronjobs in Docker

    It means the cron jobs are running properly 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.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

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


Back to top