The Docker server creates a bridge called docker0
by default inside the Linux kernel, and it can pass packets back and forth between other physical or virtual network interfaces so that they behave as a single Ethernet network . Run the following command to find out the list of interfaces in a VM and the IP addresses they are connected to:
root@ubuntu:~# ifconfig docker0 Link encap:Ethernet HWaddr 56:84:7a:fe:97:99 inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::5484:7aff:fefe:9799/64 Scope:Link inet6 addr: fe80::1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11909 errors:0 dropped:0 overruns:0 frame:0 TX packets:14826 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:516868 (516.8 KB) TX bytes:46460483 (46.4 MB) eth0 Link encap:Ethernet HWaddr 00:0c:29:0d:f4:2c inet addr:192.168.186.129 Bcast:192.168.186.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe0d:f42c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:108865 errors:0 dropped:0 overruns:0 frame:0 TX packets:31708 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:59902195 (59.9 MB) TX bytes:3916180 (3.9 MB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:336 (336.0 B) TX bytes:336 (336.0 B)
Once you have one or more containers up and running, you can confirm that Docker has properly connected them to the docker0
bridge by running the brctl
command on the host machine and looking at the interfaces
column of the output.
Before configuring the docker0
bridge, install the bridge utilities:
# apt-get install bridge-utils
Here is a host with two different containers connected:
root@ubuntu:~# brctl show bridge name bridge id STP enabled interfaces docker0 8000.56847afe9799 no veth21b2e16 veth7092a45
Docker uses the docker0
bridge settings whenever a container is created. It assigns a new IP address from the range available on the bridge whenever a new container is created, as can be seen here:
root@ubuntu:~# docker run -t -i --name container1 ubuntu:latest /bin/bash root@e54e9312dc04:/# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:07 inet addr:172.17.0.7 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: 2001:db8:1::242:ac11:7/64 Scope:Global inet6 addr: fe80::42:acff:fe11:7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:7 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:738 (738.0 B) TX bytes:696 (696.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) root@e54e9312dc04:/# ip route default via 172.17.42.1 dev eth0 172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.7
By default, Docker provides a virtual network called docker0
, which has the IP address 172.17.42.1
. Docker containers have IP addresses in the range of 172.17.0.0/16
.
To change the default settings in Docker, modify the file /etc/default/docker
.
Changing the default bridge from docker0
to br0
can be done like this:
# sudo service docker stop # sudo ip link set dev docker0 down # sudo brctl delbr docker0 # sudo iptables -t nat -F POSTROUTING # echo 'DOCKER_OPTS="-b=br0"' >> /etc/default/docker # sudo brctl addbr br0 # sudo ip addr add 192.168.10.1/24 dev br0 # sudo ip link set dev br0 up # sudo service docker start
The following command displays the new bridge name and the IP address range of the Docker service:
root@ubuntu:~# ifconfig br0 Link encap:Ethernet HWaddr ae:b2:dc:ed:e6:af inet addr:192.168.10.1 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::acb2:dcff:feed:e6af/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:738 (738.0 B) eth0 Link encap:Ethernet HWaddr 00:0c:29:0d:f4:2c inet addr:192.168.186.129 Bcast:192.168.186.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe0d:f42c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:110823 errors:0 dropped:0 overruns:0 frame:0 TX packets:33148 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:60081009 (60.0 MB) TX bytes:4176982 (4.1 MB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:336 (336.0 B) TX bytes:336 (336.0 B)