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
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
#!/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 "$@"
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.
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,
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?
I'd run a full on second container naming the service in the compose file cron
or something of the likes (maybe more specific to the actual job in the event you have multiple crons). 1 process per container is general practice. In the "cron" container I wouldn't even run it via crond
I'd have whatever I'm using on the host machine to handle the scheduling of the container. I'd change your job from being a django-cron
to a custom django-admin
command since you won't be managing the running of it with the django app anymore. You could still build the second container off the django one image just change the CMD
with the docker-compose.yml
file command: ["django-admin", "mycommand"]
. Likely would be unnecessary to expose the ports on the second container. Invoke as a normal service docker-compose run mycronservice
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.