0

Dockerfile:

FROM python:3.9


WORKDIR /mydir

# Set desired Python version
ENV python_version=3.8 \
        PYTHONPATH="/mydir/smart-git/smart-datascience/python_daemons/Virtual/app:/mydir/smart-git/smart-datascience/data/load/:/mydir/smart-git/smart-datascience/Andrew/Monitoring kalibratora/":$PYTHONPATH
RUN env

ENV TZ=Europe/Berlin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezoneA

COPY requirements.txt requirements.txt
USER root
RUN pip3 install --user -r requirements.txt

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

ADD mydir-crontab /etc/cron.d/mydir-crontab

RUN chmod 0644 /etc/cron.d/mydir-crontab

RUN crontab /etc/cron.d/mydir-crontab


CMD cron && python3 /mydir/smart-git/smart-datascience/python_daemons/Virtual/app/app.py

Crontab file:

20 * * * * /usr/local/bin/python3 /mydir/smart-git/smart-datascience/python_daemons/Virtual/app/jsonification.py

The app.py script works without any issues. However, the jsonification.py doesn't. I've tested jsonification.py for over 10 days of continuous runtime and it worked without issues so I'm assuming I messed up something with docker/crontab.

EDIT: For additional context, jsonification.py runs some processing on data and then stores it to a json file (creates a new one if it doesn't exist and appends if it does).

Jamess11
  • 114
  • 6
  • 1
    Your container just exits as soon as "jsonification.py" is completed. (Here's why the suggestion in Alez answer works). So, does this answer your question? [How to run a cron job inside a docker container?](https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) – STerliakov Jun 06 '23 at 12:05

1 Answers1

1

You're invoking 2 Python scripts: one inside the cronjob and one with the last CMD command of the Dockerfile. Leave it only inside the cronjob.

So, replace the last line of the Dockerfile with

CMD cron && tail -f /var/log/cron.log

in order to execute the cron-job on container startup and see its logs.

Alez
  • 1,913
  • 3
  • 18
  • 22
  • It's not the same script though. I'm invoking `app.py` in the last line of the Dockerfile and I'm invoking `jsonification.py` inside the cronjob. Unless I misunderstood. – Jamess11 Jun 06 '23 at 12:12
  • You're right. I updated slightly my answer to be more precise. Btw the solution remains the same. – Alez Jun 06 '23 at 12:30
  • Thank you for your answer but I don't understand where I should put the line that runs the `app.py` then? That script should be running continuously inside the docker container and `jsonification.py` should run as per the cron rule. Sorry for bothering you. – Jamess11 Jun 06 '23 at 20:45
  • 1
    Nevermind, I had an import bug all along. – Jamess11 Jun 07 '23 at 09:41