Docker: gracefully stop django server


I'm using docker and docker-compose. Inside docker-compose.yml I have command that starts django server:

command: ["./run/web.sh"]

In json format command should run in exec mode. Inside web.sh:

#!/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 stop?


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

<pre class="lang-yaml prettyprint-override">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:

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



  • Simple chat system over websockets with reconnection feature
  • CMake configuration for ffmpeg in C++ project
  • google translate not showing up when https is used in url
  • php string comparison unexpected type juggling
  • Phonegap (Application error… Network error occured)
  • Graphics using Standard C++ only?
  • Move an 8 bit image using VHDL
  • Designing a database that handles inventory tracking with product variations
  • Flexible array member and pointer member : pros and cons?
  • Binding a combobox in subform Access
  • How to set title name of the pdf. While viewing the Document(New Tab)
  • Encounter error “IB API required” when IB API is installed
  • Error with custom SVM model for tuning in caret
  • Limit # of records returned based on a form control
  • Alternative to bcrypt when saving passwords in PHP 5.2
  • configure openjpa on to spring boot
  • Spring Cloud Config - Multiple Composite Repositories?
  • Send HTML Mail with Unicode
  • Align microsoft access queries
  • Convert current tmux session to configuration file
  • SpringBoot don't replacen System variable {user.home} in Spring Tool Suite Version: 3.8.4.RELEA
  • Scripting Support For Image Filtering in DigitalMicrograph
  • Modifying native query cannot have named parameter bindings?
  • Adding native code to an existing Worklight hybrid app
  • How to display content depending on dropdown menue user selection
  • Admob in ListView not clickable
  • Swift manually rotate view controller
  • Autocomplete source from project settings
  • Reload Page with Javascript after Database changes
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Ruby regex for matching simpliest Ruby's regexes
  • C++ STL stack pop operation giving segmentation fault
  • How to configure the APNS.Certificate in the arm template
  • Terminal run dalvikvm with am.jar
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • ReferenceError: TextEncoder is not defined