0

So I want to run cronjob.py inside a container every week. I tried some of the codes from other posts but I can't manage to get cron to work.

This is my Dockerfile:

FROM python:3.7

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

WORKDIR /app
RUN pip3 install flask

COPY harborscan /etc/cron.d/harborscan
COPY cronjob.py cronjob.py

RUN systemctl start cron
RUN chmod 0644 /etc/cron.d/harborscan
RUN crontab /etc/cron.d/harborscan
RUN touch /app/cron.log

CMD ["cron", "-f"]

And the crontab file:

* * * * * /bin/touch /app/test >/app/cron.log 2>&1

So just for testing purposes I left the schedule to each minute and a simple touch command. The cron services is running:

root@6b6c056d0039:/app# systemctl status cron
cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service, enabled)
    Active: inactive (dead)

crontab -l :

root@6b6c056d0039:/app# crontab -l
* * * * * /bin/touch /app/test >/app/cron.log 2>&1

The test file is not created, I also tried to just run /usr/bin/python3 app/cronjob.py but the API calls that it is supposed to make aren't being executed.

  • I'm a little surprised the Dockerfile you provide builds; I'd expect `RUN systemctl ...` to fail, since Docker containers almost never run systemd (and definitely not during the build phase). You show a couple of shell prompts at the end; how do you build and run the container? [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) provides several recipes that may work for you. – David Maze Jul 26 '22 at 01:30
  • It does build with the ```RUN systemctl start cron``` but it is probably pointless. The command : ```docker build --no-cache -t test:1 . && docker run -v "$(pwd)"/vulnerabilities:/vulnerabilities --network harbor_harbor --rm --name registry_checker test:1``` – Pierrick Jul 26 '22 at 07:56

0 Answers0