Jetdi
Jetdi

Reputation: 35

Python Cron Job in Docker Container

I have three files which is

and docker file basically to install the pip and to run crontab on certain folder.

My question is : Why in my docker container, the cron just start once and Exited. have no ways to find the logs of it as it shows : Starting periodic command scheduler: cron.

i wish to know whats the proper way of setting up and how to keep it running.

Thanks

Upvotes: 1

Views: 448

Answers (1)

hansi_reit
hansi_reit

Reputation: 408

There are multiple ways on how you can run a cronjob inside a docker container. Here is an example for a cron-setup on debian using cronjob files.

Create a crontab file

  * * * * * root echo "Test my-cron" > /proc/1/fd/1 2>/proc/1/fd/2

my-cron - This file contains the interval, user and the command that should be scheduled. In this example we want to print the text Test my-cron every minute.

Create a docker entrypoint

#!/usr/bin/env bash

cron                   # start cron service

tail -f /dev/null      # keep container running

entrypoint.sh - This is the entrypoint which gets executed when the container gets started.

Create a Dockerfile

FROM debian:latest

RUN apt-get update \ 
    && apt-get install -y cron

# Cron file
ADD ./my-cron /etc/cron.d/my-cron
RUN chmod 0644 /etc/cron.d/my-cron

# Entrypoint
ADD ./entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh

CMD [ "entrypoint.sh" ]

Run

Build the image

docker build . --tag my-cron

Start a container

docker run -d my-cron:latest

Check the console output

docker logs <YOUR_CONTAINER_ID> --follow 

Upvotes: 1

Related Questions