Skip to content

Add show_toolbar_with_docker function for Docker IP handling #2153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def show_toolbar(request):
if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
return True

# No test passed
return False


def show_toolbar_with_docker(request):
"""
Default function to determine whether to show the toolbar on a given page.
"""
if not settings.DEBUG:
return False

# Test: settings
if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
return True

# Test: Docker
try:
# This is a hack for docker installations. It attempts to look
Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Pending

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

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


5.0.1 (2025-01-13)
------------------
* Fixing the build and release process. No functional changes.
Expand Down
9 changes: 5 additions & 4 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ option.

.. warning::

If using Docker, the toolbar will attempt to look up your host name
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``.
If using Docker you can use
``debug_toolbar.middleware.show_toolbar_with_docker`` as your
``SHOW_TOOLBAR_CALLBACK`` which attempts to automatically look up the
Docker gateway IP and treat it as an allowable internal IP so that the
toolbar is shown to you.

7. Disable the toolbar when running tests (optional)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
9 changes: 7 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
from django.test.utils import override_settings

from debug_toolbar.forms import SignedDataForm
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
from debug_toolbar.middleware import (
DebugToolbarMiddleware,
show_toolbar,
show_toolbar_with_docker,
)
from debug_toolbar.panels import Panel
from debug_toolbar.toolbar import DebugToolbar

Expand Down Expand Up @@ -72,7 +76,8 @@ 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))
self.assertFalse(show_toolbar(self.request))
self.assertTrue(show_toolbar_with_docker(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")

def test_not_iterating_over_INTERNAL_IPS(self):
Expand Down
9 changes: 7 additions & 2 deletions tests/test_integration_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from django.test.utils import override_settings

from debug_toolbar.forms import SignedDataForm
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
from debug_toolbar.middleware import (
DebugToolbarMiddleware,
show_toolbar,
show_toolbar_with_docker,
)
from debug_toolbar.panels import Panel
from debug_toolbar.toolbar import DebugToolbar

Expand Down Expand Up @@ -59,7 +63,8 @@ async 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))
self.assertFalse(show_toolbar(self.request))
self.assertTrue(show_toolbar_with_docker(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")

async def test_not_iterating_over_INTERNAL_IPS(self):
Expand Down