Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cron job in docker container?

I have a python script that populates Postgres database in AWS.

I am able to run it manually and it is loading data into database without any issues. I want to run it once for every 5 minutes inside a docker container.

So I included it in docker image to run. But I'm not sure why it's not running. I can't see anything appended to /var/log/cron.log file. Can someone help me figure out why it's not running?

I am able to copy the script to image during docker build and able to run it manually. The DB is being populated and I'm getting the expected output.

The script is in current directory which will be copied to /code/ folder

Here is my code:

Dockerfile:

FROM python:3
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y cron
RUN apt-get install -y postgresql-client
RUN touch /var/log/cron.log
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD . /code/
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
CMD cron && tail -f /var/log/cron.log

crontab:

*/5 * * * * python3 /code/populatePDBbackground.py >> /var/log/cron.log
# Empty line
like image 762
Underoos Avatar asked Mar 11 '19 06:03

Underoos


People also ask

Can you run cron in Docker container?

One way to create scheduled tasks for your containers is by using the host's crontab. Since the definition of each cron job allows you to execute commands, you can use Docker Engine in the same way you would the command line.

How do I schedule a Docker container?

Using the Host's CrontabMake sure cron is installed and then edit the system's crontab as normal. Every five minutes, your system's cron installation will create a new Docker container using your app's image. Docker will execute the /example-scheduled-task.sh script within the container.

How do I run cron in foreground?

When running cron as your ENTRYPOINT in a container, make sure you start cron in foreground mode ( cron -f ) and consider redirecting your job's stdout and stderr to crons (eg. */1 * * * * root /app1/test.sh > /proc/1/fd/1 2>/proc/1/fd/2 ).


1 Answers

Crontab requires additional field: user, who runs the command:

* * * * * root python3 /code/populatePDBbackground.py >> /var/log/cron.log
# Empty line

The Dockerfile is:

FROM python:3
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y cron postgresql-client
RUN touch /var/log/cron.log
RUN mkdir /code
WORKDIR /code
ADD . /code/
COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f

Test python script populatePDBbackground.py is:

from datetime import datetime

print('Script has been started at {}'.format(datetime.now()))

And finally we get:

$ docker run -d b3fa191e8822
b8e768b4159637673f3dc4d1d91557b374670f4a46c921e0b02ea7028f40e105

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b8e768b41596        b3fa191e8822        "/bin/sh -c 'cron -f'"   4 seconds ago       Up 3 seconds                            cocky_beaver

$ docker exec -ti b8e768b41596 bash
root@b8e768b41596:/code# tail -f /var/log/cron.log
Script has been started at 2019-03-13 00:06:01.095013
Script has been started at 2019-03-13 00:07:01.253030
Script has been started at 2019-03-13 00:08:01.273926
like image 82
nickgryg Avatar answered Oct 10 '22 20:10

nickgryg