查看所有的
docker
网络
- 命令:
docker network ls
[root@VM-0-6-centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
cb59eddef222 bridge bridge local
c86310353602 host host local
1ab8f2e4028d none null local
- 网络模式
bridge
:桥接(docker
默认,自己创建也是用bridge
模式)
none
:不配置网络,一般不用
host
:和宿主机共享网络
container
:容器网络连通(用得少!局限很大)
测试:
- 先删除所有的容器:
docker rm -f $(docker ps -aq)
[root@VM-0-6-centos ~]# docker rm -f $(docker ps -aq)
54dd5c049c26
ec958a1ad973
168c135e0325
- 查看IP:
ip addr
[root@VM-0-6-centos ~]# 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 fq_codel state UP group default qlen 1000
link/ether 52:54:00:fd:3f:be brd ff:ff:ff:ff:ff:ff
inet 172.21.0.6/20 brd 172.21.15.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fefd:3fbe/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:84:b7:a3:07 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:84ff:feb7:a307/64 scope link
valid_lft forever preferred_lft forever
- 直接启动的命令
--net bridge
,就是docker0
docker run -d -P --name tomcat01 tomcat
// 等价于
docker run -d -P --name tomcat01 --net bridge tomcat
docker0
,特点:默认,域名不能访问。 --link
可以打通连接,但是很麻烦!
自定义一个网络:
- 创建:
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
--driver bridge
:桥接(默认)
--subnet 192.168.0.0/16
:子网地址
--gateway 192.168.0.1
:网关地址
mynet
:自定义网络名称
[root@VM-0-6-centos ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
8e8d25d2e62dc4cc13c707564ee2b2b60ffbd26eeebd783b773b97f4e0dd3886
- 查看网络:
docker network ls
[root@VM-0-6-centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
cb59eddef222 bridge bridge local
c86310353602 host host local
8e8d25d2e62d mynet bridge local // 自定义的网络
1ab8f2e4028d none null local
- 查看自定义网络信息:
docker network inspect mynet
[root@VM-0-6-centos ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "8e8d25d2e62dc4cc13c707564ee2b2b60ffbd26eeebd783b773b97f4e0dd3886",
"Created": "2021-07-04T15:10:14.398657113+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
启动两个
tomcat
,再次查看网络情况
- 创建并启动容器
tomcat-net01
:docker run -d -P --name tomcat-net01 --net mynet tomcat
--net mynet
:使用自定义的网络mynet
[root@VM-0-6-centos ~]# docker run -d -P --name tomcat-net01 --net mynet tomcat
335227f5c489679ec80d1d113f4b5b859507fa9bcb7afd8865efa4c3cc5aca95
- 创建并启动容器
tomcat-net02
:
[root@VM-0-6-centos ~]# docker run -d -P --name tomcat-net02 --net mynet tomcat
1f911c743c32ddff4796ccf02837a23307cb354c2e23dc7c83674ca53e4aa5fb
- 查看自定义网络信息:
docker network inspect mynet
[root@VM-0-6-centos ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "8e8d25d2e62dc4cc13c707564ee2b2b60ffbd26eeebd783b773b97f4e0dd3886",
"Created": "2021-07-04T15:10:14.398657113+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1f911c743c32ddff4796ccf02837a23307cb354c2e23dc7c83674ca53e4aa5fb": {
"Name": "tomcat-net02",
"EndpointID": "fdc320da7ef492fd80d8853e7a77553e49b78e98ac914109d821c8874854e853",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16", // tomcat-net02 地址
"IPv6Address": ""
},
"335227f5c489679ec80d1d113f4b5b859507fa9bcb7afd8865efa4c3cc5aca95": {
"Name": "tomcat-net01",
"EndpointID": "38bc85cb94faf44ea0a856f27d6b4be8e2afe8574509b44e375d1b9f5c64a16d",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16", // tomcat-net03 地址
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
连接测试:
tomcat-net01
ping
tomcat-net02
- 用
IP
的方式测试:
[root@VM-0-6-centos ~]# docker exec -it tomcat-net01 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.089 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.068 ms
可以 ping
通
- 用容器名的方式测试:
[root@VM-0-6-centos ~]# docker exec -it tomcat-net01 ping tomcat-net02
PING tomcat-net02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.189 ms
64 bytes from tomcat-net02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.076 ms
可以 ping
通
- 反向测试:
[root@VM-0-6-centos ~]# docker exec -it tomcat-net02 ping tomcat-net01
PING tomcat-net01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from tomcat-net01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.065 ms
可以 ping
通。在自定义的网络下,容器间可以互相ping
通,不需使用 --link
自定义网络的好处:
- 不同的集群使用不同的网络,保证集群是安全和健康的:如
redis
集群、mysql
集群,可以单独构建不同的网络,使网络相互隔离。