Skip to content

Commit 7d77a34

Browse files
Show toolbar for docker's internal IP address (#1887)
Fixes #1854
1 parent 0663276 commit 7d77a34

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

debug_toolbar/middleware.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import re
6+
import socket
67
from functools import lru_cache
78

89
from django.conf import settings
@@ -19,7 +20,22 @@ def show_toolbar(request):
1920
"""
2021
Default function to determine whether to show the toolbar on a given page.
2122
"""
22-
return settings.DEBUG and request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS
23+
internal_ips = settings.INTERNAL_IPS.copy()
24+
25+
try:
26+
# This is a hack for docker installations. It attempts to look
27+
# up the IP address of the docker host.
28+
# This is not guaranteed to work.
29+
docker_ip = (
30+
# Convert the last segment of the IP address to be .1
31+
".".join(socket.gethostbyname("host.docker.internal").rsplit(".")[:-1])
32+
+ ".1"
33+
)
34+
internal_ips.append(docker_ip)
35+
except socket.gaierror:
36+
# It's fine if the lookup errored since they may not be using docker
37+
pass
38+
return settings.DEBUG and request.META.get("REMOTE_ADDR") in internal_ips
2339

2440

2541
@lru_cache(maxsize=None)

docs/changes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Change log
44
Pending
55
-------
66

7+
* Automatically support Docker rather than having the developer write a
8+
workaround for ``INTERNAL_IPS``.
9+
710
4.3.0 (2024-02-01)
811
------------------
912

docs/installation.rst

+4-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,10 @@ option.
145145

146146
.. warning::
147147

148-
If using Docker the following will set your ``INTERNAL_IPS`` correctly in Debug mode::
149-
150-
if DEBUG:
151-
import socket # only if you haven't already imported this
152-
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
153-
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "10.0.2.2"]
148+
If using Docker, the toolbar will attempt to look up your host name
149+
automatically and treat it as an allowable internal IP. If you're not
150+
able to get the toolbar to work with your docker installation, review
151+
the code in ``debug_toolbar.middleware.show_toolbar``.
154152

155153
Troubleshooting
156154
---------------

tests/test_integration.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import time
44
import unittest
5+
from unittest.mock import patch
56

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

70+
@patch("socket.gethostbyname", return_value="127.0.0.255")
71+
def test_show_toolbar_docker(self, mocked_gethostbyname):
72+
with self.settings(INTERNAL_IPS=[]):
73+
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
74+
# is shifted to be 1.
75+
self.assertTrue(show_toolbar(self.request))
76+
mocked_gethostbyname.assert_called_once_with("host.docker.internal")
77+
6978
def test_should_render_panels_RENDER_PANELS(self):
7079
"""
7180
The toolbar should force rendering panels on each request

0 commit comments

Comments
 (0)