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:

  1. 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`.

  2. 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

    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

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      FROM php:8.0apache

      # 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 startnnexec /’ /usr/local/bin/apache2foreground

    • Dockerfile with Ubuntu Linux

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      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”]

  4. 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.

  5. 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 a Cronjob Inside Docker: A Beginner’s Guide cron Docker Dockerfile
    Running Cronjobs in Docker

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