Currently, I have a Django wsgi to handle web traffic. (I have a nginx as front end to forward traffic to wsgi)
Now, I would like to start a crobtab service, to run periodic background job.
This is what I had done
django\Dockerfile
FROM python:3.6.4-alpine3.4
...
RUN chmod +x entrypoint.sh
ENTRYPOINT ["sh", "entrypoint.sh"]
CMD /usr/local/bin/gunicorn web.wsgi:application -b django:5000 --log-level=info --error-logfile=/var/log/gunicorn3.err.log
django\entrypoint.sh
#!/bin/sh
...
python manage.py crontab add
# https://stackoverflow.com/questions/37015624/how-to-run-a-cron-job-inside-a-docker-container
#
# "crond -help" yields:
#
# -f Foreground
# -b Background (default)
# -S Log to syslog (default)
# -l N Set log level. Most verbose:0, default:8
# -d N Set log level, log to stderr
# -L FILE Log to FILE
# -c DIR Cron dir. Default:/var/spool/cron/crontabs
# start cron
/usr/sbin/crond -f -l 8
exec "$@"
docker-compose.yml
django:
build:
context: ./django
dockerfile: Dockerfile
restart: always
depends_on:
- pgbouncer
expose:
- "5000"
volumes:
- static_data:/app/static
If I use the above setup, I notice that.
- My schedule cron worker is running periodically.
- But, wsgi unable to serve web traffic.
First, I try to change in django\entrypoint.sh
# start cron
/usr/sbin/crond -f -l 8
to
# start cron
/usr/sbin/crond -b -l 8
After making above change,
- My schedule cron worker is no longer running. Not sure why.
- wsgi can serve web traffic.
May I know why is it so? How can I make my django
container to serve web traffic, and run cron job at the same time?
Or, is it not the right way to do such thing in Docker? I should use 2 containers?