Docker custom network and network connection

Docker custom network and network connection

docker custom network

View all networks

docker network ls

Screenshot:
Insert picture description here
Network mode:

mode description
bridge Bridge docker (default), create by yourself also use bridge mode
none Do not configure the network
host Share network with host
container The container network is connected! (Use less, but have a lot of limitations)

test:

# 我们直接启动的命令 --net bridge ,而这个就是我们的docker0
docker run -d -p 8003:8080 --name tomcat01 tomcat 
docker run -d -p 8003:8080 --name --net bridge tomcat01 tomcat 

# docker0特点,默认,域名不能访问,--link可以打通(比较麻烦)

# 我们可以自定义网络
# --driver bridge 
# --subnet 192.168.0.0/16  192.168.0.2 192.168.255.255
# --gateway 192.168.0.1
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet



# 查看所有网络
docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
326f1b307a15   bridge    bridge    local
bbdfc1178b51   host      host      local
04eb5e8aa466   mynet     bridge    local
cc581864c464   none      null      local

View own network

docker network inspect mynet

Screenshot:
Insert picture description here

test:

[root@izwz94goxqbb9ayvrlpgcaz ~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
0b3de9475305fb53e972b139a64cbf91ddbb97b712b0a1e322261509e65b12af
[root@izwz94goxqbb9ayvrlpgcaz ~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
b1aed3b45e7b9c4b8cb32f9e345783dde40eba5803a71e5f0f00c16d0ba21240
[root@izwz94goxqbb9ayvrlpgcaz ~]# docker network inspect mynet
[
    {
    
    
        "Name": "mynet",
        "Id": "04eb5e8aa466dc4cfd9bcac0673dafb2eda41287545abe4900546deb76aeac72",
        "Created": "2021-02-06T22:30:32.576364411+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
    
    
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
    
    
            "0b3de9475305fb53e972b139a64cbf91ddbb97b712b0a1e322261509e65b12af": {
    
    
                "Name": "tomcat-net-01",
                "EndpointID": "69905b3fdd1950c567db96da806d6be936f89dae4f8f537302298cece9e72d38",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "b1aed3b45e7b9c4b8cb32f9e345783dde40eba5803a71e5f0f00c16d0ba21240": {
    
    
                "Name": "tomcat-net-02",
                "EndpointID": "1b540ed1dcf4968c81b6ef20be0769ca91cd7504141caf02aa9a040ecdd0897c",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]

# 再测试pingip
[root@izwz94goxqbb9ayvrlpgcaz ~]# docker exec -it tomcat-net-01 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.066 ms
^C
--- 192.168.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 5ms
rtt min/avg/max/mdev = 0.063/0.078/0.107/0.022 ms

# 现在不用--link也可以ping名字了
[root@izwz94goxqbb9ayvrlpgcaz ~]# docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.052 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=0.056 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=6 ttl=64 time=0.057 ms
^C
--- tomcat-net-02 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 25ms
rtt min/avg/max/mdev = 0.050/0.052/0.057/0.009 ms

Our custom network docker has helped us maintain the corresponding relationship, and it is recommended that we usually use the network like this!

Benefits:
redis-different clusters use different networks to ensure that the cluster is safe and healthy
mysql-different clusters use different networks to ensure that the cluster is safe and healthy

Network connectivity

Idea:
Insert picture description here
Related commands:
Insert picture description here
Insert picture description here
test:

# tomcat 连通mynet
docker network connect mynet tomcat01

# mynet详细信息
docker network inspect mynet

After connecting, put tomcat01 under the mynet network

A container with two ip addresses Alibaba Cloud: public network ip, private network ip
Insert picture description here

docker exec -it tomcat01 ping tomcat-net-01

Screenshot:
Insert picture description here
Conclusion: Tomcat01 has opened up mynet, so tomcat01 can ping tomcat-net-01.

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/113731794