docker安装之后自带三个网络,可以通过ip addr来查看。
docker是如何处理容器网络的访问的?
#1 启动一个tomcat
docker run -d -P --name tomcat01 tomcat
#2 查看容器的内部网络地址 在命令后追加 ip addr 发现容器启动的时候会得到一个eth0@if114 ip地址,docker分配的
[root@iZuf65o9ovhniso4w0oji5Z ~]# docker exec -it newtomcat01 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
113: eth0@if114: <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-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# 思考 linux服务器是否可以ping通tomcat容器
[root@iZuf65o9ovhniso4w0oji5Z ~]# 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.088 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.074 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.063 ms
#linux服务器可以ping通tomcat容器
1、每启动一个docker容器,docker就会给容器分配一个ip,当我们安装docker的时候,会有一个桥接模式的网卡-docker0,它使用的技术是evth-pair!
再次测试ip addr出现了114的网卡
[root@iZuf65o9ovhniso4w0oji5Z ~]# 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:20:b0:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.0.201/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 310111830sec preferred_lft 310111830sec
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:c9:f9:ba:5c 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
114: veth905b50f@if113: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether fe:8a:d6:9f:5d:9d brd ff:ff:ff:ff:ff:ff link-netnsid 1
# 容器启动带来的网卡,都是成对出现的,一端连着协议,一端彼此相连,正是通过veth-pair来连接各种虚拟网络设备。
# 在启动一个tomcat02
ip addr 发现又多了一对网卡115-116
3、测试tomcat01和tomcat02是否可以ping通
结论:容器和容器之间是互相ping通的。
网络模型图
遇到的问题:
Q1:
[root@iZuf65o9ovhniso4w0oji5Z ~]# docker run -d -P --name newtomcat tomcat
docker: Error response from daemon: driver failed programming external connectivity on endpoint newtomcat (39b1a5d363a56a0626796b96e4a9bf839262bf4b5123995545f796a59bd9b16f): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 49175 -j DNAT --to-destination 172.17.0.9:8080 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
出现这个问题,重启docker可以解决。用systemctl restart docker命令。