乐趣区

关于docker:Docker学习笔记-07Docker网络

docker0

装置 docker 的主机会有一个 docker0 网卡(桥接模式), 应用的是 evth-pair 技术
evth-pair 是虚构设施接口,成对呈现

ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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: <NO-CARRIER,BROADCAST,MULTICAST,UP> 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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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

–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": ""
            }
        },
退出移动版