简体   繁体   English

如何在 Windows 10 上通过 DockerNAT IP 地址访问 Docker 容器?

[英]How to access Docker container through DockerNAT IP address on Windows 10?

Overview概述

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 ) 进行授权。

docker-compose for the services docker-compose 用于服务

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

Token service container inspect (relevant parts)令牌服务容器检查(相关部分)

"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 .官方文档

You can fine longer explanations here .你可以在这里做更长的解释。

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.

相关问题 如何在 Docker 桌面 Windows 10 中使用 IP ADDRESS 从主机访问容器(尤其是使用 Z05E406053C418A2DA1) - How to access container from host using IP ADDRESS in Docker Desktop Windows 10 (esspecially with use docker compose)? Windows 10上的docker:容器没有IP地址 - docker on windows 10: container has no ip address Docker-Windows上容器的IP地址 - Docker - IP address of container on Windows 如何为Windows容器给docker分配一个自己的IP地址? - How do I give an own ip address to docker for Windows container? 如何从 Windows 主机获取 Docker 容器的 IP 地址 - How to get a Docker container's IP address from the Windows host 如何从主机访问 Docker 容器的内部 IP 地址? - How to access Docker container's internal IP address from host? 如何通过 ip 地址访问 docker 容器 - How can I access a docker container via ip address 无法使用 IP 地址访问本地 docker 容器 - Unable to access local docker container with IP address Windows Docker容器没有NAT IP地址。 无法在本地访问容器 - Windows Docker Container has no NAT IP Address. Cannot access container locally 如何访问没有ip地址的postgres-docker容器other docker容器 - How to access postgres-docker container other docker container without ip address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM