-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Simplify Docker IP determination in middleware.py #2098
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
Simplify Docker IP determination in middleware.py #2098
Conversation
- Updated `show_toolbar` function in `debug_toolbar/middleware.py` to use `gateway.docker.internal` for Docker IP determination. - Updated `docs/changes.rst` to reflect the change. django-commons#2076
for more information, see https://pre-commit.ci
- Changed the mocked return value of `socket.gethostbyname` to `192.168.65.1` in both `test_integration.py` and `test_integration_async.py`. - Updated the expected call argument to `gateway.docker.internal`. - Hardcoded the `REMOTE_ADDR` value to match the mocked return value. Additionally, removed the duplicate variable assignment in `debug_toolbar/middleware.py`.
So, I ran some tests in the podman environment: [06.03. 08:20:11] ~$ podman run -it python
Python 3.13.2 (main, Feb 25 2025, 05:25:21) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname_ex("host.docker.internal")
('host.containers.internal', ['host.docker.internal'], ['169.254.1.2'])
>>> socket.gethostbyname_ex("gateway.docker.internal")
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
socket.gethostbyname_ex("gateway.docker.internal")
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -2] Name or service not known
>>> socket.gethostbyname_ex("host.containers.internal")
('host.containers.internal', ['host.docker.internal'], ['169.254.1.2'])
>>> socket.gethostbyname_ex("gateway.containers.internal")
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
socket.gethostbyname_ex("gateway.containers.internal")
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -2] Name or service not known
>>> I'm not sure if there's a solution for this. It seems to me that this could unfortunately be a dead end. I don't know the usage numbers but not supporting podman seems like a non starter. Opinions? |
Or maybe I'm doing it wrong and the network isn't set up correctly when using |
@matthiask as agreed, I am closing this PR. Our current implementation appears to be working for Docker and Podman users alike. There is no point in potentially breaking functionality only for the sake of cleaner code. |
Description
Updated
show_toolbar
function indebug_toolbar/middleware.py
to usegateway.docker.internal
for Docker IP determination per @srus's comment.To test in a multicontainer, production-ready environment, a cookiecutter-django template configured for Docker was used.
After starting an interactive shell inside the Django container, the commands below were run to compare our current implementation (
host.docker.internal
) to the proposed change to (gateway.docker.internal
).This confirms that since
host.docker.internal
resolves to 192.168.65.254, we don’t need to modify the last octet manually. Instead, we can directly usegateway.docker.internal
, which already resolves to 192.168.65.1.The warning in our docs at installation is still valid.
Tested on:
Keeping as a draft PR until we have tested in at least one more Docker version as well as Podman per @matthiask 's comment.
Fixes #2076
Checklist:
docs/changes.rst
.