Can't access internet inside docker windows container inside corporate proxy

Stef asked:

I am needing to connect to the internet from inside a container.

Initially the Docker daemon was not able to connect to the internet and I was unable to even run:

docker pull hello-world

I resolved this by specifying a manual proxy in the docker desktop GUI settings, under resources
Once I manually set the proxy I was able to pull the hello-world image – so I know this proxy works.

I then created a test Dockerfile to see if I could get a container to connect to the internet when building an image, and this is the Dockerfile:

FROM mcr.microsoft.com/windows/servercore:1903
RUN curl 'www.google.co.za'

and got this output:

PS C:\Projects\Test> docker build -t test:01 .

Sending build context to Docker daemon  3.906GB

Step 1/2 : FROM mcr.microsoft.com/windows/servercore:1903

---> 66c07b0d3e85

Step 2/2 : RUN curl 'www.google.co.za'

---> Running in 99f79ea70731

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: 'www.google.co.za'

The command 'cmd /S /C curl 'www.google.co.za'' returned a non-zero code: 6

As was suggested here I tried adding a DNS as follows:

"dns": ["1.1.1.1"]

but got the same result as above.
I had previously had issues in Linux containers where I couldn’t install packages inside the container and I had to add the following to the Dockerfile which worked:

ENV HTTP_PROXY http://myproxy:3128
ENV http_proxy http://myproxy:3128

I have tried doing this in my test Dockerfile and I get this result (note that this is the output without the dns settings):

PS C:\Projects\Test> docker build -t test:01 .

Sending build context to Docker daemon  3.906GB

Step 1/4 : FROM mcr.microsoft.com/windows/servercore:1903

---> 66c07b0d3e85

Step 2/4 : ENV HTTP_PROXY http://myproxy.com:3128

---> Using cache

---> d6bbca2c1984

Step 3/4 : ENV http_proxy http://myproxy.com:3128

---> Using cache

---> a22311c57c8d

Step 4/4 : RUN curl 'www.google.co.za'

---> Running in 3e3be3786b62

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100   874  100   874    0     0    874      0  0:00:01 --:--:--  0:00:01  1189

<HTML><HEAD>

<TITLE>Network Error</TITLE>

</HEAD>

<BODY>

<FONT face="Helvetica">

<big><strong></strong></big><BR>

</FONT>

<blockquote>

<TABLE border=0 cellPadding=1 width="80%">

<TR><TD>

<FONT face="Helvetica">

<big>Network Error (dns_unresolved_hostname)</big>

<BR>

<BR>

</FONT>

</TD></TR>

<TR><TD>

<FONT face="Helvetica">

Your requested host "'www.google.co.za'" could not be resolved by DNS.

</FONT>

</TD></TR>

<TR><TD>

<FONT face="Helvetica">

 

</FONT>

</TD></TR>

<TR><TD>

<FONT face="Helvetica" SIZE=2>

<BR>

For assistance, contact your network support team.<br><br>Your request was categorized by Blue Coat Web Filter as 'none'. <br>If you wish to question or dispute this result, please click <a href="http://sitereview.bluecoat.com/sitereview.jsp?referrer=136&url=http://'www.google.co.za'/">here</a>.

</FONT>

</TD></TR>

</TABLE>

</blockquote>

</FONT>

</BODY></HTML>

Removing intermediate container 3e3be3786b62

---> 79dcdbf32644

Successfully built 79dcdbf32644

Successfully tagged test:01

If I add the dns settings back into the docker GUI I get this output:

PS C:\Projects\Test> docker build -t test:01 .

Sending build context to Docker daemon  3.906GB

Step 1/4 : FROM mcr.microsoft.com/windows/servercore:1903

---> 66c07b0d3e85

Step 2/4 : ENV HTTP_PROXY http://myproxy.com:3128

---> Running in 4ac3e6e23b5f

Removing intermediate container 4ac3e6e23b5f

---> 6bfbb6923789

Step 3/4 : ENV http_proxy http://myproxy.com:3128

---> Running in 7e7b08cef7b3

Removing intermediate container 7e7b08cef7b3

---> 4a827371e6fa

Step 4/4 : RUN curl 'www.google.co.za'

---> Running in 317a2ea37342

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0curl: (5) Could not resolve proxy: myproxy.com

The command 'cmd /S /C curl 'www.google.co.za'' returned a non-zero code: 5

So with the dns settings it can’t resolve the proxy and without the dns settings it can’t resolve the URL. I don’t know how to proceed from here.

My answer:


You should remove the literal single quotes from your curl command. They are being sent to the proxy server, so that it tries to find a host named 'www.google.co.za' instead of the desirable www.google.co.za.

It should instead be:

RUN curl www.google.co.za

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.