Tuesday, November 5, 2024
HomeDevOpsHow to connect Docker containers External Network using Bridge

How to connect Docker containers External Network using Bridge

Using the default docker0 bridge and the port mapping works for most of the scenarios, but not all the scenarios, for example, you want to put all the docker containers in a flat network to provide full-access between the containers on different docker hosts. There are several ways to configure the docker multi-host networking, this post will cover one of these ways: using Linux bridge to directly bridge the docker containers to the external network.

Create the Linux bridge

# brctl addbr br0
# brctl addif br0 eth1   #eth1 is interface name (to get # ip a)
# brctl setfd br0 0      #0 is bridge forward delay time
# ifconfig br0 10.137.0.161 netmask 255.255.0.0

Now created bridge using command line, but it’s not persistent through reboots, to make persistent, modify the network interface configuration. This example is in centos

Read more: Basic Docker Networking – Explained

 # cat /etc/network/interfaces.d/br0
   auto br0
    iface br0 inet static
      address 10.137.0.161
      netmask 255.255.0.0
      gateway 10.137.0.3
      bridge_ports eth1
      bridge_fd 0
      bridge_hello 2
      bridge_maxage 12
      bridge_stp off

 # /etc/init.d/network restart

When the bridge is created successfully, the brctl show command will show something like:

# brctl show br0
 bridge name     bridge id               STP enabled     interfaces
 br0             8000.005056bcfeae       no              eth1

Connect Docker to bridge

Option 1:

Create a new docker network to use this Linux bridge and explicitly specify –net with docker run command.

# docker network create --driver=bridge --ip-range=10.138.0.0/24 --subnet=10.0.0.0/16 --aux-address='ip1=10.138.0.1' --aux-address='ip2=10.138.0.2' --aux-address='ip3=10.137.0.3' -o "com.docker.network.bridge.name=br0" br0

# docker run --net=docker_br –name foxcontainer204 -it  motoskia/apache-php

Option 2:

Have docker to use the Linux bridge as default network.

Like us on Facebook

Update /etc/default/docker with the following line:

# cat /etc/default/dockerDOCKER_OPTS="--bridge=br0 --fixed-cidr=10.138.0.0/24 --default-gateway=10.138.0.3"

# /etc/init.d/docker restart

# docker run -it –name foxcontainer204 motoskia/apache-php

Verify if the docker containers are connected to the bridge correctly

If the docker containers are connected to the bridge correctly, brctl show <bridge_name> will show new veth ports.

# brctl show br0
 bridge name     bridge id               STP enabled     interfaces
 br0             8000.005056bcfeae       no              eth1
                                                         vetha2b29b2@if27

You can check docker network information from docker inspect <containername> will show the right network information

# docker inspect foxcontainer204
 ......
 
 "Networks": {
    "bridge": {
       "IPAMConfig": null,
       "Links": null,
       "Aliases": null,
       "NetworkID": " 8eecff31a9e9c9aea9760d0296ff84809fa4fb6420f6578539328c4982308022",
       "EndpointID": " fe639927539d6c168c18ddda87457a7af0696a048448733df2de7a7129349611",
       "Gateway": "10.138.0.3",
       "IPAddress": "10.138.0.161",
       "IPPrefixLen": 16,
       "IPv6Gateway": "",
       "GlobalIPv6Address": "",
       "GlobalIPv6PrefixLen": 0,
       "MacAddress": "02:42:0a:89:02:01"
  }
 ......

Like us on Facebook: FoxuTech

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments