Django & Docker

Django Debug Toolbar not showing up when using Docker — Django & Docker

The solution to why Django Debug Toolbar does not show up when using Docker.

Ranjan MP

--

Django Debug Toolbar is an essential tool in the development process of a Django application.

This article provides the solution as well as an explanation. If you just want the solution please scroll down to Solution 2 and 3.

Django Debug Toolbar has a setting called INTERNAL_IPS . The Toolbar only appears if the IP addresses from which the user is accessing the application is listed in INTERNAL_IPS . This is a security feature of the Django Debug Toolbar so that the Toolbar cannot be accessed by users accessing the application from different IP addresses. The developer can set his/her IP Address in the INTERNAL_IPS setting so that the Toolbar can only be accessed by the developer. During local development the INTERNAL_IPS should be set to ["127.0.0.1"] .

So what's the issue with Django Debug Toolbar and Docker.

An Individual Docker container is assigned an IP address from the Docker bridge network by default. When multiple containers are run through docker-compose a new network created with a default subnet mask and gateway for all the applications within the docker-compose. This new network is given a default name <folder_name>_default , if the network name is not specified in the docker-compose file, <folder_name> is the name of the folder/directory in which the docker-compose file resides. eg: If the docker-compose file resides in your project folder, the network name would be project_default .

Any request to the container is passed through the gateway of the Docker bridge network in the case of the Individual container and the gateway of the network created by the docker-compose in the case of multi-container Docker applications.

Coming to Django Debug Toolbar
The request originating from the local host is passed through the gateway of the network, due to which the Django application resolves the IP address of the request originating as the gateway IP address of the Docker network. As a result, even if we have the set ["127.0.0.1"] as INTERNAL_IPS during development in the local machine, the Django Debug Toolbar does not appear.

Solutions

  1. We can set the gateway IP address value in the INTERNAL_IPS .
    (This is not an ideal solution, Please use solution 2 or 3)
  • List all the Docker networks
$ docker network lsNETWORK ID     NAME                   DRIVER    SCOPE
ad7i9bff3c1a bridge bridge local
ys8f1asdf87r <folder_name>_default bridge local
hf74hfkgi83e host host local
46dhfgritu44 none null local

<folder_name>_default is the default network created, if the network is not specified, when running a multi-Container setup using docker-compose

  • To get the gateway IP address of the bridge in when running Individual containers
$ docker network inspect bridge

The "Gateway" value in the response is the gateway IP address

  • To get the gateway IP address of bridge in when running multi-container setup using docker-compose
$ docker network inspect <folder_name>_default

The "Gateway" value in the response is the gateway IP address

The gateway IP address can be added to the INTERNAL_IPS setting, to display the Django Debug Toolbar.

However this is not the Ideal Solution as the gateway IP address can change when a new image is built, containers are stopped, removed, and started. Updating INTERNAL_IPS every time, especially during the development process, where the rebuilding of images and stopping and starting containers are quite common, This adds an additional step and consumes unnecessary time, which can be avoided.

There are two ways by which this can be avoided.

2. Accessing the Docker gateway IP address during the runtime and setting the INTERNAL_IPS.

In Django settings.py add.

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname()) returns the IP address of the host machine in ips parameter.

[ip[:-1] + '1' for ip in ips] removes the last digit of the host IP address and replaces it by 1. In simple words, this is done because let's say the IP address of the host machine is 172.17.0.3 by default, the gateway of the Docker network is always set to172.17.0.1 , hence the last value of the IP address is replaced with 1.

3. Setting the Django Debug Toolbar setting SHOW_TOOLBAR_CALLBACK = True

In Django settings.py add.

The function lambda request: False if request.is_ajax() else True , checks if the request is an Ajax request or not, if it's not, it returns True.

When SHOW_TOOLBAR_CALLBACK is set to True, the Django Debug Toolbar is displayed ignoring the Internal IP setting.

** Never enable Django Debug Toolbar in a production environment.

Thank you!!

--

--