WittCode💻

How to Run a Cron Job Inside a Docker Container

By

Learn how to run a cron job inside a Docker container. We will also go over what a crontab is, the crontab format, and how to run cron in the foreground.

Table of Contents 📖

Creating the Crontab

To begin, lets create a cron job that will print the current date to a log file at every minute.

* * * * * root date >> /var/log/cron.log
# New line character required!

INFO: Cron requires that each entry in a crontab ends with a new line character.

We will create this cron.log file later on with Docker volumes. Also, it is important to note that this crontab must include the user field or it will not run. This is why we include the root user at the beginning.

Creating the Docker Image

To create the Cron job, we will use Ubuntu as the base image.

FROM ubuntu

Now lets import our crontab into our Docker image and change its permissions.

COPY crontab /etc/cron.d/my-cron
RUN chmod 644 /etc/cron.d/my-cron
  • Copy over the crontab into the /etc/cron.d directory. Cron reads the crontabs placed in this directory.
  • Make the cron job readable and writable by the owner while read only to the group and others.

INFO: Every user on the system has their own set of cron jobs. A list of cron jobs for a user is their crontab.

Now, it is important to note that cron isn't installed on the Ubuntu Docker image by default. Therefore, we need to install it in our Dockerfile.

RUN apt update
RUN apt install -y cron

INFO: Note that the -y flag provided to apt install is for non-interactive mode.

Now we simply need to run cron.

ENTRYPOINT cron -f

INFO: Both the ENTRYPOINT and CMD instructions are executed when a Docker container is created from an image. However, the instructions supplied to CMD can be overwritten while the instructions supplied to ENTRYPOINT can not.

It is important that we run cron in the foreground in the Docker container with the -f flag. This is so the Docker container keeps running while the cron service is running. Also, if the cron service ever stopped so would the Docker container.

Creating the Docker Container

Now lets build our Docker image, create a container from it, and create our log file using a volume.

docker build -t cron-i . && docker run --name cron-c -v ./cron.log:/var/log/cron.log cron-i

Now, at every minute, check the cron.log file on our host computer to see the logs being generated.

Thu May  9 15:19:01 UTC 2024
Thu May  9 15:20:01 UTC 2024
Thu May  9 15:21:01 UTC 2024
Thu May  9 15:22:01 UTC 2024
Thu May  9 15:23:01 UTC 2024
Thu May  9 15:24:01 UTC 2024

We can see how our cron job is running at every minute. Now lets navigate into our docker container.

docker exec -it cron-c bash

As we have cron running in the foreground, we can see our container exit if we stop cron.

service cron status  
* cron is running
service cron stop  
* Stopping periodic command scheduler cron

Our Docker container will now exit and we will see Terminated logged to the console.

Terminated