Skip to content

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

Closed

Conversation

dr-rompecabezas
Copy link
Member

@dr-rompecabezas dr-rompecabezas commented Mar 5, 2025

Description

Updated show_toolbar function in debug_toolbar/middleware.py to use gateway.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).

# getent ahostsv4 host.docker.internal
192.168.65.254  STREAM host.docker.internal
192.168.65.254  DGRAM  
192.168.65.254  RAW    
# getent ahostsv4 gateway.docker.internal
192.168.65.1    STREAM gateway.docker.internal
192.168.65.1    DGRAM  
192.168.65.1    RAW   

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 use gateway.docker.internal, which already resolves to 192.168.65.1.

The warning in our docs at installation is still valid.

Tested on:

  • Docker version 27.5.1 + django-debug-toolbar v5.0.1

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:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

dr-rompecabezas and others added 3 commits March 5, 2025 16:27
- 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
- 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`.
@dr-rompecabezas
Copy link
Member Author

I updated the tests but had to hardcode REMOTE_ADDR. I'll keep working on this, because the tests just don't seem right, and I just don't know what I'm missing here.

image

@matthiask
Copy link
Member

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. host.docker.internal and host.containers.internal exist, but gateway.* does not. My version of podman and the engine is 5.3.2 released in January 2025. 5.4 is available but only just, and the release notes do not mention "gateway" at all.

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?

@matthiask
Copy link
Member

Or maybe I'm doing it wrong and the network isn't set up correctly when using podman run. I don't know.

@dr-rompecabezas
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify / clean up Docker IP determination
3 participants