先不讨论为什么会有这么奇葩的需求
windows
虚拟机CentOS
Docker 容器
[root@localhost zookeeper]# docker network ls
NETWORK ID NAME DRIVER SCOPE
d12c5590cb04 bridge bridge local
8f18d5d4a41c host host local
f6adefbb5345 none null local
a908ee8eaa13 zookeeper_default bridge local
[root@localhost zookeeper]# docker network
[root@localhost zookeeper]# docker network inspect zookeeper_default
[
{
"Name": "zookeeper_default",
"Id": "a908ee8eaa131161aa5d7687c0b8bfc23a205701001aa1161ed41082a3dfb702",
"Created": "2019-06-25T14:07:37.889856326+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"d0c74dbea38bf285eb72727f85fe514ff71ae3d10a3294c681898692ccdda6ab": {
"Name": "zookeeper_2",
"EndpointID": "f5450cf472a517833e81645abb7af723f97a81ab7d0b0b7f3cfd44d4d45d91be",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"e21b074d2fd14521fe28340287362a1005eb7b677b6211bb95956f1f06d941a1": {
"Name": "zookeeper_1",
"EndpointID": "da0c18ff9a0d1e379ef8a998cebe461546b687a116e14328c486e016c0ad7c8b",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"edaf9da55460ff4de0e43e9d060e7382816df38af9b3c257dd8154f445049550": {
"Name": "zookeeper_3",
"EndpointID": "d00a7b694a698c6bd2ef663cbfcb0f2ff1749ba160504f9559f164eb3ca5d407",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "zookeeper",
"com.docker.compose.version": "1.25.0-rc1"
}
}
]
[root@localhost zookeeper]#
[root@localhost zookeeper]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d0c74dbea38b zookeeper:3.5.3-beta "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 2888/tcp, 3888/tcp, 0.0.0.0:2182->2181/tcp zookeeper_2
e21b074d2fd1 zookeeper:3.5.3-beta "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp zookeeper_1
edaf9da55460 zookeeper:3.5.3-beta "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 2888/tcp, 3888/tcp, 0.0.0.0:2183->2181/tcp zookeeper_3
[root@localhost zookeeper]#
环境基本如上
目标,从windows可以ping通docker容器
如果此时在宿主机windows中ping Docker容器是ping不同的,因为在宿主机上没有通往172.18.0.0/16网络的路由,宿主机会将发往172.18.0.0/16网络的数据发往默认路由,这样就无法到达容器。
解决方法:
1. 首先要保证在虚拟机中能够连接到Docker容器中,用ping测试是否通畅
2. 关闭虚拟中的防火墙: systemctl stop firewalld.service
3. 打开宿主机(windows)的cmd,在其中添加通往172.18.0.0/16网络的路由。
通往172.18.0.0/16网络的数据包由192.168.1.101来转发
route print 172.18.0.0
route add 172.18.0.0 mask 255.255.0.0 192.168.1.101
配置完成!