Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bridge name associated to docker Network

i am creating a docker network using "docker network create --d bridge mynet". I want to get the bridge name that is associated to this docker network.

I know i can use the "-o" to provide bridge name. ex :"docker network create --d bridge -o com.docker.network.bridge.name=mybridge mynet". This method will work with normal bridge driver but in my case, I am using an ovs driver hence i am starting the network as "docker network create --d ovs mynet". If i use the "-o" in case of ovs driver it does not work.

please suggest me a way to get the bridge name associated to a network. The new bridge which is being created when i create the network,the bridge which has been created with it has a random name and i can see it using "ifconfig" command.

like image 760
peejain Avatar asked Mar 15 '16 21:03

peejain


1 Answers

Bridge name is not part of docker network inspect output, but it's always br-<first 12 characters of network id> (unless explicitly specified when creating the network with -o com.docker.network.bridge.name=<bridge name>).

network_id=$(docker network inspect -f {{.Id}} <network name>)
bridge_name="br-${network_id:0:12}"

Another option is to rely on the fact that the bridge serves as network's default gateway and therefore its IP address and network's gateway address are the same.

network_gateway=$(docker inspect \
    -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}' <network name>)
bridge_name=$(ifconfig | grep -B1 "inet $network_gateway" | head -1 | cut -d: -f1)
like image 96
Jarek Przygódzki Avatar answered Nov 08 '22 05:11

Jarek Przygódzki