File tree Expand file tree Collapse file tree 4 files changed +33
-7
lines changed Expand file tree Collapse file tree 4 files changed +33
-7
lines changed Original file line number Diff line number Diff line change 33"""
44
55import re
6+ import socket
67from functools import lru_cache
78
89from 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 )
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ Change log
44Pending
55-------
66
7+ * Automatically support Docker rather than having the developer write a
8+ workaround for ``INTERNAL_IPS ``.
9+
7104.3.0 (2024-02-01)
811------------------
912
Original file line number Diff line number Diff 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
155153Troubleshooting
156154---------------
Original file line number Diff line number Diff line change 22import re
33import time
44import unittest
5+ from unittest .mock import patch
56
67import html5lib
78from 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
You can’t perform that action at this time.
0 commit comments