Not able to access internet inside docker windows container
Asked Answered
G

7

19

I am trying to build an image using docker windows container. Here is my dockerfile

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

When I run this it is failing to connect to google.com

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

I am working on a corporate network and I tried setting proxy using RUN netsh winhttp set proxy proxy-server="http://proxy.com:80" bypass-list="*.xyz.com" and also imported the registry HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

ipconfig /all inside my container gives below result

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

I suspect there is some dns/firewall issue or not setting proxy in a correct way. Can someone help me in identifying the root cause and fixing this issue.

Goldie answered 16/1, 2020 at 9:4 Comment(1)
Do you get the same error when you create a container and attempt to connect to the internet? docker run -it mcr.microsoft.com/windows/servercore:ltsc2019 powershell then Invoke-WebRequest -Uri 'www.google.com' -UseBasicParsing inside the container?Pedi
K
37

I have had the same problem and was able to resolve it setting the DNS.

If you are making the call during build time you can set it in your docker daemon. In Docker Desktop go to "Settings" > "Docker Engine" and add the following to the JSON config:

"dns": ["1.1.1.1"]

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

If you are experiencing the problem during run time you can add the dns argument

--dns 1.1.1.1

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

Kimi answered 3/4, 2020 at 11:44 Comment(5)
I am still shocked that this worked for me. Im on windows 11 docker-desktop and any internet build operation was totally flaky. changing the dns setting fixed the flaky inet operationsReport
OMG I tried everything this worked for me too! I have development DB in aws rds and need to develop locally with my container. This worked! I tried everything and couldn't connect but this did it! (I don't even know why it works lol)Flexile
In my case, a simple day all my dockers lost his internet access without reason, this works, thanksShipwright
This should be on every MS page related to Docker & Windows Containers. 3 days of pi$$in' about trying to get the "Quickstart" app to run :-(Overcloud
I tried this but it did not work. Then i tried 8.8.8.8 and it worked. Running docker desktop on windows 11Brief
S
10

Specifying network command can be used as a workaround. Works for both build and run

docker build --network="Default Switch" .

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

And the name is the one from here:

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

For some reason docker default (nat) doesn't have internet connection.

Stalingrad answered 8/4, 2021 at 10:11 Comment(1)
I got it fixed eventually so default (nat) started working, but don't remember exactly how. I think I had something misconfigured in my network card that it was using some weird Windows proxy.Stalingrad
O
5

Finally found a straightforward answer for this here https://github.com/docker/for-win/issues/2760#issuecomment-430889666

Basically need to set your internet connection as top priority:

  1. Find you internet interface e.g. 'Wi-Fi' Get-NetIPInterface -AddressFamily IPv4 | Sort-Object -Property InterfaceMetric -Descending

  2. Make it top priority by setting it to the lowest value Set-NetIPInterface -InterfaceAlias 'Wi-Fi' -InterfaceMetric 1

Orbit answered 16/8, 2022 at 15:21 Comment(1)
Another suggestion in the linked discussion was what did it for me: disabling the ethernet adapter. I'm on my laptop on wifi and Hyper-V was sharing the wrong network adapter with the underlying VM used by dockerMeliorism
S
4

I was struggling with a similar issue, and it was because I was connected to a VPN and hadn't enabled the setting "Allow local (LAN) access when using VPN (if configured)" in Cisco AnyConnect. Just in case anyone else is struggling with a similar issue.

Stlouis answered 17/8, 2020 at 2:43 Comment(0)
S
0

In Docker Desktop go to "Settings" > "Docker Engine" and add the below line to the

JSON config:
"dns":[8.8.8.8]
It looks like this:
{
  "dns": [
    "8.8.8.8"
  ],
  "experimental": false
}

Apply and Restart. Once restarted, go to any running container and do "ping google.com" it should work. Thanks

Stackhouse answered 5/8, 2023 at 10:49 Comment(0)
K
0

I had the same issue. What worked for me was to create a new network:

docker network create --driver nat my-new-network

Then I used the new network in the docker build command:

docker build --network=my-new-network .
Kus answered 12/10, 2023 at 10:25 Comment(0)
V
0

Configure Internet inside a Windows Hyper-V Docker Container

  1. Create an Internal Hyper-V Switch from Hyper-V Manager hyper v virtual switch
  2. Get IPv4 Address of your newly created internal switch using ipconfig cmd
  3. ipconfig cmd result
  4. RUN docker network prune
  5. RUN docker network create -d "nat" --subnet "[192.168.100.0]/24" my_nat Here 192.168.100 is coming from internal switch IPV4.
  6. retart docker
  7. Try again
Vitrain answered 19/4 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.