I'm having a strange issue when trying to run a Python script via Crontab within a Docker container. Here's the setup.
I have a Dockerfile like this:
FROM python:3-onbuild
# Install cron and dependencies
RUN apt-get update && apt-get -y install cron
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/simple-cron
WORKDIR /src
COPY . /src
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/simple-cron
RUN chmod 777 /usr/local/lib/python3.6/site-packages
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD ["cron", "-f"]
And a crontab file like this:
* * * * * root python /src/test.py > /proc/1/fd/1 2>/proc/1/fd/2
This executes correctly with no problems when I start the container. The first line of my Python file is import redis, which causes the script to fail, saying that the module cannot be found.
The interesting thing is that when I manually execute docker exec -it [container_name] python test.py, everything works exactly as expected.
What could be the issue here? I'm thinking that the requirements file isn't being installed to the right location?