Saturday, January 8, 2022

[SOLVED] Cron not runing a docker container which starts a python script

Issue

I have a docker container which loads a python image. I am then running a python script copied to the docker container via a Dockerfile like so:

docker run --net=host mybuiltimage /bin/bash -c "python src/runscript.py"

This command works directly from the terminal. For it to work via cron and output to a log file I did some changes:

* * * * * cd /dir/to/dockerfile && /usr/bin/docker run --net=host mybuiltimage /bin/bash -c "python src/runscript.py" >> /dir/to/log/mylog.log 2>&1

The change here is that I am changing directory to the Dockerfile directory and explicitly giving the full path to the docker command.

This cronjob is still not executing. mylog.log is not being created in the given directory. what is wrong with this?


Solution

It might be useful to create a script file, say /dir/to/script/myscript.sh:

#!/bin/bash

/usr/bin/docker run --net=host mybuiltimage /bin/bash -c "python src/runscript.py" >> /dir/to/log/mylog.log 2>&1

You should be able to test the script file:

$ bash /dir/to/script/myscript.sh
$ cat /dir/to/log/mylog.log

Then in the cron file:

* * * * * bash /dir/to/script/myscript.sh


Answered By - Johann-Michael Thiebaut