dockerasp.net-core.net-coredockerfilevisual-studio-2019

dotnet restore fails from Docker container


I have researched this for the past couple days and none of the research I've found has helped me solve this issue, including restarting Docker, restarting Docker service, restarting Visual Studio, deleting Nuget, adjusting proxy settings, adjusting Nuget.config etc etc.

Ultimately the error message I get is Unable to load the service index for source https://api.nuget.org/v3/index.json. but please bear with me I'm giving exect steps to reproduce this error.

When I create an ASP.NET Core Web Application in Visual Studio 2019, then Add Docker support to the project (Right click project, choose Add -> Docker Support...), a Dockerfile is created which looks like this:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
RUN dotnet restore "WebApplication3/WebApplication3.csproj"
COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]

When I build the docker image (Right click dockerfile, choose Build Docker Image), it builds fine and I get:

Starting up container(s)...
docker build -f "C:\Users\TheUser\source\repos\WebApplication3\WebApplication3\Dockerfile" --force-rm -t webapplication3:dev --target base  --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=WebApplication3" "C:\Users\TheUser\source\repos\WebApplication3" 
Sending build context to Docker daemon  4.396MB

Step 1/6 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
 ---> dc28376d4369
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> d0cbefb504d1
Step 3/6 : EXPOSE 80
 ---> Using cache
 ---> 865f960359d6
Step 4/6 : EXPOSE 443
 ---> Using cache
 ---> 4d040d5c8a4c
Step 5/6 : LABEL com.microsoft.created-by=visual-studio
 ---> Using cache
 ---> 4223be37abec
Step 6/6 : LABEL com.microsoft.visual-studio.project-name=WebApplication3
 ---> Running in d1ced38ba0fa
Removing intermediate container d1ced38ba0fa
 ---> fb400230edf4
Successfully built fb400230edf4
Successfully tagged webapplication3:dev
docker run -dt -v "C:\Users\TheUser\onecoremsvsmon\16.4.0067.0:C:\remote_debugger:ro" -v "C:\Users\TheUser\source\repos\WebApplication3\WebApplication3:C:\app" -v "C:\Users\TheUser\source\repos\WebApplication3:C:\src" -v "C:\Users\TheUser\AppData\Roaming\Microsoft\UserSecrets:C:\Users\ContainerUser\AppData\Roaming\Microsoft\UserSecrets:ro" -v "C:\Users\TheUser\AppData\Roaming\ASP.NET\Https:C:\Users\ContainerUser\AppData\Roaming\ASP.NET\Https:ro" -v "C:\Users\TheUser\.nuget\packages\:c:\.nuget\fallbackpackages2" -v "C:\Program Files\dotnet\sdk\NuGetFallbackFolder:c:\.nuget\fallbackpackages" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=https://+:443;http://+:80" -e "NUGET_PACKAGES=c:\.nuget\fallbackpackages2" -e "NUGET_FALLBACK_PACKAGES=c:\.nuget\fallbackpackages;c:\.nuget\fallbackpackages2" -P --name WebApplication3 --entrypoint C:\remote_debugger\x64\msvsmon.exe webapplication3:dev /noauth /anyuser /silent /nostatus /noclrwarn /nosecuritywarn /nofirewallwarn /nowowwarn /fallbackloadremotemanagedpdbs /timeout:2147483646 /LogDebuggeeOutputToStdOut 
eba5dec02ed7a80158340d6f8d6af504b86edf7cd6944eb1d4ce71f7847fabb5
Container started successfully.
========== Finished ==========

However if I add a .Net Core Class Library to the solution, add that class library as a project reference to the Web Application and Add Docker Support again, I get a new Dockerfile that looks like this:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
COPY ["ClassLibrary1/ClassLibrary1.csproj", "ClassLibrary1/"]
RUN dotnet restore "WebApplication3/WebApplication3.csproj"
COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]

And when I Build Docker Image again this time I get an error which looks like this:

