17

Hello i am trying to dockerize a ASP NET Core 2.1 application and the docker build fails when it executes dotnet restore. I have already checked other threads for this specific problem
Nuget connection attempt failed "Unable to load the service index for source".

The solutions provided there did not help me.

Dockerfile

ARG serverPath=./Core/Server

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app


COPY ./Core/Server/*.csproj ./
RUN dotnet restore  //fails here

COPY ./Core/Server/ ./
RUN dotnet publish -c Release -o out  


FROM microsoft/dotnet:2.1-aspnetcore-runtime 
WORKDIR /app
COPY --from=build-env /app/out  .
ENTRYPOINT ["dotnet","Server.dll"]

Output of docker build

$ docker build -t server .
Sending build context to Docker daemon  11.13MB
Step 1/11 : ARG serverPath=./Core/Server
Step 2/11 : FROM microsoft/dotnet:sdk AS build-env
 ---> 343e2dc38168
Step 3/11 : WORKDIR /app
 ---> Using cache
 ---> e9b75480ecb9
Step 4/11 : COPY ./Core/Server/*.csproj ./
 ---> Using cache
 ---> 2de864bedf6a
Step 5/11 : RUN dotnet restore 
 ---> Running in 2fc6963e7e2c
  Restoring packages for /app/Server.csproj...
/usr/share/dotnet/sdk/2.2.100/NuGet.targets(114,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/app/Server.csproj]
/usr/share/dotnet/sdk/2.2.100/NuGet.targets(114,5): error :   The SSL connection could not be established, see inner exception. [/app/Server.csproj]
/usr/share/dotnet/sdk/2.2.100/NuGet.targets(114,5): error :   Authentication failed because the remote party has closed the transport stream. [/app/Server.csproj]
The command '/bin/sh -c dotnet restore -p:RestoreUseSkipNonexistentTargets=false -nowarn:msb3202,nu1503' returned a non-zero code: 1
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 2
    I'm having the same issue but with .NET Core 2.2 Windows images. I too went through that other thread without success. One strange thing is that I can run Docker out of Visual Studio 2017. I only have the Nuget connection issues from the command line (even when run as administrator). – Dane Vinson Dec 22 '18 at 04:19
  • I am having similar issues. As per discussions on this thread https://github.com/NuGet/Home/issues/6742 I seem to be having more success adding the `--disable-parallel` option to dotnet restore. – Emil Nov 07 '19 at 09:40

4 Answers4

4

Try this

docker network ls

This should list the networkIDs try running the docker build

docker build --network=<networkid> tag=<tag>

try the above with all the network listed it will work with one, that's the host network.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
stravish
  • 59
  • 2
  • this was the only solution that worked for me when facing this annoying 'nuget restore' network error. thanks! – StratMN Nov 04 '20 at 19:36
  • 1
    I'm running docker build from WSL2 (Ubuntu 20.04.3 LTS) and after trying this with all of the networks that show up in `docker network ls`, it still doesn't work with any of them. Some timeout instantly and one takes a while. – BearsEars Jan 12 '22 at 15:25
  • Turns out my issue was due to my DNS being not quite right in my WSL2. Posting another answer below... – BearsEars Jan 12 '22 at 15:53
2

Try updating your DNS Server to be Fixed (default should be 8.8.8.8).

I found this in Settings-> Network -> DNS Server

Ed Harrod
  • 3,423
  • 4
  • 32
  • 53
1

I was seeing this same error while trying to build a docker image within WSL2 (Ubuntu 20.04) running on my Windows machine.

In my case, I confirmed that I could hit nuget from my WSL2 with:

curl -v https://api.nuget.org/v3/index.json

But then if I tried that same request from a Docker container, it could not resolve the hostname (this would just hang):

docker run -it --rm curlimages/curl -v -k https://api.nuget.org/v3/index.json

So this pointed to an issue with DNS and since Docker uses your "host" DNS config, I focused on my WSL2 DNS. I was using the default DNS that WSL2 uses. This means /etc/resolv.conf is auto-generated... I turned that off following the instructions here: https://superuser.com/questions/1533291/how-do-i-change-the-dns-settings-for-wsl2

The DNS I then manually configured in /etc/resolv.conf was my company's internal DNS server. After restarting WSL2 and Docker, I was able to resolve api.nuget.org in Docker containers.

BearsEars
  • 849
  • 1
  • 13
  • 21
0

If on Windows 7 or Windows Server 2008 R2, enable TLS 1.2 to talk to the new NuGet server. Microsoft DevBlogs on NuGet TLS status

GregC
  • 7,737
  • 2
  • 53
  • 67
  • https://improveandrepeat.com/2019/10/how-to-activate-tls-1-2-on-windows-server-2008-r2-and-iis-7-5/ – GregC Jan 14 '21 at 05:27