Issue
I've been trying to set up a simple cron job in a docker container. When I build and run the job there are no errors but nothing is logged. If I go into the container I can see the crontab (i.e. crontab -l) and run the file (python test.py). Idk what I'm missing to see the scheduled job run. Idk if it's running and my log location is wrong or if it's not even running at all.
Dockerfile
FROM python:3.8.8
RUN apt-get update && apt-get -y install cron vim
WORKDIR /app
COPY crontab /etc/cron.d/crontab
COPY test.py /app/test.py
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab
# run crond as main process of container
CMD ["cron", "-f"]
crontab
* * * * * python /app/test.py > /proc/1/fd/1 2>/proc/1/fd/2
# new line
test.py
print('test')
Solution
I to reproduced your setup with a slight adjustment: I replaced your python script by a simple echo >> /crontest.txt
. It works as expected. The file is created inside the docker container and each minute one line is appended.
This leaves you only with the question why python /app/test.py > /proc/1/fd/1 2>/proc/1/fd/2
behaves different than echo >> /crontest.txt
.
Dockerfile:
FROM python:3.8.8
RUN apt-get update && apt-get -y install cron vim
WORKDIR /app
COPY crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab
# run crond as main process of container
CMD ["cron", "-f"]
crontab:
* * * * * echo "test" >> /crontest.txt
# new line
- build docker image
docker build --tag contest .
- run docker container
docker run -n crontest-container contest
- exec into the running container
docker exec -it crontest-container bash
- output content of /crontest.txt
cat /crontest.txt
(you can also check and see that crontab is runningtop
)
Answered By - Raphael