Docker-Windows容器无法解析主机

时间:2018-08-16 21:25:07

标签: docker docker-compose dockerfile docker-for-windows

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

在前一种情况下,我可以ping主机名(例如:google.com),并且解析起来很好。

pinging from linux container

但是,当运行Windows容器时,我不能做同样的事情。

windows network details and pinging from windows container

我正在运行自定义网络,以便确保容器在所需的子网中启动:

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

需要明确的是,我可以使用IP进行ping操作,但是由于我想通过主机名连接到数据库,因此在开发过程中并不是特别有用。

1 个答案:

答案 0 :(得分:1)

我刚刚弄清楚了。需要在Docker Desktop中“切换到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中创建外部虚拟交换机。选择您的wifi适配器。 (应在启用或禁用vpn的情况下使用)。

4)。重新启动。

5)。我使用了此图像,因为它必须与我的本地Windows Windows 10版本: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
相关问题