Docker学习笔记 07Docker网络

docker0

安装docker的主机会有一个docker0网卡(桥接模式),使用的是evth-pair技术
evth-pair是虚拟设备接口,成对出现
ip addr

1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo...
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 172.17.0.16/20 brd 172.17.15.255 scope global eth0...
3: docker0:  mtu 1500 qdisc noqueue state DOWN group default
    inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0...

/16是网络标识位,子网掩码是255.255.0.0,/24为255.255.255.0

容器启动的时候docker会分配给容器一个eth@ifxxx IP地址,可以在外部ping通
docker run -it -P --name tomcat01 tomcat
docker exec -it b4da99b29aef /bin/bash
ip addr

1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
62: eth0@if63:  mtu 1500 qdisc noqueue state UP group default 
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever
每启动一个容器,宿主机也会多一个网卡
63: vethb4f3058@if62:  mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether f6:e4:da:9e:1c:bc brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::f4e4:daff:fe9e:1cbc/64 scope link 
       valid_lft forever preferred_lft forever
结论:容器间可以相互ping通
docker run -d -P --name tomcat02 tomcat
docker exec -it tomcat02 /bin/bash
ping ping 172.18.0.2

Docker学习笔记 07Docker网络_第1张图片

--link

docker run -d -P --name tomcat03 --link tomcat02 tomcat

docker exec -it tomcat03 ping tomcat02

查看网络

docker network ls

docker network inspect 网卡ID

--link 就是在tomecat03容器的hosts中写入172.18.0.3 tomcat02 容器ID

缺点:docker0不支持容器名访问!

自定义网络

创建自定义网络

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
e7e2c109f027   mynet     bridge    local

--driver bridge 桥接
--subnet 子网
--gateway 网关

docker run 默认执行 --net bridge 参数
使用自定义网络

docker run -d -P --name tomcat01 --net mynet tomcat
docker run -d -P --name tomcat02 --net mynet tomcat

docker exec -it tomcat01 ping tomcat02

网络联通

docker network connect 网络 容器名

测试

docker run -d -P --name tomcat03 tomcat

#接入网络
docker network connect mynet tomcat03

#测试
docker exec -it tomcat01 ping tomcat03

#查看网络
docker inspect mynet

"Containers": {
            "02a670496edfd99d1542ddc3f871d53b5e66295c1b9228f119f9262ee362bcc4": {
                "Name": "tomcat03",
                "EndpointID": "ee626c9944c7732c10234feb78d7e6e59877d4ad3c531690cbf6af23103cb581",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            },
            "b42c7b942d856b83af449e14696dc773f34d63acc5ca6d576ce2493763cc6d89": {
                "Name": "tomcat01",
                "EndpointID": "392deab3c2848252c99b122f655ed903c2f7b33b136a585dfb845105cb24150a",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "e84de7e8d89aa59ffd866ec4620a13c4b49fa510dac6989cdba9b6b870602571": {
                "Name": "tomcat02",
                "EndpointID": "4461c14570c1a2fa87b43c15843b295af6f2a4551f9a7fcda5a27418cf6b8450",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            }
        },

你可能感兴趣的:(docker)