0

This is a funny stackover flow question, because I have an answer, but the answer is a few years old. I can't find much content which is new, yet it seems like it would be quite high profile.

I am using docker-compose to start a few containers. Two of them use standard postgres and redis images. The others are django 2.2.9 (and celery) This is a development environment, and I start them with docker compose, like this:

  command: ./manage.py runserver 0.0.0.0:80

docker-compose stop sends a SIGINT. The redis and postgres containers exit quickly. the django containers don't. docker-compose stop loses patience and kills them.

(and pycharm has infinite patience currently, and doesn't send a kill until I force it).

This post from 2015 referring to Django 1.9 (http://blog.lotech.org/fix-djangos-runserver-when-run-under-docker-or-pycharm.html) says that

"The quick fix is to specifically listen for SIGINT and SIGTERM in your manage.py, and sys.kill() when you get them. So modify your manage.py to add a signal handler:"

and it says how. The fix to change manage.py to catch SIGINT works and it's a handful of lines, although it doesn't work for celery which has its own startup. So I can carry forward my own version of of manage.py and fix celery, but really is this still how to fix this?

I see the the dockerfile could have

STOPSIGNAL SIGINT

but it doesn't make and difference, I suppose because the entry point is managed by docker-compose.

Tim Richardson
  • 6,608
  • 6
  • 44
  • 71
  • ah. Answered here: https://stackoverflow.com/a/52046161/401226 there is a docker-compose equivalent to STOPSIGNAL – Tim Richardson Feb 06 '20 at 10:49
  • As a side note, if this is going into production you don't want to listen on port 80 because that means running as root, and running as root isn't secure: https://pythonspeed.com/articles/root-capabilities-docker-security/ – Itamar Turner-Trauring Feb 06 '20 at 12:05
  • in production I don't use docker compose, it's run by supervisor, and it's behind nginx proxying to gunicorn – Tim Richardson Feb 07 '20 at 03:57

1 Answers1

0

Use the list variant of command:

command: ["./manage.py", "runserver", "0.0.0.0:80"]

See https://hynek.me/articles/docker-signals/ for details why.

Itamar Turner-Trauring
  • 3,430
  • 1
  • 13
  • 17