How to Build a Docker Image For Running a Cron Job

Creating Dockerfile

  • You need to replace cool_task.cron with your cron file
FROM python:3.7.9-buster
USER root
RUN apt-get update -y \
&& apt-get install cron -y
COPY cool_task.cron /etc/cron.d/cool_task.cron
RUN chmod 0644 /etc/cron.d/cool_task.cron \
&& crontab /etc/cron.d/cool_task.cron
CMD ["cron", "-f"]
*/2 * * * * bash / >> ~/cron.log 2>&1

Building the image

sudo docker build . -t your_repo/py-cron:latest

Creating docker-compose.yml

  • You need to specify your volume mount (if required)
# docker-compose.yml
version: '3'
image: your_repo/py-cron:latest
container_name: py-cron

Running docker-compose

sudo docker-compose up -d




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Automate the Boring Stuff of Starting a Django Project

CakePHP 4 How To Use Named Route Tutorial

Serverless Compute to Measure End-User Experience with AWS Lambda

Cloud Comes in Many Forms, Sizes, and Shapes

What happens when you type gcc main.c

Test your Java code with the help of Testcontainers

What most candidates are getting wrong in tech coding interviews!

SRE / DevOps / Kubernetes Weekly Collection#62(Week 14, 2021)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Poltak Jefferson

Poltak Jefferson

More from Medium

How to Expose your localhost server using Ngrok

You Release HOW OFTEN?

Generate self signed certificates with SAN using openSSL

Access Your Router Settings Via A Connected Device