1>------ Rebuild All started: Project: ClassLibrary1, Configuration: Debug Any CPU ------
1>ClassLibrary1 -> C:\Users\TheUser\source\repos\WebApplication3\ClassLibrary1\bin\Debug\netcoreapp3.1\ClassLibrary1.dll
2>------ Rebuild All started: Project: WebApplication3, Configuration: Debug Any CPU ------
2>docker rm -f eba5dec02ed7a80158340d6f8d6af504b86edf7cd6944eb1d4ce71f7847fabb5
2>eba5dec02ed7a80158340d6f8d6af504b86edf7cd6944eb1d4ce71f7847fabb5
2>WebApplication3 -> C:\Users\TheUser\source\repos\WebApplication3\WebApplication3\bin\Debug\netcoreapp3.1\WebApplication3.dll
2>WebApplication3 -> C:\Users\TheUser\source\repos\WebApplication3\WebApplication3\bin\Debug\netcoreapp3.1\WebApplication3.Views.dll
2>Docker version 19.03.8, build afacb8b
2>docker build -f "c:\users\TheUser\source\repos\webapplication3\webapplication3\dockerfile" --force-rm -t webapplication3  --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=WebApplication3" "c:\users\TheUser\source\repos\webapplication3"
2>Sending build context to Docker daemon    4.4MB
2>
2>Step 1/20 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
2> ---> dc28376d4369
2>Step 2/20 : WORKDIR /app
2> ---> Using cache
2> ---> d0cbefb504d1
2>Step 3/20 : EXPOSE 80
2> ---> Using cache
2>Step 4/20 : EXPOSE 443
2> ---> 865f960359d6
2> ---> Using cache
2> ---> 4d040d5c8a4c
2>Step 5/20 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
2> ---> c67fa4d2a089
2>Step 6/20 : WORKDIR /src
2> ---> Using cache
2> ---> 14763e98238e
2>Step 7/20 : COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
2> ---> ad0ded95d169
2>Step 8/20 : COPY ["ClassLibrary1/ClassLibrary1.csproj", "ClassLibrary1/"]
2> ---> 22667eda405c
2>Step 9/20 : RUN dotnet restore "WebApplication3/WebApplication3.csproj"
2> ---> Running in a3e6a184b4e9
2>  Restore completed in 495.4 ms for C:\src\ClassLibrary1\ClassLibrary1.csproj.
2>C:\Program Files\dotnet\sdk\3.1.201\NuGet.targets(124,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [C:\src\WebApplication3\WebApplication3.csproj]
2>C:\Program Files\dotnet\sdk\3.1.201\NuGet.targets(124,5): error :   No such host is known. [C:\src\WebApplication3\WebApplication3.csproj]
2>Removing intermediate container a3e6a184b4e9
2>The command 'cmd /S /C dotnet restore "WebApplication3/WebApplication3.csproj"' returned a non-zero code: 1
2>c:\users\TheUser\source\repos\webapplication3\webapplication3\dockerfile : error CTC1014: Docker command failed with exit code 1.
2>c:\users\TheUser\source\repos\webapplication3\webapplication3\dockerfile : error CTC1014: The command 'cmd /S /C dotnet restore "WebApplication3/WebApplication3.csproj"' returned a non-zero code: 1
2>Done building project "WebApplication3.csproj" -- FAILED.
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

enter image description here Should be easy to reproduce. Why does it work without the class library reference then error when adding a reference? Do I need to update the Dockerfile with something extra? I have been going around in circles with this and none of the other posts on the subject have phrased it this way or given me any answers that work for me.


Solution

  • The actual error seems to be:

    Unable to load the service index for source https://api.nuget.org/v3/index.json
    

    Which means that nuget is failing to access the endpoint when it is trying to download the dependencies.

    There are a number of solutions to this listed here

    https://github.com/NuGet/Home/issues/2880

    and also

    Nuget connection attempt failed "Unable to load the service index for source"