Docker网络是Docker容器之间和容器与外部网络之间的通信和连接的一种机制。在Docker中,每个容器都可以有自己的网络栈,包括网络接口、IP地址和网络配置。Docker网络提供了一种灵活且可定制的方式,使得容器之间可以相互通信,并与主机或其他网络资源进行交互。
Docker网络可以实现容器之间的互联以及端口映射,容器IP变动的时候可以通过服务名直接网络通信而不受影响。
命令 | 作用 |
---|---|
connect | 将容器连接到网络 |
create | 创建网络 |
disconnect | 断开容器与网络的连接 |
inspect | 显示一个或多个网络上的详细信息 |
ls | 列举所有网络 |
prune | 删除所有未使用的网络 |
rm | 删除一个或多个网络 |
通过 docker network ls
查看所有网络。
root@DESKTOP-R4LSK2C:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
4a0f7a00714f bridge bridge local
ff4eecf4c521 host host local
db2d9d647b48 none null local
通过docker network inspect 网络名称
查看网络的具体信息
root@DESKTOP-R4LSK2C:~# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "4a0f7a00714fda127c61e6359fd9ead4f9f29c738f7623e4bf695c8b7df7c61a",
"Created": "2023-06-25T09:08:49.753463804+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
通过 docker network create 网络名称
创建一个新的网络(自定义网络)。
root@DESKTOP-R4LSK2C:~# docker network create my_network
fe52e5328a98071302bf79c9740a7c07e6d9f6c3f7feee064311f4747e1bb5c2
启动一个tomcat8容器,查看网络连接状态。
root@DESKTOP-R4LSK2C:~# docker run -d -p 8082:8080 --name tomcat82 billygoo/tomcat8-jdk8
903c09561a0a98b97eb2c169ce2df3c43ed183dbf3c46e377f6c0dbbdb0e9571
通过 docker inspect 容器名称|ID
查看容器的网络信息,在 NetworkSettings
节点中可以看到详细信息。
root@DESKTOP-R4LSK2C:~# docker inspect tomcat82 | tail -n 40
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "5f1bf621976c7aa9797285805dff18ce63cad6d9f1706ac8ca1636e2edd9111d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/5f1bf621976c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4a0f7a00714fda127c61e6359fd9ead4f9f29c738f7623e4bf695c8b7df7c61a",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
通过 docker network connect 网络名称 容器名称
为容器连接新的网络模。
root@DESKTOP-R4LSK2C:~# docker network connect my_network tomcat82
发现以及连接了我们新建的网络。
root@DESKTOP-R4LSK2C:~# docker inspect tomcat82 | tail -n 40
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4a0f7a00714fda127c61e6359fd9ead4f9f29c738f7623e4bf695c8b7df7c61a",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
},
"my_network": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"01d6192dc23d"
],
"NetworkID": "",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": {}
}
}
}
}
]
通过 docker network disconnect 网络名称 容器名称
命令断开网络。
root@DESKTOP-R4LSK2C:~# docker network disconnect my_network tomcat82
root@DESKTOP-R4LSK2C:~# docker inspect tomcat82 | tail -n 40
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "5f1bf621976c7aa9797285805dff18ce63cad6d9f1706ac8ca1636e2edd9111d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/5f1bf621976c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4a0f7a00714fda127c61e6359fd9ead4f9f29c738f7623e4bf695c8b7df7c61a",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
通过 docker network rm 网络名称
删除网络。
root@DESKTOP-R4LSK2C:~# docker network rm my_network
aa
网络模式 | 简介 |
---|---|
bridge | 为每个容器分配,设置ip等,并将容器连接到一个docker0,虚拟网桥,默认该模式。 |
host | 容器将不会虚拟出自己的网卡,配置自己的Ip等,而是使用宿主机的IP和端口。 |
none | 容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair和网桥连接,IP等。 |
container | 而创建的容器不会创建自己的网卡和配置自己的IP,而是和一个指定的容器共享IP、端口范围等。 |
Docker 服务默认会创建一个 docker0 网桥(其上有一个 docker0 内部接口),该桥接网络的名称为docker0,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络。Docker 默认指定了 docker0 接口 的 IP 地址和子网掩码,让主机和容器之间可以通过网桥相互通信。
启动两个容器,启动容器要保证你的系统中有相应的镜像。
root@DESKTOP-R4LSK2C:~# docker run -d -p 8081:8080 --name tomcat81 billygoo/tomcat8-jdk8
b8f3c005d48a525c62b9e3c0c3d0b9d0d807cf066e1f137b0e95da3cc8cbde5d
root@DESKTOP-R4LSK2C:~# docker run -d -p 8082:8080 --name tomcat82 billygoo/tomcat8-jdk8
01d6192dc23d5a34dea3ef13d6a7ac6b476a3cbfbbdc2cee36287494d80165b2
查看docker的ip地址,会新增两个网卡veth86f068f@if5,vethb1f17be@if7。所以可以得出结论们每启动一个容器,就会多出一对网卡,同时他们被连接到docker0上,而docker0又和虚拟机之间连通。
root@DESKTOP-R4LSK2C:~# 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 mq state UP group default qlen 1000
link/ether 00:15:5d:86:0b:07 brd ff:ff:ff:ff:ff:ff
inet 172.22.78.81/20 brd 172.22.79.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe86:b07/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:36:f7:04:fc 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
inet6 fe80::42:36ff:fef7:4fc/64 scope link
valid_lft forever preferred_lft forever
6: veth86f068f@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 62:3f:7c:bf:04:b4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::603f:7cff:febf:4b4/64 scope link
valid_lft forever preferred_lft forever
8: vethb1f17be@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 7e:6b:dc:90:cd:6c brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::7c6b:dcff:fe90:cd6c/64 scope link
valid_lft forever preferred_lft forever
进入端口号为81的容器,查看容器的ip地址,会有eth0@if6这个网卡。
root@DESKTOP-R4LSK2C:~# docker exec -it tomcat81 bash
root@b8f3c005d48a:/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
5: eth0@if6: <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-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
进入端口号为82的容器,查看容器的ip地址,会有eth0@if8这个网卡。
root@DESKTOP-R4LSK2C:~# docker exec -it tomcat82 bash
root@01d6192dc23d:/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
7: eth0@if8: <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
从中我们可以知道只要我们创建了一个容器,docker就会新增一个网卡,同时容器也会新增一个网卡来与之对应,我们可以抽象为这样一个网络模型。
从以上的分析我们可以得出以下结论
通过上述,将宿主机上的所有容器都连接到这个内部网络上,两个容器在同一个网络下,会从这个网关下各自拿到分配的ip,此时两个容器的网络是互通的。
直接使用宿主机的 IP 地址与外界进行通信,不再需要额外进行NAT 转换。
我们可以使用host 网络模式需要在创建容器时通过参数 --net host
或者 --network host
指定,使用了host网络模式就不需要使用-p指定端口号,通过-p设置的参数将不会起到任何作用,端口号会以主机端口号为主,重复时则递增。
root@DESKTOP-R4LSK2C:~# docker run -d -p 8083:8080 --network host --name tomcat83 billygoo/tomcat8-jdk8
WARNING: Published ports are discarded when using host network mode
022bd58f324206ae8a0d059810bd94ace5c69613c1027b653d48a211f657d29c
正确的使用方法直接使用--net host
就可以了。
root@DESKTOP-R4LSK2C:~# docker run -d --net host --name tomcat83 billygoo/tomcat8-jdk8
3995d89c8a373c3d3aabfc1250141b712de118d355c9bcaf30ca8517a4d1d56a
查看docker的ip地址,我们可以发现使用host网络模式并没有在docker中并没有增加网卡。
root@DESKTOP-R4LSK2C:~# 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 mq state UP group default qlen 1000
link/ether 00:15:5d:86:0b:07 brd ff:ff:ff:ff:ff:ff
inet 172.22.78.81/20 brd 172.22.79.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe86:b07/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:36:f7:04:fc 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
inet6 fe80::42:36ff:fef7:4fc/64 scope link
valid_lft forever preferred_lft forever
6: veth86f068f@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 62:3f:7c:bf:04:b4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::603f:7cff:febf:4b4/64 scope link
valid_lft forever preferred_lft forever
8: vethb1f17be@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 7e:6b:dc:90:cd:6c brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::7c6b:dcff:fe90:cd6c/64 scope link
valid_lft forever preferred_lft forever
对比一下tomcat82容器和tomcat83容器的网络连接,发现host网络模式子安是没有IP地址和网关的,而bridge模式下且是有网关和IP地址。
root@DESKTOP-R4LSK2C:~# docker inspect tomcat83 | tail -n 20
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ff4eecf4c521cc1dadfd9352b22efec3c2094f2065234b20acf96e4c46cf3f3d",
"EndpointID": "49b9468a47e650e19b6ea2291973bc68f011abce58e0ce541f89d626c6e6f354",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
root@DESKTOP-R4LSK2C:~# docker inspect tomcat82 | tail -n 20
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4a0f7a00714fda127c61e6359fd9ead4f9f29c738f7623e4bf695c8b7df7c61a",
"EndpointID": "4135c3042cd5eacb8b42638f70b0ad967c7e9e8636524596aaeaec1a5e7bcf71",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
入tomcat83容器查看IP地址,可以发现和docker上网卡是一模一样的。
root@DESKTOP-R4LSK2C:/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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:86:0b:07 brd ff:ff:ff:ff:ff:ff
inet 172.22.78.81/20 brd 172.22.79.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe86:b07/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:36:f7:04:fc 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
inet6 fe80::42:36ff:fef7:4fc/64 scope link
valid_lft forever preferred_lft forever
6: veth86f068f@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 62:3f:7c:bf:04:b4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::603f:7cff:febf:4b4/64 scope link
valid_lft forever preferred_lft forever
8: vethb1f17be@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 7e:6b:dc:90:cd:6c brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::7c6b:dcff:fe90:cd6c/64 scope link
valid_lft forever preferred_lft forever
我们可以抽象为这样一个网络模型。
从以上的分析我们可以得出以下结论
容器将不会虚拟出自己的网卡而是使用宿主机的IP和端口
。新建的容器和已经存在的一个容器共享一个网络ip配置而不是和宿主机共享。在创建容器时通过参数
--net container:已运行的容器名称|ID
或者--network container:已运行的容器名称|ID
指定。
启动alpine1容器,查看IP地址。
root@DESKTOP-R4LSK2C:~# docker run -it --name alpine1 alpine /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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
9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
用container网络模式启动alpine2容器,让其共用alpine1容器的网络,查看alpine2容器的IP地址。
root@DESKTOP-R4LSK2C:~# docker run -it --network container:alpine1 --name alpine2 alpine /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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
9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
从以上分析我们可以得出以下结论。
新创建的容器不会创建自己的网卡,配置自己的IP,而是和一个指定的容器共享IP、端口范围等
。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。禁用网络功能,只有lo标识(环回地址:127.0.0.1)
启动tomcat84容器,使用none模式。
root@DESKTOP-R4LSK2C:~# docker run -d -p 8084:8080 --network none --name tomcat84 billygoo/tomcat8-jdk8
a3346e4a854bb8aa44490e01a5ae3f46b9c92da5d2600d0b6aa3814be39b7117
查看tomcat84容器的IP。
root@DESKTOP-R4LSK2C:~# docker exec -it tomcat84 bash
root@a3346e4a854b:/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
从以上分析我们可以得出结论。
虽然 Docker 提供的默认网络使用比较简单,但是为了保证各容器中应用的安全性,在实际开发中更推荐使用自定义的网络进行容器管理,以及启用容器名称到 IP 地址的自动 DNS 解析。
在上面的实例中我们已经创建了两个容器一个是tomcat81,一个是tomcat82,这两个容器都是使用bridge网络模式,现在我们来验证以下他们之间的网络通讯。我们进入两个容器互相ping对方的IP。
root@DESKTOP-R4LSK2C:~# docker exec -it tomcat81 /bin/bash
root@b8f3c005d48a:/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
5: eth0@if6: <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-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@DESKTOP-R4LSK2C:~# docker exec -it tomcat82 /bin/bash
root@01d6192dc23d:/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
7: eth0@if8: <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
root@b8f3c005d48a:/usr/local/tomcat# 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.126 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.050 ms
root@01d6192dc23d:/usr/local/tomcat# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.438 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.083 ms
64 bytes from 172.17.0.2: icmp_seq=5 ttl=64 time=0.099 ms
可以发现他们两个容器之间的网络通讯是正常的,那么我们ping对方的容器名呢?
root@01d6192dc23d:/usr/local/tomcat# ping tomcat81
ping: tomcat1: Name or service not known
root@b8f3c005d48a:/usr/local/tomcat# ping tomcat82
ping: tomcat2: Name or service not known
无法ping通对法的容器,因为IP是动态的我们不可能写死IP所以我们需要用自定义的网络模式来通过容器名来进行容器之间的通讯。
新建一个自定义的网络
root@DESKTOP-R4LSK2C:~# docker network create my_network
2fe7b062a07d8d6e04abfd7a8db523279afa63d1d27240e46b512c4b2a9612fd
停掉tomcat81和tomcat82容器,然后使用自定义网络模式的方式启动容器。
root@DESKTOP-R4LSK2C:~# docker stop tomcat82
tomcat82
root@DESKTOP-R4LSK2C:~# docker stop tomcat81
tomcat82
root@DESKTOP-R4LSK2C:~# docker run -d -p 8081:8080 --net my_network --name tomcat81 billygoo/tomcat8-jdk8
903c09561a0a98b97eb2c169ce2df3c43ed183dbf3c46e377f6c0dbbdb0e9571
root@DESKTOP-R4LSK2C:~# docker run -d -p 8082:8080 --net my_network --name tomcat82 billygoo/tomcat8-jdk8
20766ca1078657bc5ba1d30363c89992de30af3843d9f1059ce7f91b63fcc62d
进入容器,互相ping对方的容器名。
root@DESKTOP-R4LSK2C:~# docker exec -it tocat81 /bin/bash
root@903c09561a0a:/usr/local/tomcat# ping tocat82
PING tocat82 (172.19.0.3) 56(84) bytes of data.
64 bytes from tocat82.my_network (172.19.0.3): icmp_seq=1 ttl=64 time=0.269 ms
64 bytes from tocat82.my_network (172.19.0.3): icmp_seq=2 ttl=64 time=0.102 ms
64 bytes from tocat82.my_network (172.19.0.3): icmp_seq=3 ttl=64 time=0.094 ms
64 bytes from tocat82.my_network (172.19.0.3): icmp_seq=4 ttl=64 time=0.082 ms
root@DESKTOP-R4LSK2C:~# docker exec -it tocat82 /bin/bash
root@20766ca10786:/usr/local/tomcat# ping tocat81
PING tocat81 (172.19.0.2) 56(84) bytes of data.
64 bytes from tocat81.my_network (172.19.0.2): icmp_seq=1 ttl=64 time=0.200 ms
64 bytes from tocat81.my_network (172.19.0.2): icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from tocat81.my_network (172.19.0.2): icmp_seq=3 ttl=64 time=0.117 ms
从以上分析我们可以得出以下结论。
自定义网络本身就维护好了主机名和IP的对应关系(IP和域名都能通)
docker网络的内容就分享到这里啦,根据自己的学习我会进一步完善这篇文章,希望能帮到大家。