Open In App

How To Run A Cron Job Inside A Docker Container ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In the containerization world, management of resources and scaling the applications are necessary to make our services effective and user-friendly for the users. In this, the cron job helps in a significant way with facilitating features such as isolation, portability, scalability, and resource management. It will be a convenient and efficient solution for managing scheduled tasks in modern software environments. In this article, we will discuss the primary terminologies related to cron jobs and docker containers, and then guide you in the implementation of running a cron job inside the docker container step by step effectively.

Understanding Of Primary Terminologies

Cron Job: It is a scheduling task that runs the scripts or commands at specific intervals on Unix-like operation systems.

  • Docker Container: It is a lightweight standalone executable package program that can act as an operating system in the containerization world, It includes everything that is needed to run a specific piece of software such as code, runtime, libraries, and dependencies.
  • Dockerfile: It is a document or text file that contains a collection of commands or instructions to build the Docker image.
  • Docker Image: A Docker Image is a read-only template that contains instructions and software to create a docker container.
  • OCI Runtime: Open Container Initiative runtime, which is responsible for executing containers according to the OCI specification.

How To Run A Cron Job Inside A Docker Container: A Step-By-Step Guide

Step 1: Login Console

Login-Console

Step 2: EC2 Dashboard

  • After login in, you will be landed on console home, here click on EC2.
  • On clicking EC2, you will be redirected to EC2 Dashboard.
  • Then Click on Running Instance.

Console-Home

Step 3: Launch Instance

  • Click on Launch Instance and Define the configurations to the instance such as Instance name, no of instance and key pairs.

Launching-Instance

Step 4: Configure Instance

  • Define the Instance name as “my_aws_instance” , number of instance as “1” and Choose the AMI as “Amazon Linux 2” latest version. Choosing the latest version helps in providing latest softwares.

Configuring-Instance

Step 5: Configure Network Security Groups

  • Configure the Network Security Groups by clicking on edit button and configure the protocols, port number and IP address as All Traffic, ALL, Anywhere options as shown in the below screenshot. It helps in not getting any security restricts from the AWS Instance.

Note: If you want to provide the security restrict the network traffic and access you can customize here.

  • Review The Definitions and Configurations once and confirm by clicking on Launch Instance.

Configuring-Security-Groups

Step 6: Connect To EC2 Console

  • After creating the EC2 Instance wait till the instance come to the running state, then go inside it by choosing the instance and clicking on connect button.
  • Inside the EC2 Instance go through EC2 Instance Connect section and go through Connect button.

Connecting-To-EC2-Console

Step 7: Update The Software And Packages

  • After connecting to EC2 Instance Connect you will get EC2 Console to interact with instance.
  • Firstly update the software and packages with the following command:
sudo yum update -y

Updating-To-software-and-packages

Step 8: Verify The Docker Version

  • Install the docker with the following command:
sudo yum install docker -y
  • Verify the Docker Installation with the following command:
docker --version
  • After installing enable the docker service with the following command:
sudo systemctl enable docker --now
  • Verify the Docker Service state with the following command:
sudo systemctl status docker
  • As shown in screenshot, we verify the docker service, checking with running the following command:
sudo docker images

Verifying-the-Docker-Version-

Step 9: Configure Script File

  • Now, we will go on creating files such as requirements.txt, mycron_script.sh, Dockerfile.
  • Here Firstly we creating mycron_script.sh file with the following code:
#!/bin/bash

#This is a sample cron job script
echo "Cron job is running at $(date)" >> /var/log/my_cron_job.log

Configuring-Script-file

Step 10: Configure Dockerfile

  • Configure the Dockerfile with the following code:
FROM python:3.9

#Update and Install cron
RUN apt-get update && apt-get install -y cronm

#Copy The Cron Job Script Into The Container
COPY mycron_script.sh /etc/cron.d/my_cron_job_container

#Give Exection rights on the cron job
RUN chmod 0644 /etc/cron.d/my_cron_job_script

#Create the log file to be able to run tail
RUN touch /var/log/my_cron_job.log

#Run the cmmand on container startup
CMD cron && tail -f /var/log/my_cron_job.log

Configuring-Dockerfile

Step 11: Docker Build And Docker Container

  • Build the docker image from the Dockerfile with the following command:
sudo docker build -t mycron_img:v1 .
  • Run a docker container with container mycron_job1 with the following command:
docker run -dit --name mycron_job1 mycron_img:v1

Docker-Container

Step 12: Running Docker Execution Inside

  • On running the following command you can see the created container is running properly, it can be seen in the screenshot.
docker ps 
  • Enter inside the docker container with the following command:
docker exec -it mycron_job1 bash
  • The following screenshot illustrates the successful copy of the cron job code.

Running-Docker-Execution-Inside

Step 13: Monitoring The Cron Job

  • You can monitor the running cron job inside the docker container with the following command and the following screenshot illustrates it clearly.
docker logs -f mycron_job1

Monitoring-The-Cron-Job

Running A Cron Job And Docker Container – FAQ’s

What is a cron job, and why would I want to run it inside a Docker container?

It is a scheduling task and on running it inside the docker container provides the protability, isolation and consistency across the environements.

How do I create a cron job inside a Docker container?

You can create a cron job inside a docker container by Installing the cron packages and defining the task in cronjob file at time of Configuring the Dockerfile.

How can I debug issues with my cron job inside a Docker container?

On checking the logs of your cron job inside the docker container you can understand and debug the issues with your cron job.

Can I schedule multiple cron jobs inside a single Docker container?

Yes, It is possible to schedule multiple cron jobs inside a docker container by adding multiple entries to your cron job file.

What are some best practices for running cron jobs inside Docker containers?

On ensuring the your Docker image containing only the necessary dependencies will be best practice to handle the logging and ensuring reliability in cron job setup.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads