Skip to content

Commit

Permalink
Show toolbar for docker's internal IP address (#1887)
Browse files Browse the repository at this point in the history
Fixes #1854
  • Loading branch information
tim-schilling committed Feb 22, 2024
1 parent 0663276 commit 7d77a34
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
18 changes: 17 additions & 1 deletion debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import re
import socket
from functools import lru_cache

from django.conf import settings
Expand All @@ -19,7 +20,22 @@ def show_toolbar(request):
"""
Default function to determine whether to show the toolbar on a given page.
"""
return settings.DEBUG and request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS
internal_ips = settings.INTERNAL_IPS.copy()

try:
# This is a hack for docker installations. It attempts to look
# up the IP address of the docker host.
# This is not guaranteed to work.
docker_ip = (
# Convert the last segment of the IP address to be .1
".".join(socket.gethostbyname("host.docker.internal").rsplit(".")[:-1])

This comment has been minimized.

Copy link
@QBH3

QBH3 May 29, 2024

gethostbyname() can timeout and if it will to so on every request the django server receives

+ ".1"
)
internal_ips.append(docker_ip)
except socket.gaierror:

This comment has been minimized.

Copy link
@QBH3

QBH3 May 29, 2024

One could consider to not try to resolve the domain host.docker.internal on every request if the first try lead to a timeout or error.

# It's fine if the lookup errored since they may not be using docker
pass
return settings.DEBUG and request.META.get("REMOTE_ADDR") in internal_ips


@lru_cache(maxsize=None)
Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change log
Pending
-------

* Automatically support Docker rather than having the developer write a
workaround for ``INTERNAL_IPS``.

4.3.0 (2024-02-01)
------------------

Expand Down
10 changes: 4 additions & 6 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ option.

.. warning::

If using Docker the following will set your ``INTERNAL_IPS`` correctly in Debug mode::

if DEBUG:
import socket # only if you haven't already imported this
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "10.0.2.2"]
If using Docker, the toolbar will attempt to look up your host name

This comment has been minimized.

Copy link
@QBH3

QBH3 May 29, 2024

The code in debug_toolbar/middleware.py is being used even if django is not running in a Docker Environment.

automatically and treat it as an allowable internal IP. If you're not
able to get the toolbar to work with your docker installation, review
the code in ``debug_toolbar.middleware.show_toolbar``.

Troubleshooting
---------------
Expand Down
9 changes: 9 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import time
import unittest
from unittest.mock import patch

import html5lib
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
Expand Down Expand Up @@ -66,6 +67,14 @@ def test_show_toolbar_INTERNAL_IPS(self):
with self.settings(INTERNAL_IPS=[]):
self.assertFalse(show_toolbar(self.request))

@patch("socket.gethostbyname", return_value="127.0.0.255")
def test_show_toolbar_docker(self, mocked_gethostbyname):
with self.settings(INTERNAL_IPS=[]):
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
# is shifted to be 1.
self.assertTrue(show_toolbar(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")

def test_should_render_panels_RENDER_PANELS(self):
"""
The toolbar should force rendering panels on each request
Expand Down

0 comments on commit 7d77a34

Please sign in to comment.