【问题标题】:Not able to access internet inside docker windows container无法在 docker windows 容器内访问互联网
【发布时间】:2020-01-16 09:04:19
【问题描述】:

我正在尝试使用 docker windows 容器 构建映像。这是我的 dockerfile

FROM mcr.microsoft.com/windows/servercore:ltsc2019 as installer
RUN Invoke-WebRequest -URI 'www.google.com'

当我运行它时,它无法连接到 google.com

Invoke-WebRequest : The remote name could not be resolved: 'www.google.com'
At line:1 char:73

我在公司网络上工作,我尝试使用设置代理 RUN netsh winhttp set proxy proxy-server="http://proxy.com:80" bypass-list="*.xyz.com" 并且还导入了注册表HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

ipconfig /all inside my container 给出以下结果

Windows IP Configuration

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

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   Physical Address. . . . . . . . . : 00-15-5D-B4-55-54
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fd80::a1f4:ab74:983:af83%4(Preferred)
   IPv4 Address. . . . . . . . . . . : 172.xxx.xxx.xx(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.xxx.xxx.xx
   Default Gateway . . . . . . . . . : 172.xxx.xxx.xx
   DHCPv6 IAID . . . . . . . . . . . : 67114333
   DHCPv6 Client DUID. . . . . . . . : 00-01-40-01-25-D1-CB-C4-00-15-5D-D4-A5-54
   DNS Servers . . . . . . . . . . . : 172.xxx.xxx.xx
                                       10.xxx.xxx.xx
   NetBIOS over Tcpip. . . . . . . . : Disabled

我怀疑存在一些 dns/防火墙问题或没有以正确的方式设置代理。有人可以帮助我找出根本原因并解决此问题。

【问题讨论】:

  • 您在创建容器并尝试连接到 Internet 时是否遇到同样的错误? docker run -it mcr.microsoft.com/windows/servercore:ltsc2019 powershell 然后Invoke-WebRequest -Uri 'www.google.com' -UseBasicParsing 在容器内?

标签: docker docker-for-windows windows-container


【解决方案1】:

我也遇到了同样的问题,通过设置 DNS 解决了。

如果您在构建期间进行调用,您可以在您的 docker 守护进程中进行设置。 在 Docker Desktop 中,转到“设置”>“Docker 引擎”并将以下内容添加到 JSON 配置中:

"dns": ["1.1.1.1"]

https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-dns-options

如果您在运行时遇到问题,可以添加 dns 参数

--dns 1.1.1.1

https://docs.docker.com/engine/reference/run/#network-settings

【讨论】:

  • 这对我有用,谢谢!
【解决方案2】:

我遇到了类似的问题,这是因为我已连接到 VPN,并且没有在 Cisco AnyConnect 中启用“使用 VPN 时允许本地 (LAN) 访问(如果已配置)”设置。以防万一其他人遇到类似问题。

【讨论】:

    【解决方案3】:

    指定网络命令可以作为一种解决方法。适用于 buildrun

    docker build --network="Default Switch" .
    
    docker run --rm -it --network="Default Switch" mcr.microsoft.com/windows/servercore:ltsc2019
    

    这个名字来自这里:

    docker network ls
    NETWORK ID     NAME             DRIVER    SCOPE
    79b90200b5ad   Default Switch   ics       local
    07ffb031d4b7   nat              nat       local
    6682d6220de5   none             null      local
    

    由于某种原因,docker 默认 (nat) 没有互联网连接。

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 2021-01-26
      • 2020-10-18
      • 2018-05-01
      • 1970-01-01
      • 2014-07-11
      • 2021-09-23
      • 1970-01-01
      • 2020-12-16
      相关资源
      最近更新 更多