Docker Community Forums

Share and learn in the Docker community.

Determine overlay network IP from inside container

Hi all, I have a use case where I need to determine a container’s overlay network IP from inside the container. It’s a debian:buster-slim based container. Adding iproute2 and issuing ip addr gives something like this:

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet scope host lo
       valid_lft forever preferred_lft forever
2783: eth0@if2784: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default 
    link/ether 02:42:0a:00:0a:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet brd scope global eth0
       valid_lft forever preferred_lft forever
2785: eth1@if2786: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet brd scope global eth1
       valid_lft forever preferred_lft forever

So in this case the IP is However I think it’s not a good idea to rely on interface names or the like for parsing the IP out of this… so is there maybe a better way?

Think the easiest would be to get the last line from /etc/hosts
or even: hostname -i

Nice, both approaches resolve to the correct IP in my case. Is this somewhat guaranteed to be the case always?

I belive hostname -i uses the ip from the hosts file