自定义网络

我们尝试通过容器名来ping

[root@iZbp1htnoa70qlle394f3wZ ~]# docker exec -it tomcat02 ping tomcat01
ping: tomcat01: Name or service not known
# 发现ping不通

每次重启容器,容器的IP都会改变,
那有没有什么办法可以通过容器名来ping通呢?

  1. --link 容器互联 (不推荐)
  2. 自定义网络 (修护了docker0的缺点)

我们现在简单认识学习网络知识

[root@iZbp1htnoa70qlle394f3wZ ~]# docker network --help #查看docker网络帮助
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 # 移除一个或者多个网络
Run 'docker network COMMAND --help' for more information on a command.
# 创建网络帮助
[root@iZbp1htnoa70qlle394f3wZ ~]# 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网络

docker network create --driver (网络连接模式) --subnet (子网掩码) --gateway (网关地址) 自定义网络名称
[root@iZbp1htnoa70qlle394f3wZ ~]# docker network create --driver bridge --subnet 168.192.0.0/16 --gateway 168.192.0.1 mynet
4dbc2e7400cec3f7351ab114cce7b29b7b165abbae46312e932607030c75f242
[root@iZbp1htnoa70qlle394f3wZ ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
be9eaef1b50f bridge bridge local
4a9488e02a4e host host local
4dbc2e7400ce mynet bridge local
f15ddc928f82 none null local
[root@iZbp1htnoa70qlle394f3wZ ~]# docker network inspect 4dbc2e7400ce # 查看自定义网络信息
[
{
"Name": "mynet",
"Id": "4dbc2e7400cec3f7351ab114cce7b29b7b165abbae46312e932607030c75f242",
"Created": "2022-04-23T21:50:00.71398336+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "168.192.0.0/16",
"Gateway": "168.192.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]

使用我们自定义的网络

[root@iZbp1htnoa70qlle394f3wZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newtomcat 1.0 024406d08dd3 59 minutes ago 705MB
[root@iZbp1htnoa70qlle394f3wZ ~]# docker run -d -P --name tomcat01 --net mynet 024406d08dd3
ed44603b3099959dcf8fa48a3d29dd3df9e66d0f77b444a00caf834546b78790
[root@iZbp1htnoa70qlle394f3wZ ~]# docker run -d -P --name tomcat02 --net mynet 024406d08dd3
434910470e9f938c66f103edada34fd369ba6edd88f06144f3e557af363099cd
[root@iZbp1htnoa70qlle394f3wZ ~]# docker network inspect 4dbc2e7400ce
[
{
"Name": "mynet",
"Id": "4dbc2e7400cec3f7351ab114cce7b29b7b165abbae46312e932607030c75f242",
"Created": "2022-04-23T21:50:00.71398336+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "168.192.0.0/16",
"Gateway": "168.192.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"434910470e9f938c66f103edada34fd369ba6edd88f06144f3e557af363099cd": {
"Name": "tomcat02",
"EndpointID": "669929e0792b52ecd319853b6cbe598aa093961411ce80e9ea5d2e17dcf7f640",
"MacAddress": "02:42:a8:c0:00:03",
"IPv4Address": "168.192.0.3/16",
"IPv6Address": ""
},
"ed44603b3099959dcf8fa48a3d29dd3df9e66d0f77b444a00caf834546b78790": {
"Name": "tomcat01",
"EndpointID": "d8c8f4c058a464a409dfad9c8c30a2fbb45822a46a2666589e01dccdeea7ec0b",
"MacAddress": "02:42:a8:c0:00:02",
"IPv4Address": "168.192.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

容器之间相互ping

[root@iZbp1htnoa70qlle394f3wZ ~]# docker exec -it 434910470e9f ping 168.192.0.2 #容器02ping容器01
PING 168.192.0.2 (168.192.0.2) 56(84) bytes of data.
64 bytes from 168.192.0.2: icmp_seq=1 ttl=64 time=0.198 ms
64 bytes from 168.192.0.2: icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from 168.192.0.2: icmp_seq=3 ttl=64 time=0.129 ms
[root@iZbp1htnoa70qlle394f3wZ ~]# docker exec -it tomcat01 ping tomcat02 #我们发现使用我们自定义的网络,可以解决ping容器名的问题
PING tomcat02 (168.192.0.3) 56(84) bytes of data.
64 bytes from tomcat02.mynet (168.192.0.3): icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from tomcat02.mynet (168.192.0.3): icmp_seq=2 ttl=64 time=0.078 ms
posted @   小罗要有出息  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示