View the Docker network type

root:~ $ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
2294b8d47566        bridge              bridge              local
7ff5fac64629        host                host                local
ffdd428e9c41        none                null                local
Copy the code

Check the network

#Viewing the Local IP Address
root:~ $ ip addr
#Local loopback address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
#Intranet address of Ali Cloud2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:16:3e:04:96:35 BRD FF :ff:ff:ff:ff: FF inet 172.18.240.142/20 BRD 172.18.255.255 scope Global Dynamic eth0 VALID_LFT 281094494sec preferred_lft 281094494sec inet6 fe80::216:3eff:fe04:9635/64 scope link valid_lft forever preferred_lft forever#Docker0 is the docker network address3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:70:ff:98:d0 brd Ff :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:70ff:feff:98d0/64 scope link valid_lft forever preferred_lft forever#Address of the container       
85: veth654c937@if84: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 02:d1:7a:b1:94:aa brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::d1:7aff:feb1:94aa/64 scope link 
       valid_lft forever preferred_lft forever
#Container address Docker assigns an IP address vethb825d28@if86 to the container and matches 87:86 through the bridge (a pair of network cards)
87: vethb825d28@if86: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 26:c6:e9:2e:a8:b6 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::24c6:e9ff:fe2e:a8b6/64 scope link 
       valid_lft forever preferred_lft forever
Copy the code

Docker network principle

As long as docker is installed, the host will have a network card Docker0 bridge mode, every start a Docker container, Docker will allocate an IP for the container, the use of technology is EVTH-pair (bridge) technology!

Test it out!! You can see that when a new container is started, Docker assigns a bridge pair to the container

Test communication between containers

root:~ $ docker exec -it centos1 ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever 88: eth0@if89: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd Ff :ff:ff:ff:ff:ff :ff link- netnSID 0 inet 172.17.0.2/16 BRD 172.17.255.255 scope global eth0 valid_lft forever preferred_lft  forever root:~ $ docker exec -it centos2 ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever 90: eth0@if91: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:03 brd Ff :ff:ff:ff:ff:ff :ff link- netnSID 0 inet 172.17.0.3/16 BRD 172.17.255.255 scope global eth0 valid_lft forever preferred_lft Forever root:~ $docker exec -it centos1 ping 172.17.0.3 ping 172.17.0.3 (172.17.0.3) 56(84) bytes of data.64 bytes From 172.17.0.3: ICmp_seq =1 TTL =64 time=0.105 ms 64 bytes from 172.17.0.3: Icmp_seq =2 TTL =64 time=0.091 ms 64 bytes from 172.17.0.3: ICmp_seq =3 TTL =64 time=0.083 ms 64 bytes from 172.17.0.3: Icmp_seq = 4 TTL = 64 time = 0.088 msCopy the code

By viewing IP address information, we can see that Docker realizes communication between containers through EVTH-pair technology

If you’re smart enough, you can see how container communication works

Summary: CentoS1 and CentoS2 communicate via a common router (Docker0)!

Docker0 will assign a default IP address to the container.

IP address upper limit (based on subnet) : 172.17.0.2/16 Upper limit: 255 x 255. 172.17.0.2/24 Upper limit: 255

The disadvantage of docker0

Root :~ $docker exec it centos1 ping 172.17.0.3 ping 172.17.0.3 (172.17.0.3) 56(84) bytes of data.64 bytes from 172.17.0.3: ICmp_seq =1 TTL =64 time=0.079 ms 64 bytes from 172.17.0.3: Icmp_seq =2 TTL =64 time= 0.03ms ^C -- 172.17.0.3 ping statistics -- 3 packets transmitted, 2 received 0% packet loss, Time 8ms RTT min/avg/ Max /mdev = 0.079/0.080/0.081/0.001 ms root:~ $docker exec -it centos1 ping centos2 ping: centos2 Name or service not knownCopy the code

Native Docker containers cannot communicate with each other by container name!!

Here comes the custom web

#Docker network mode
root:~ $ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
2294b8d47566        bridge              bridge              local
7ff5fac64629        host                host                local
ffdd428e9c41        none                null                local
Copy the code

tip: docker run -dit -P –name centos1 centos = docker run -dit -P –name centos1 –net bridge centos

Creating network Mode

#Use bridge mode -- Driver Bridge
#Allocate subnet --subnet 192.168.0.0/16
#Assign gateway (network egress) -- Gateway 192.168.0.1Root :~ $docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet dfd664e6ada8011941f6287e14a9ae272ef74747f6def44ba65d45d754cde461 root:~ $ docker network ls NETWORK ID NAME DRIVER SCOPE  2294b8d47566 bridge bridge local 7ff5fac64629 host host local dfd664e6ada8 mynet bridge local ffdd428e9c41 none null local root:~ $ docker inspect mynet [ { "Name": "mynet", "Id": "dfd664e6ada8011941f6287e14a9ae272ef74747f6def44ba65d45d754cde461", "Created": "2021-09-08T10:11:28.152426072+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Default", "Options" : {}, "Config" : [{" Subnet configures ":" 192.168.0.0/16 ", "Gateway" : "192.168.0.1"}]}, "Internal" : false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]Copy the code

Start the container using a custom network

root:~ $ docker run -dit -P --name centos3 --net mynet centos 6a665f9682d94f46588aebab5e7231635abfdf2dd437343ef603333d7f5b5f29 root:~ $ docker run -dit -P --name centos4 --net mynet centos 437b46ed31953e4dc865dacdcaa66364a469d19b0ab515e3ddb83d14195bac42 root:~ $ docker exec -it centos3 ping centos4 PING centos4 (192.168.0.3) 56(84) bytes of data.64 bytes from centos4. Mynet (192.168.0.3): Icmp_seq =1 TTL =64 time=0.136 ms 64 bytes from centos4.mynet (192.168.0.3): Icmp_seq =2 TTL =64 time= 0.081ms ^C -- packet statistics -- 3 packets transmitted, 2 received, 0% packet loss Time 2ms RTT min/avg/ Max /mdev = 0.081/0.108/0.136/0.029 ms root:~ $docker network inspect mynet [{"Name": "mynet", "Id": "dfd664e6ada8011941f6287e14a9ae272ef74747f6def44ba65d45d754cde461", "Created": "2021-09-08T10:11:28.152426072+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Default", "Options" : {}, "Config" : [{" Subnet configures ":" 192.168.0.0/16 ", "Gateway" : "192.168.0.1"}]}, "Internal" : false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "437b46ed31953e4dc865dacdcaa66364a469d19b0ab515e3ddb83d14195bac42": { "Name": "centos4", "EndpointID": "c452f49a1957ece9ea4020597072c8ed9e2258efc6475445f2d34fbcba93f558", "MacAddress": "02:42:c0:a8:00:03", "IPv4Address": "" IPv6Address 192.168.0.3/16", ":" "}, "six a665f9682d94f46588aebab5e7231635abfdf2dd437343ef603333d7f5b5f29" : {" Name ": "centos3", "EndpointID": "08f5e5cede761596b843cb1b3141a588798684355bdf48cc7b661bf1e855d83f", "MacAddress": "02:42: c0: a8:00:02", "IPv4Address" : "192.168.0.2/16", "IPv6Address" : ""}}," Options ":" Labels ": {} {},}]Copy the code

Summary: Use custom network to communicate by container name, achieve fast scaling under microservice publishing!