3

I'm aware that you can get a host's ip programatically with Python doing something like:

from socket import gethostname, gethostbyname

ip = gethostbyname(gethostname())
print(ip)

but, as expected, when running the code mentioned above from a container (just ftr, with user namespaces enabled) the output is 172.17.0.2.

How can I programmatically get the host's ip from within the container?

Note: I'm working in a Linux environment.

2
  • 1
    A container can't normally see the host's IP addresses (plural); this is doubly true if you're running inside a VM-based Docker environment like Docker Desktop. Using the host ip in docker-compose suggests an approach based on finding an appropriate IP address on the host and injecting it into the container as an environment variable; does that work for you? Commented Jan 5, 2023 at 17:09
  • @DavidMaze I think the option of passing the IP address as an environment variable is the only one left. Thanks! Commented Jan 6, 2023 at 7:57

1 Answer 1

2

This works on my mac

ip = gethostbyname('docker.for.mac.localhost')

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I have updated the questions. I need it for Linux systems. Btw, I think nowadays is host.docker.internal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.