Running A Cronjob Inside a Docker Container In 5 Steps

Table of contents

Running cron jobs inside Docker containers is a common DevOps practice, especially for automated backups, log rotations, or scheduled tasks in microservices environments. In this guide, you’ll learn how to configure and test a cron job step-by-step inside a Docker container using Ubuntu, along with a sample Dockerfile and script. This guide is ideal for developers, DevOps engineers, and SREs looking for a hands-on, repeatable setup.

Prerequisites

  • Docker should be installed on your system.
  • You should have an account on GitHub.

Steps to Run a Cron Job Inside a Container

Below is a step-by-step guide to create a Cron Job inside a Docker Container and execute the custom tasks periodically.

1. Create a Script File

First, create a script file as per your requirement, which commands or tasks you have to run, and save it as a .sh file. For example:

#!/bin/bash
touch /var/log/cron.log
crontab /etc/cron.d/container_cronjob

Save the file as script.sh.

2. Create a Cron Job File

Now, create a cron file and write the cron job details inside it. Here’s a sample cron job that runs every minute:

* * * * * echo "The test cron ran at $(date)" > /proc/1/fd/1 2>/proc/1/fd/2
💡
Note: An empty line at the end of the cron file is required for it to be valid.

3. Create a Dockerfile

Create a Dockerfile to install the necessary tools and copy the files. Here’s a sample:

FROM ubuntu:18.04
MAINTAINER Nivesh_goyal
RUN apt-get update && \
    apt-get install cron -y && \
    apt-get install vim -y
COPY ./files/cronjob /etc/cron.d/container_cronjob
COPY ./files/script.sh /script.sh
RUN chmod +x /script.sh
CMD ["/bin/bash", "-c", "/script.sh && chmod 644 /etc/cron.d/container_cronjob && cron && tail -f /var/log/cron.log"]
💡
Note: Place your cron job file and script in the same directory as the Dockerfile.

4. Build the Docker Image

Build the Docker image using the following command:

docker build --tag cronjob:testing .

5. Run the Docker Container

Spin up the container using:

docker run -itd --name container_name cronjob:testing

How to Verify Cron Job Execution

To confirm if your cron job is running, use the following command:

docker logs --tail 100 -f container_name

Conclusion: What’s Next

That’s all about how you can run a cron job inside a Docker container. Read the next blog to learn how to deploy a Cron Job inside a Kubernetes cluster using Devtron’s CI/CD tool.

Commonly Asked Questions

Can you run a cron job in a Docker container?

Yes. You can configure a cron job inside a Docker container by installing cron, writing a job file, and starting the cron daemon in your Dockerfile.

What is the best base image to use for cron jobs in Docker?

Ubuntu and Alpine are popular choices. Ubuntu is beginner-friendly, while Alpine is lightweight but may require extra packages for cron.

How do I check if my Docker cron job ran successfully?

Use docker logs <container_name> to view cron output. Make sure your cron job writes to stdout or a log file that you can tail.

Can I schedule Kubernetes cron jobs using Devtron?

Yes. Devtron supports Kubernetes-native cron jobs through its CI/CD pipeline configurations

Visibility Guide
visibility-guide

Related articles

Related articles