File tree 4 files changed +33
-7
lines changed
4 files changed +33
-7
lines changed Original file line number Diff line number Diff line change 3
3
"""
4
4
5
5
import re
6
+ import socket
6
7
from functools import lru_cache
7
8
8
9
from django .conf import settings
@@ -19,7 +20,22 @@ def show_toolbar(request):
19
20
"""
20
21
Default function to determine whether to show the toolbar on a given page.
21
22
"""
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
23
39
24
40
25
41
@lru_cache (maxsize = None )
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ Change log
4
4
Pending
5
5
-------
6
6
7
+ * Automatically support Docker rather than having the developer write a
8
+ workaround for ``INTERNAL_IPS ``.
9
+
7
10
4.3.0 (2024-02-01)
8
11
------------------
9
12
Original file line number Diff line number Diff line change @@ -145,12 +145,10 @@ option.
145
145
146
146
.. warning ::
147
147
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 ``.
154
152
155
153
Troubleshooting
156
154
---------------
Original file line number Diff line number Diff line change 2
2
import re
3
3
import time
4
4
import unittest
5
+ from unittest .mock import patch
5
6
6
7
import html5lib
7
8
from django .contrib .staticfiles .testing import StaticLiveServerTestCase
@@ -66,6 +67,14 @@ def test_show_toolbar_INTERNAL_IPS(self):
66
67
with self .settings (INTERNAL_IPS = []):
67
68
self .assertFalse (show_toolbar (self .request ))
68
69
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
+
69
78
def test_should_render_panels_RENDER_PANELS (self ):
70
79
"""
71
80
The toolbar should force rendering panels on each request
You can’t perform that action at this time.
0 commit comments