Docker - Windows 容器未解析主机

Docker - Windows Container Not Resolving Hosts

我已经使用 Docker 工具在 Visual Studio 中建立了两个新项目。第一个是针对 Linux 容器的 asp.net 站点 运行。 第二个是针对 Windows 容器的 asp.net 站点 运行。

在前者中,我可以 ping 主机名(例如:google.com)并且它解析得很好。

然而,当 运行 windows 容器时,我不能做同样的事情。

我是 运行 自定义网络,这样我可以确保容器在我想要的子网上启动:

docker network create --driver=nat --subnet=192.168.221.0/24

需要说明的是,我可以使用 IP ping 正常,但由于我想通过主机名连接到数据库,这在开发过程中并不是特别有用。

我刚刚弄明白了。需要在 Docker 桌面中“切换到 windows 容器”。

1).关注:https://docs.docker.com/machine/drivers/hyper-v/#example:

2).启动 hyper v(可能需要启用):https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v

3).然后在 hyper v 中创建外部虚拟交换机。 Select 你的 wifi 适配器。 (应该在打开或关闭 vpn 的情况下工作)。

4).重启。

5).我使用此图像是因为它必须与我的本地 windows windows 10 version:1809

匹配
docker pull mcr.microsoft.com/windows:1809   #takes an hour to finish

6).启动容器并附加到新网络。

docker run -dit --name win1809 mcr.microsoft.com/windows:1809 powershell
docker network ls
docker network connect "John Windows Container Switch" win1809
docker network inspect "John Windows Container Switch"

显示:

        "Containers": {
            "b8c4ae07761fdf082602f836654013b8d83a717cce9156880a80c7542d855842": {
                "Name": "win1809",
                "EndpointID": "e84652fc93fd1fa2970c3bdcad513d8928fc35823a9f8cf0e638926b6091a60c",
                "MacAddress": "00:15:5d:fb:77:dd",
                "IPv4Address": "",
                "IPv6Address": ""

7).连接到容器并 ping 一些东西:

docker exec -it win1809 powershell
ping www.google.com

Pinging www.google.com [172.217.10.36] with 32 bytes of data:
Reply from 172.217.10.36: bytes=32 time=19ms TTL=118
Reply from 172.217.10.36: bytes=32 time=18ms TTL=118
Reply from 172.217.10.36: bytes=32 time=18ms TTL=118
Reply from 172.217.10.36: bytes=32 time=14ms TTL=118