Docker容器可以访问Internet,但不能访问本地网络

时间:2020-09-30 17:28:57

标签: docker docker-networking

我在Win Server 2016上运行Windows docker容器

可以从网络上的其他节点访问容器中运行的站点。

该容器可以访问Internet(可以访问网络外部的第3方节点),但是无法连接到网络中的其他节点。

当容器中运行的应用尝试访问网络中另一台计算机(machine_name)上的服务时,会出现以下错误:

The remote name could not be resolved: machine_name

当应用尝试连接到网络上的数据库时:

A network-related or instance-specific error occurred while establishing a connection

因此,该容器似乎无权访问或无法在Intranet上找到计算机

我跑了docker exec -ti e87633560c6c ipconfig /all ,得到了以下内容:

Windows IP Configuration

   Host Name . . . . . . . . . . . . : gmsa_acct
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter vEthernet (Container NIC 0b35fe9f):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
   Physical Address. . . . . . . . . : 00-15-5D-30-F4-1D
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::7939:903e:141f:5c98%24(Preferred)
   IPv4 Address. . . . . . . . . . . : 172.22.223.136(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.22.208.1
   DNS Servers . . . . . . . . . . . : 172.22.208.1
                                       10.xxx
                                       10.xxx
   NetBIOS over Tcpip. . . . . . . . : Disabled

我在运行容器的机器上运行了此命令

docker exec e87633560c6c nltest /sc_verify:mydomain.com
Flags: b0 HAS_IP  HAS_TIMESERV
Trusted DC Name \\D1dns01.mydomain.com
Trusted DC Connection Status Status = 0 0x0 NERR_Success
Trust Verification Status = 0 0x0 NERR_Success
The command completed successfully

奇怪的是,同一容器在另一台主机上运行时没有任何问题。我们现在正在尝试在新主机上运行它,并遇到上述问题。

感谢您的帮助。

谢谢。

编辑:我能够通过IP地址而不是机器名称进行连接。 如何通过机器名称连接?

3 个答案:

答案 0 :(得分:1)

Docker在为其运行中的容器创建网络时,默认情况下会创建类型为bridge的NATed网络。您可以使用命令docker network ls完善有关容器网络的更多详细信息,其结果如下所示:

NETWORK ID          NAME                DRIVER              SCOPE
17e324f45964        bridge              bridge              local
6ed54d316334        host                host                local
7092879f2cc8        none                null                local

您可以尝试使用“主机”网络配置:

如果您将主机网络模式用于容器,则该容器的 网络堆栈未与Docker主机隔离(容器 共享主机的网络名称空间),而容器不 获取自己分配的IP地址。例如,如果您运行一个容器 绑定到端口80,并且您使用主机网络,则该容器的 主机IP地址上的端口80上提供了该应用程序。

Use host networking

答案 1 :(得分:0)

问题是您的DNS设置不起作用。 Windows上似乎有许多关于Docker的类似问题的报告(请参阅here)。但是,您可以尝试以下几种方法:

  • 升级到最新的docker版本(如果您还没有的话)
  • 重置为出厂设置(请参见here
  • 通过daemon.json文件分配静态DNS服务器,例如:

"dns" : ["8.8.8.8"]

答案 2 :(得分:0)

如@a_manfrinati答案中所述,默认网络brige由新容器使用。以您的情况来看,这两个节点都不在同一网络上运行。

尝试先创建一个网络,然后再向其添加节点:

$ docker network create my_new_network
$ docker container run -d --name node1 --network my_new_network node1
$ docker container run -d --name node2 --network my_new_network node2

还请注意,DNS名称将与容器名称相同。