I have hundreds of containers in various software virtual switches. I would like to know if its possible to find out which vnet# belongs to which docker container. Right now, I am detecting that by watching syslog as each container gets created. In KVM, there is a command "virsh domiflist ", which is exactly what i'm looking for.
4 Answers
The iflink of the container is the same as the ifindex of the veth#.
You can get the iflink of the container as follows:
docker exec -it <container-name> bash -c 'cat /sys/class/net/eth0/iflink'
Say, that results in 12, then grep for that:
grep -l 12 /sys/class/net/veth*/ifindex
That will give a unique result, on my system:
/sys/class/net/veth11d4238/ifindex
Combine that in a script:
#!/bin/bash
for container in $(docker ps -q); do
iflink=`docker exec -it $container bash -c 'cat /sys/class/net/eth0/iflink'`
iflink=`echo $iflink|tr -d '\r'`
veth=`grep -l $iflink /sys/class/net/veth*/ifindex`
veth=`echo $veth|sed -e 's;^.*net/\(.*\)/ifindex$;\1;'`
echo $container:$veth
done
The script was written to be easy to follow.
Sample run:
$ docker ps -q
c4d8096eff43
34ac6e9f1e6e
d5a2aa5f3de3
$ sudo ./vethfinder
c4d8096eff43:veth11d4238
34ac6e9f1e6e:veth7d52cd1
d5a2aa5f3de3:vethe46073d
Reference: https://forums.docker.com/t/relationship-between-interface-vethxxxxx-and-container/12872/20
- 2,630
searches for all interfaces in containers.
#!/bin/bash
for container in $(docker ps --format '{{.Names}}'); do
iflink=`docker exec -it $container bash -c 'cat /sys/class/net/eth*/iflink'`
for net in $iflink;do
net=`echo $net|tr -d '\r'`
veth=`grep -l $net /sys/class/net/veth*/ifindex`
veth=`echo $veth|sed -e 's;^.*net/\(.*\)/ifindex$;\1;'`
echo $container:$veth
done
done
I am using a different method, which seem to work just fine:
[root@kh1 ~]# docker inspect 6d48e279c5b8 --format '{{.State.Pid}}'
56316
[root@kh1 ~]#
[root@kh1 ~]# ip netns identify 56316
ns-56316
[root@kh1 ~]#
[root@kh1 ~]# ip netns list | grep ns-56316
ns-56316 (id: 6)
[root@kh1 ~]#
[root@kh1 ~]# ip link show | grep -B1 "link-netnsid 6"
330: veth1ce76e2b@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master cni0 state UP mode DEFAULT group default
link/ether de:f8:ce:a2:85:f9 brd ff:ff:ff:ff:ff:ff link-netnsid 6
- 121
-
I have created a tool that does the steps above automatically. You can download the linux x86_64 binary from here – mhristache May 29 '18 at 11:36
Have a look here: https://stackoverflow.com/questions/37860936/find-out-which-network-interface-belongs-to-docker-container
There are examples of how one can fetch veth names for running docker containers without root access.
An example shell script can be found here: https://github.com/dicho-usp/dockerveth
#!/bin/bash
NAME=$1
PID=$(docker inspect $NAME --format "{{.State.Pid}}")
while read iface id; do
[[ "$iface" == lo ]] && continue
veth=$(ip -br addr | sed -nre "s/(veth.)@if$id./\1/p")
echo -e "$NAME\t$iface\t$veth"
done < <(</proc/$PID/net/igmp awk '/^[0-9]+/{print $2 " " $1;}')
- 101
-
2Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Dec 16 '21 at 13:18
iflinkvalues might get out of sync. The system might assign two consecutiveiflinkvalues to two containers, instead of alternating between the container and the system. – NZD Mar 03 '17 at 00:37iflinklines up nicely. Stopped a few containers and created a few new ones, verified thatiflinkstill lines up nicely. I got 115 containers running and my highestiflinkwas 244. How do you get to 4216? – NZD Mar 05 '17 at 05:39iflinkvalue with a hostvethinterface's ifindex value". My script usesiflinkfor both. Maybe try 'ifindex' for 'veth' ? – NZD Mar 05 '17 at 05:51bashtoshin the first line of the for loop. Now it reads: iflink=docker exec -it $container sh -c 'cat /sys/class/net/eth0/iflink'This works fine in all my containers. – MadMike Jan 15 '18 at 15:03sudo sh docker-vethfinder.shI get "docker-vethfinder.sh: 2: Syntax error: word unexpected (expecting "do")" – Grumpy ol' Bear Jul 13 '22 at 18:11