我们尝试通过容器名来ping
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| ping: tomcat01: Name or service not known |
| |
| |
每次重启容器,容器的IP都会改变,
那有没有什么办法可以通过容器名来ping通呢?
- --link 容器互联 (不推荐)
- 自定义网络 (修护了docker0的缺点)
我们现在简单认识学习网络知识
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| |
| 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 ~] |
| |
| 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 ~] |
| 4dbc2e7400cec3f7351ab114cce7b29b7b165abbae46312e932607030c75f242 |
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| NETWORK ID NAME DRIVER SCOPE |
| be9eaef1b50f bridge bridge local |
| 4a9488e02a4e host host local |
| 4dbc2e7400ce mynet bridge local |
| f15ddc928f82 none null local |
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| [ |
| { |
| "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 ~] |
| REPOSITORY TAG IMAGE ID CREATED SIZE |
| newtomcat 1.0 024406d08dd3 59 minutes ago 705MB |
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| ed44603b3099959dcf8fa48a3d29dd3df9e66d0f77b444a00caf834546b78790 |
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| 434910470e9f938c66f103edada34fd369ba6edd88f06144f3e557af363099cd |
| [root@iZbp1htnoa70qlle394f3wZ ~] |
| [ |
| { |
| "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 ~] |
| 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 ~] |
| 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 |
· 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语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决