Skip to content

Show toolbar for docker's internal IP address #1887

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
Feb 22, 2024
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
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])
+ ".1"
)
internal_ips.append(docker_ip)
except socket.gaierror:
# 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
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