[英]How to access Docker container through DockerNAT IP address on Windows 10?
I am using a course to learn how to Dockerize my ASP.NET Core application.我正在使用一门课程来学习如何 Dockerize 我的 ASP.NET Core 应用程序。 I have a networking issue with the token server I am trying to use in my configuration.
我在我的配置中尝试使用的令牌服务器存在网络问题。
The ASP.NET Core Web application ( webmvc
) allows authorization through a token server ( tokenserver
). ASP.NET Core Web 应用程序 (
webmvc
) 允许通过令牌服务器 ( tokenserver
) 进行授权。
tokenserver令牌服务器
tokenserver:
build:
context: .\src\Services\TokenServiceApi
dockerfile: Dockerfile
image: shoes/token-service
environment:
- ASPNETCORE_ENVIRONMENT=ContainerDev
- MvcClient=http://localhost:5500
container_name: tokenserviceapi
ports:
- "5600:80"
networks:
- backend
- frontend
depends_on:
- mssqlserver
tokenserver
knows about the webmvc
url. tokenserver
知道webmvc
url。
webmvc微博
webmvc:
build:
context: .\src\Web\WebMvc
dockerfile: Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=ContainerDev
- CatalogUrl=http://catalog
- IdentityUrl=http://10.0.75.1:5600
container_name: webfront
ports:
- "5500:80"
networks:
- frontend
depends_on:
- catalog
- tokenserver
Running the container confirms that webmvc
will try to reach the identity server at http://10.0.75.1:5600
.运行容器确认
webmvc
将尝试访问位于http://10.0.75.1:5600
的身份服务器。
By running ipconfig
in my Windows machine I confirm that DockerNAT is using 10.0.75.1
:通过在我的 Windows 机器上运行
ipconfig
我确认 DockerNAT 正在使用10.0.75.1
:
Ethernet adapter vEthernet (DockerNAT):
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 10.0.75.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
http://10.0.75.1:5600/
is not accessible when accessed from the host machine while http://localhost:5600
is accessible.从主机访问时
http://10.0.75.1:5600/
不可访问,而http://localhost:5600
可访问。
However, I have to rely on DockerNAT IP because webmvc
must access the service from its own container where localhost:5600 does not make sense:但是,我必须依赖 DockerNAT IP,因为
webmvc
必须从其自己的容器访问服务,其中 localhost:5600 没有意义:
docker exec -it webfront bash
root@be382eb4608b:/app# curl -i -X GET http://10.0.75.1:5600
HTTP/1.1 404 Not Found
Date: Fri, 03 Jan 2020 08:55:48 GMT
Server: Kestrel
Content-Length: 0
root@be382eb4608b:/app# curl -i -X GET http://localhost:5600
curl: (7) Failed to connect to localhost port 5600: Connection refused
"HostConfig": {
"Binds": [],
....
"NetworkMode": "shoesoncontainers_backend",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "5600"
}
]
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "6637a47944251a4dc59205dc6e03670bc4b03f8bf38a7be0dc11b72adf6a3afa",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5600"
}
]
},
"SandboxKey": "/var/run/docker/netns/6637a4794425",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"shoesoncontainers_backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"tokenserver",
"d31d9b5f4ec7"
],
"NetworkID": "a50a9cee66e6a65a2bb90a7035bae4d9716ce6858a17d5b22e147dfa8e33d686",
"EndpointID": "405b1beb5e20636bdf0d019b36494fd85ece86cfbb8c2d57283d64cc20e5d760",
"Gateway": "172.28.0.1",
"IPAddress": "172.28.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:1c:00:04",
"DriverOpts": null
},
"shoesoncontainers_frontend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"tokenserver",
"d31d9b5f4ec7"
],
"NetworkID": "b7b3e8599cdae7027d0bc871858593f41fa9b938c13f906b4b29f8538f527ca0",
"EndpointID": "e702b29016b383b7d5872f8c55cad0f189d6f58f2631316cf0313f3df30331c0",
"Gateway": "172.29.0.1",
"IPAddress": "172.29.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:1d:00:03",
"DriverOpts": null
}
}
}
I have also created an inbound rule for port 5600 in Windows Defender Firewall with Advanced Security.我还在具有高级安全性的 Windows Defender 防火墙中为端口 5600 创建了一个入站规则。
Question: How to access Docker container through DockerNAT IP address on Windows 10?问题:如何在 Windows 10 上通过 DockerNAT IP 地址访问 Docker 容器?
I think you are looking for host.docker.internal
.我认为您正在寻找
host.docker.internal
。 It's a special DNS name which allow you to connect from a container to a service on the host or a container exposed on the host.它是一个特殊的 DNS 名称,允许您从容器连接到主机上的服务或主机上公开的容器。
The official documentation .官方文档。
I am not sure why it does not work as expected, but using the information provided here I was able to figure out how to make it work:我不确定为什么它不能按预期工作,但是使用此处提供的信息,我能够弄清楚如何使其工作:
You can try add incoming rule in firewall:您可以尝试在防火墙中添加传入规则:
Example:例子:
protocol: any/tcp/udp/... program: any action: allow local port: any remote port: any local address: 10.0.75.1 remote address: 10.0.75.0/24协议:any/tcp/udp/... 程序:任何动作:允许本地端口:任何远程端口:任何本地地址:10.0.75.1 远程地址:10.0.75.0/24
or you can try use address 10.0.75.2 instead of 10.0.75.1或者您可以尝试使用地址 10.0.75.2 而不是 10.0.75.1
For me the second solution worked.对我来说,第二种解决方案有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.