Docker Cronjob: How to Run Cron Jobs Inside a Docker Container (Step-by-Step Guide)

Table of contents
💡
Key Takeaways

1. Learn what a Docker cronjob is and why running scheduled tasks inside a container offers portability, isolation, and easier dependency management.

2. Understand how to create a Docker cronjob step by step—writing scripts, configuring cron, building the Docker image, and keeping the container alive.

3. Discover how to monitor and troubleshoot cronjobs using Docker logs, log aggregation tools, and external cron monitoring solutions.

4. Identify common issues like containers exiting or logs not updating, and learn how to fix them quickly.

5. Explore how to scale your cronjobs in production using Kubernetes CronJobs or CI/CD platforms like Devtron for automation and reliability.

What is a Docker Cronjob?

A Docker cronjob is simply a cron job running inside a Docker container. Instead of scheduling the job on the host machine, you package the cron daemon and the scripts inside a container for:

  • Portability: Runs the same in dev, staging, or production
  • Isolation: Avoids conflicts with host cron or other apps
  • Version Control: Your cron job configuration lives with your code
  • Scalability: Easy to deploy multiple scheduled jobs with Docker or Kubernetes

Why Run Cron Jobs in Docker Containers?

Running cron jobs in Docker brings several benefits for DevOps and SRE teams:

  1. Portability: Cronjobs become part of your Docker image, ensuring consistent execution across all environments.
  2. Isolation: Keeps your scheduled tasks separated from the host system.
  3. Dependency Management: All scripts, libraries, and environment variables are inside the container.
  4. Version Control & CI/CD Friendly: Track and deploy cronjob changes easily with Git.
  5. Scalability: Works seamlessly with Docker Compose or Kubernetes CronJobs for larger workloads.

How to Create a Docker Cronjob (Step-by-Step)

We’ll create a Docker cronjob that logs a message every minute.

1. Create the Cron Script

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
# This script logs a timestamped message
echo "The Docker cronjob ran at $(date)" >> /var/log/container_cron.log 2>&1

Make it executable:

chmod +x log_message.sh

2. Create a Cron Job Configuration

Create a file named container_cronjob:

* * * * * root /log_message.sh


This runs the script every minute.

3. Create a Dockerfile

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

FROM ubuntu:22.04
MAINTAINER YourName <your.email@example.com>

# Install cron
RUN apt-get update && \
    apt-get install -y cron && \
    rm -rf /var/lib/apt/lists/*

# Copy script and cron job
COPY ./files/container_cronjob /etc/cron.d/container_cronjob
COPY ./files/log_message.sh /log_message.sh

# Set permissions
RUN chmod +x /log_message.sh
RUN chmod 0644 /etc/cron.d/container_cronjob

# Start cron in foreground & keep container alive
CMD cron -f

4. Build and Run the Docker Cronjob

Build the image:

docker build -t docker-cronjob-example .

5. Run the container:

Spin up the container using:

docker run -itd --name my-cronjob docker-cronjob-example

6. Check logs:

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

docker logs -f my-cronjob

You should see:

The Docker cronjob ran at Mon Aug 4 10:00:01 UTC 2025
The Docker cronjob ran at Mon Aug 4 10:01:01 UTC 2025

How to Monitor Docker Cronjobs

  • Logs: Use docker logs for quick checks
  • Log Aggregation: Send logs to ELK, Loki, or CloudWatch
  • Health Checks: Use Docker health checks or Kubernetes probes
  • Cron Monitoring Tools: Cronitor or Healthchecks.io for alerts on job failures

Common Issues with Docker Cronjobs

  1. Container Exits Immediately
    • Use cron -f to keep cron in the foreground
  2. Cron File Not Executing
    • Ensure /etc/cron.d files have 0644 permissions
    • File must end with a newline
  3. Logs Not Updating
    1. Redirect output to /var/log and check with docker logs

Conclusion

A Docker cronjob is an easy and reliable way to schedule tasks inside a container without depending on the host system.

You’ve learned how to:

  1. Create a cron script
  2. Configure cron inside Docker
  3. Build and run the Docker container
  4. Monitor and troubleshoot your cronjob

For scalable cronjobs in production, consider Kubernetes CronJobs and CI/CD tools like Devtron to automate scheduling, monitoring, and deployment.

FAQ

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