[Django]-Docker: gracefully stop django server

7đź‘Ť

âś…

In your docker-compose.yml file, set the stop_signal to SIGINT:

django:
  build: .
  command: ["./run/web.sh"]
  stop_signal: SIGINT

I struggled with this for a while myself…

When starting the runserver you’ll notice it says “Quit the server with CONTROL-C.” I took this to mean that Django’s runserver is written to handle SIGINT (ctrl-c) and not SIGTERM (the signal docker sends). For me, changing the signal Docker sends to Django was easier than trying to handle SIGTERM in my script and exit the runserver gracefully.

Note that this depends on using exec in your web.sh script so that the signals don’t get eaten by the shell – but I see you’re already doing that.

👤tenni

0đź‘Ť

I’ll leave here the config that works for me:

yml file:

backend:
restart: always
container_name: django_proj
image: username/django_proj
build:
  context: .
  dockerfile: ./docker/django/Dockerfile
command: /code/gunicorn-entrypoint.sh
expose:
  - 8000

gunicorn-entrypoint.sh which is the same thing as your sh:

#!/bin/sh
sleep 10

python manage.py makemigrations

python manage.py migrate

gunicorn -c gunicorn-config.py app_name.wsgi:application
👤Dmitrii G.

Leave a comment