一、docker网络
1.1 理解docker0
- 清空docker中所有容器
docker rm -f $(docker ps -aq)
- 清空docker中所有镜像
docker rmi -f $(docker images -aq)
- ctrl+l是清屏快捷键,相当于clear
- ip addr查看服务器的IP地址信息,可以看到3个网卡信息
1.2 运行一个tomcat容器
docker run -d -P --name tomcat01 tomcat
- -P,随机映射本机与容器之间的端口号
[root@zhouhao ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27deb683e957 tomcat "catalina.sh run" 16 minutes ago Up 16 minutes 0.0.0.0:49155->8080/tcp, :::49155->8080/tcp tomcat01
查看容器内部网卡信息,新版tomcat没有ip addr命令,需要先安装该命令
- 进入容器安装
apt update && apt install -y iproute2
apt命令
apt(Advanced Packaging Tool)是一个在 Debian 和 Ubuntu 中的 Shell 前端软件包管理器。
apt 命令提供了查找、安装、升级、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。
apt 命令执行需要超级管理员权限(root)。
查看容器内部网卡信息
root@27deb683e957:/usr/local/tomcat# 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
70: eth0@if71: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
1.3 Linux服务器能否ping通docker容器内部?
[root@zhouhao ~]# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.049 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from 172.18.0.2: icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from 172.18.0.2: icmp_seq=4 ttl=64 time=0.067 ms
64 bytes from 172.18.0.2: icmp_seq=5 ttl=64 time=0.061 ms
- 结论是可以ping通
- dockers容器内IP地址是172.18.0.2,Linux服务器docker0的IP地址是172.18.0.1,是同一个网端
[root@zhouhao ~]# 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
inet6 ::1/128 scope host
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:0e:ac:db brd ff:ff:ff:ff:ff:ff
inet 172.17.139.29/20 brd 172.17.143.255 scope global dynamic eth0
valid_lft 314555882sec preferred_lft 314555882sec
inet6 fe80::216:3eff:fe0e:acdb/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:4c:c9:62:7c brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:4cff:fec9:627c/64 scope link
valid_lft forever preferred_lft forever
71: veth721544e@if70: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether aa:e7:6a:71:ee:4e brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::a8e7:6aff:fe71:ee4e/64 scope link
valid_lft forever preferred_lft forever
编号71的网卡,是启动docker容器后自动生成的网卡
1.4 网络原理
Linux服务器只要按装了docker,就会有一个docker0的网卡,是桥接模式,使用的是evth-pair技术。启动一个docker容器,docker就会给docker容器分配一个IP地址。
veth-pair技术
发现Linux服务器中的网卡信息,与docker容器中的网卡信息是对应关系,是成对出现的。
veth-pair技术是虚拟设备接口,一段连着协议,一段彼此相连。veth-pair充当一个桥梁,连接虚拟网络设备。
1.5 启动tomcat02容器
docker run -d -P --name tomcat02 tomcat
查看启动的容器
[root@zhouhao ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e124d494351 tomcat "catalina.sh run" 21 seconds ago Up 19 seconds 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp tomcat02
27deb683e957 tomcat "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:49155->8080/tcp, :::49155->8080/tcp tomcat01
进入tomcat02容器,并安装ip addr命令
docker exec -it tomcat02 /bin/bash
apt update && apt install -y iproute2
查看tomcat02容器的网卡信息
root@6e124d494351:/usr/local/tomcat# 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
72: eth0@if73: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
容器内安装ping命令
apt install iputils-ping
在 tomcat02容器内pingtomcat01容器
root@6e124d494351:/usr/local/tomcat# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time