Docker自定义网络
查看所有的docker网络
# 查看docker network的帮助命令
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
# 查看docker network
docker network ls
NETWORK ID NAME DRIVER SCOPE
94fc848a4fd9 bridge bridge local
7340c21cddff host host local
1338cd2e6834 none null local
网络模式
bridge: 桥接模式(默认,自己创建也使用bridge)
none:不配制网络
host:和宿主机共享网络
container:容器内网络联通!(用的少,局限性很大)
测试
# 我们直接启动的命令 --net bridge, 这个是我们的docker0
docker run -d -P --name tomcat01 --net bridge tomcat
# docker0的特点, 默认的,域名是不能访问的, --link是可以打通连接
# 自定一个网络
docker network create --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by
Network driver (default map[])
--config-from string The network from which to copy the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network‘s scope
--subnet strings Subnet in CIDR format that represents a network segment
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c
# 查看自定义的网络是否创建成功
docker network ls
NETWORK ID NAME DRIVER SCOPE
94fc848a4fd9 bridge bridge local
7340c21cddff host host local
bfb8374fa9c4 mynet bridge local
1338cd2e6834 none null local
# 查看自己创建的自定义网络
docker network inspect mynet
[
{
"Name": "mynet",
"Id": "bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c",
"Created": "2021-07-10T06:34:17.9819573Z",
"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": {}
}
]
# 测试下自己的容器网络
docker run -d -P --name tomcat-net-01 --net mynet tomcat
d18bedc300d7f7f5e854875ac88594b56311bb417c223cd89f8fed02178a614e
docker run -d -P --name tomcat-net-02 --net mynet tomcat
283b394949d0230ce159e675f12f765fa6e7c9978d8103521d75da5b95ffde46
# 启动容器后,查看自己创建网络的容器情况
docker network inspect mynet
[
{
"Name": "mynet",
"Id": "bfb8374fa9c4f7822777e8e30ed25bb450ee46151377cd954425697d34fd2a7c",
"Created": "2021-07-10T06:34:17.9819573Z",
"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": {
"283b394949d0230ce159e675f12f765fa6e7c9978d8103521d75da5b95ffde46": {
"Name": "tomcat-net-02",
"EndpointID": "784507211c5661cae82fc7461d866af69669bc440d48e821e31144b5fd4a7877",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"d18bedc300d7f7f5e854875ac88594b56311bb417c223cd89f8fed02178a614e": {
"Name": "tomcat-net-01",
"EndpointID": "7ddc1493e42c89ef3793bdfcc273df3c8513693d342a613c670846ac2101a302",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 查看下自定义网络是否能ping通容器的ip
docker exec -it tomcat-net-01 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.171 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.306 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.349 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.239 ms
64 bytes from 192.168.0.3: icmp_seq=5 ttl=64 time=0.341 ms
64 bytes from 192.168.0.3: icmp_seq=6 ttl=64 time=0.383 ms
# 查看自定义的网络是否能ping通容器名
docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.291 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.317 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.451 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=1.39 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=6 ttl=64 time=0.257 ms
自定义网络的好处
不同的集群使用不同的网络,保证集群是安全和健康的