I am trying to add cronjob and flask server in same docker image. My Dockerfile looks like this
FROM python:alpine3.9
RUN apk --update add build-base libffi-dev openssl-dev curl
ADD crontab.txt /crontab.txt
ADD cronscript.sh /cronscript.sh
COPY entry.sh /entry.sh
RUN chmod 755 /cronscript.sh /entry.sh
RUN /usr/bin/crontab /crontab.txt
RUN /bin/sh /entry.sh
WORKDIR /
RUN mkdir app
COPY requirements.txt /app
RUN pip install -r /app/requirements.txt
COPY *.py /app/
WORKDIR /app
RUN chmod +x main.py
ENTRYPOINT [ "python" ]
CMD [ "main.py" ]
cronscript.sh
#!/bin/sh
echo "Starting cronjob"
curl http://localhost:4000/health
echo "Completed cronjob"
crontab.txt
*/15 * * * * /cronscript.sh >> /var/log/cron.log
entry.sh
#!/bin/sh
# start cron
/usr/sbin/crond -l 2
This does not start cronjob. I see no data in /var/log/cron.log
after waiting for an hour.
Where as if I move the line /bin/sh /entry.sh
in my flask main function. Everything works.
flask main function
if __name__ == "__main__":
os.system("/bin/sh /entry.sh")
app.run(host='0.0.0.0', port=4000, debug=True)
Is there a way I can get this cron job working from Dockerfile itself and not python code? This feels like a hack.