Skip to content

Commit 4056f11

Browse files
Add show_toolbar_with_docker function for Docker IP handling
- Introduced a new function, show_toolbar_with_docker, to determine if the toolbar should be displayed when running inside Docker containers. - Updated installation documentation to reference the new function for Docker configurations. - Added a changelog entry for the new functionality. Co-authored-by: Matthias Kestenholz <[email protected]>
1 parent a060c79 commit 4056f11

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

debug_toolbar/middleware.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ def show_toolbar(request):
3131
if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
3232
return True
3333

34+
# No test passed
35+
return False
36+
37+
38+
def show_toolbar_with_docker(request):
39+
"""
40+
Default function to determine whether to show the toolbar on a given page.
41+
"""
42+
if not settings.DEBUG:
43+
return False
44+
45+
# Test: settings
46+
if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
47+
return True
48+
3449
# Test: Docker
3550
try:
3651
# This is a hack for docker installations. It attempts to look

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Pending
66

77
* Added support for checking if pytest as the test runner when determining
88
if tests are running.
9+
* Added ``show_toolbar_with_docker`` function to check Docker host IP address
10+
when running inside Docker containers.
911

1012
5.2.0 (2025-04-29)
1113
------------------
@@ -46,6 +48,7 @@ Pending
4648
* Fix for exception-unhandled "forked" Promise chain in rebound window.fetch
4749
* Create a CSP nonce property on the toolbar ``Toolbar().csp_nonce``.
4850

51+
4952
5.0.1 (2025-01-13)
5053
------------------
5154
* Fixing the build and release process. No functional changes.

docs/installation.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ option.
152152

153153
.. warning::
154154

155-
If using Docker, the toolbar will attempt to look up your host name
156-
automatically and treat it as an allowable internal IP. If you're not
157-
able to get the toolbar to work with your docker installation, review
158-
the code in ``debug_toolbar.middleware.show_toolbar``.
155+
If using Docker you can use
156+
``debug_toolbar.middleware.show_toolbar_with_docker`` as your
157+
``SHOW_TOOLBAR_CALLBACK`` which attempts to automatically look up the
158+
Docker gateway IP and treat it as an allowable internal IP so that the
159+
toolbar is shown to you.
159160

160161
7. Disable the toolbar when running tests (optional)
161162
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/test_integration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from django.test.utils import override_settings
1616

1717
from debug_toolbar.forms import SignedDataForm
18-
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
18+
from debug_toolbar.middleware import (
19+
DebugToolbarMiddleware,
20+
show_toolbar,
21+
show_toolbar_with_docker,
22+
)
1923
from debug_toolbar.panels import Panel
2024
from debug_toolbar.toolbar import DebugToolbar
2125

@@ -72,7 +76,8 @@ def test_show_toolbar_docker(self, mocked_gethostbyname):
7276
with self.settings(INTERNAL_IPS=[]):
7377
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
7478
# is shifted to be 1.
75-
self.assertTrue(show_toolbar(self.request))
79+
self.assertFalse(show_toolbar(self.request))
80+
self.assertTrue(show_toolbar_with_docker(self.request))
7681
mocked_gethostbyname.assert_called_once_with("host.docker.internal")
7782

7883
def test_not_iterating_over_INTERNAL_IPS(self):

tests/test_integration_async.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from django.test.utils import override_settings
1212

1313
from debug_toolbar.forms import SignedDataForm
14-
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
14+
from debug_toolbar.middleware import (
15+
DebugToolbarMiddleware,
16+
show_toolbar,
17+
show_toolbar_with_docker,
18+
)
1519
from debug_toolbar.panels import Panel
1620
from debug_toolbar.toolbar import DebugToolbar
1721

@@ -59,7 +63,8 @@ async def test_show_toolbar_docker(self, mocked_gethostbyname):
5963
with self.settings(INTERNAL_IPS=[]):
6064
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
6165
# is shifted to be 1.
62-
self.assertTrue(show_toolbar(self.request))
66+
self.assertFalse(show_toolbar(self.request))
67+
self.assertTrue(show_toolbar_with_docker(self.request))
6368
mocked_gethostbyname.assert_called_once_with("host.docker.internal")
6469

6570
async def test_not_iterating_over_INTERNAL_IPS(self):

0 commit comments

Comments
 (0)