Related concepts:
Bridge: equivalent to a virtual switch, all devices connected to the bridge can communicate normally;
veth pair: virtual network card pair (2 network cards), the sending and receiving data between the two network cards are consistent;
docker network:
Docker0 bridge: after installing and starting docker0, there will be a network card device of docker0 (this device is equivalent to a switch);
After the docker container is created, two virtual network cards will be created, one end is displayed in the host, the other end is eth0 in the container. These two network cards are virtual network card pairs.
Create two containers and execute ip a on the host. You can see that there will be two virtual network cards (starting with veth):
docker run -d --name nginx-1 nginx docker run -d --name nginx-2 nginx [root@ks-allinone ~]# ip a ... 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:92:a1:07:20 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:92ff:fea1:720/64 scope link valid_lft forever preferred_lft forever 37: veth87e12aa@if36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 56:3f:71:00:b2:95 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::543f:71ff:fe00:b295/64 scope link valid_lft forever preferred_lft forever 39: vethed473fd@if38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 66:f0:c4:e4:df:a9 brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet6 fe80::64f0:c4ff:fee4:dfa9/64 scope link valid_lft forever preferred_lft forever
brctl show: You can see that both virtual network cards are bound to docker0 (equivalent to connecting to a switch via a virtual network card on the host).
[root@ks-allinone ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.024292a10720 no veth87e12aa vethed473fd
Note: After connecting to the same bridge (equivalent to connecting to the same switch), the container can communicate with each other.