首先需要明确的是,我的Docker容器在 NetworkSettings.Networks.bridge
下有一个IP地址。那个IP地址是 172.17.0.2
然而,在使用 docker run -p 3000:3000 node:8.10.0
运行后,我无法通过 http://172.17.0.2:3000/
访问该容器。
我的研究表明,在Windows本地使用Docker可能存在一些问题。根据这篇简短的指南:https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/,我发现需要使用网络NAT IP地址。我看到这种做法在许多地方被推荐。但是我的 docker inspect
中并没有这个部分。它返回:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "0c81d8f7ce5bcba70ec0487bbfe91bc6f221e29d9378c33708fac78d0ae47d3e",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3000"
}
]
},
"SandboxKey": "/var/run/docker/netns/0c81d8f7ce5b",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "bbe5f42a5b53c001bd3c2852bc6b22976fb9bab2afcb88b30f6660d7662f526f",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "0af66a62bc57ce25491c82f5a13d947ca79f5a879f3c1a6db40a90233eeb04f8",
"EndpointID": "bbe5f42a5b53c001bd3c2852bc6b22976fb9bab2afcb88b30f6660d7662f526f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
正如您所看到的,NAT部分完全丢失了。我找不到任何在线信息提及为什么会这样或者如何解决。我曾希望它缺失是一件好事,并且可以更容易地连接。但事实并非如此。非常感谢任何帮助。以下是dockerfile:
FROM node:8.10.0
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["./node_modules/gulp/bin/gulp.js", "assets"]
CMD [ "node", "app.js" ]
EXPOSE 3000
$DOCKER_HOST
是否被设置了任何值? - BMitch