docker-compose.yml I have command that starts django server:
In json format command should run in exec mode. Inside
#!/usr/bin/env bash exec python manage.py runserver
When I tried to stop service with
docker-compose stop it waited 10 seconds (default timeout) and then just kill service. In logs I've found
project_web_1 exited with code 137.
How to stop django runserver gracefully with
docker-compose.yml file, set the
django: build: . command: ["./run/web.sh"] stop_signal: SIGINT<hr />
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.
I'll leave here the config that works for me:
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