-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show toolbar for docker's internal IP address (#1887)
Fixes #1854
- Loading branch information
1 parent
0663276
commit 7d77a34
Showing
4 changed files
with
33 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
""" | ||
|
||
import re | ||
import socket | ||
from functools import lru_cache | ||
|
||
from django.conf import settings | ||
|
@@ -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]) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
+ ".1" | ||
) | ||
internal_ips.append(docker_ip) | ||
except socket.gaierror: | ||
This comment has been minimized.
Sorry, something went wrong.
QBH3
|
||
# 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
This comment has been minimized.
Sorry, something went wrong.
QBH3
|
||
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 | ||
--------------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gethostbyname() can timeout and if it will to so on every request the django server receives