简体   繁体   中英

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

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

This might not be directly related, but I was getting stuck with my cronjob running in a docker container. I tried the accepted answer with no luck. I finally discovered that the editor I was using to edit the Crontab file (Notepad++) was set to Windows line-endings (CR LF). Once I changed this to Unix line endings (LF) my cronjob ran successfully in my docker container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM