Finding out the veth# interface of a docker container

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


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