Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get host's ip from within Docker container with Python

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.

like image 240
albertoperdomo2 Avatar asked Jan 27 '26 09:01

albertoperdomo2


1 Answers

This works on my mac

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

like image 131
NiRR Avatar answered Jan 28 '26 23:01

NiRR