Posted onEdited onInDevOpsViews: Word count in article: 2kReading time ≈7 mins.
Docker网络
Docker网络
Linux 网络
查看本地网络信息
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@sail ~]# 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 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:30:01:20 brd ff:ff:ff:ff:ff:ff inet 172.24.19.94/18 brd 172.24.63.255 scope global dynamic eth0 valid_lft 310201059sec preferred_lft 310201059sec 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:23:ae:ac:24 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
有三个网卡信息:
lo:本地。
ens:虚拟机或阿里云服务器地址。
docker0:Docker 网络地址。
Docker 网络
在 Docker 安装后,主机会为 Docker 分配一个网卡,名为 docker0 。
该网卡使用桥接模式,使用的是 veth-pair 技术。
启动两个容器
1 2 3 4 5 6 7 8 9 10
[root@sail ~]# docker run -d -p 8081:8080 --name=tomcat01 tomcat 29a06eab16e73f34458b77a520081083fe536d8eb34eb67dbb9c6632fc720687
[root@sail ~]# docker run -d -p 8082:8080 --name=tomcat02 tomcat 442add0d94cef631e0f531dff9d8f55b7e2f1aaeb088f742c3d8e240d4f9cc7d
[root@sail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 442add0d94ce tomcat "catalina.sh run"4 seconds ago Up 3 seconds 0.0.0.0:8082->8080/tcp tomcat02 29a06eab16e7 tomcat "catalina.sh run"15 seconds ago Up 14 seconds 0.0.0.0:8081->8080/tcp tomcat01
查看 Linux 网络
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[root@sailtomcat]# 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 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:16:3e:30:01:20 brd ff:ff:ff:ff:ff:ff inet 172.24.19.94/18 brd 172.24.63.255 scope global dynamic eth0 valid_lft 310199524sec preferred_lft 310199524sec 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:23:ae:ac:24 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 7: veth4a18f1b@if110: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 52:69:3c:bc:83:4a brd ff:ff:ff:ff:ff:ff link-netnsid0 9: veth296fd0d@if112: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 76:3c:34:e8:c4:90 brd ff:ff:ff:ff:ff:ff link-netnsid1
[root@sail ~]# docker exec -it tomcat01 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 6: eth0@if7: <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 link-netnsid0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@sail ~]# docker exec -it tomcat02 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 8: eth0@if9: <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 link-netnsid0 inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever
可以看到容器内 IP 与本机 IP 成对出现,这就是 veth-pair 技术。
容器访问 docker0 测试
1 2 3 4 5 6 7 8
[root@sail ~]# docker exec -it tomcat01 ping 172.17.0.1 PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data. 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.845 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.139 ms 64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.130 ms 64 bytes from 172.17.0.1: icmp_seq=4 ttl=64 time=0.134 ms 64 bytes from 172.17.0.1: icmp_seq=5 ttl=64 time=0.119 ms 64 bytes from 172.17.0.1: icmp_seq=6 ttl=64 time=0.082 ms
[root@sail ~]# docker network ls NETWORK ID NAME DRIVER SCOPE f3eeb014197a bridge bridge local 28d77e958643 host host local c3ff850e96f0 none null local
[root@sail ~]# docker network --help Usage: docker network COMMAND Manage networks Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove one or more networks Run 'docker network COMMAND --help'for more information on a command.
[root@sail ~]# docker network ls NETWORK ID NAME DRIVER SCOPE f3eeb014197a bridge bridge local 28d77e958643 host host local 801fbbe1b38c mynet bridge local c3ff850e96f0 none null local
[root@sail ~]# docker network ls NETWORK ID NAME DRIVER SCOPE f3eeb014197a bridge bridge local 28d77e958643 host host local 801fbbe1b38c mynet bridge local c3ff850e96f0 none null local
[root@sail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a02cd4172da tomcat "catalina.sh run"5 minutes ago Up 5 minutes 0.0.0.0:49159->8080/tcp tomcat02-net
[root@sail ~]# docker inspect 5a02cd4172da
这里也可以发现容器 tomcat02-net 已经与 mynet 建立了连接。
测试连接
1 2 3 4 5 6
[root@sail ~]# docker exec -it tomcat02 ping tomcat02-net PING tomcat02-net (192.168.0.2) 56(84) bytes of data. 64 bytes from tomcat02-net.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.121 ms 64 bytes from tomcat02-net.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.064 ms ^C --- tomcat02-net ping statistics ---