11

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.

TrevorBrooks
  • 3,590
  • 3
  • 31
  • 53
  • 1
    I was not only behind a proxy, but had a custom root certificate. I had to copy the root cert into the container and have `RUN update-ca-certificates` – jmzagorski Sep 01 '21 at 19:03
  • 3
    i was searching for the same question , but my problem was parallel computing of nugets in dotnet which leads to many request to nuget packages and blocking some of them as result, i solved it with `dotnet restore --disable-parallel` – Mahdi Khalili Oct 07 '21 at 19:48

5 Answers5

3

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"

Rosco
  • 2,108
  • 17
  • 17
  • But it restores just fine before I add the class library reference. – TrevorBrooks Mar 31 '20 at 23:10
  • 1
    Have you tried any of the solutions on the github link? – Rosco Mar 31 '20 at 23:27
  • This is the real problem - Ensure the Proxy env. variable settings and any user credentials specified for the NuGet server are correct. Most likely NuGet was succeeding before because it didn't call the API if it has all the dependencies needed locally. – ryanwebjackson Jun 30 '22 at 15:58
3

Are you behind a proxy of some sort? Corporate or otherwise.

I was able to get around this issue by setting the http_proxy in my Dockerfile.

ARG HTTP_PROXY="http://username:password@proxy.example.com:8080"

Full Dockerfile example below:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env

WORKDIR /app

# Copy csproj and restore as distinct layers

COPY *.csproj ./

ARG HTTP_PROXY="http://username:password@proxy.example.com:8080"

USER administrator
RUN dotnet restore

# Copy everything else and build

COPY . ./

USER administrator
RUN dotnet publish -c Release -o out

# Build runtime image

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

WORKDIR /app

COPY --from=build-env /app/out .

ENTRYPOINT ["dotnet", "myapp.dll"]
Morgeh
  • 679
  • 3
  • 11
  • 27
2

Not able to reproduce it. This is my docker file.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

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

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

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

Different line:

COPY ["../ClassLibrary1/ClassLibrary1.csproj", "../ClassLibrary1/"]
RUN dotnet restore "./WebApplication1.csproj"

I think your csproj and sln file are in different folder. When using visual studio make sure you select the option: Place solution and project file in the same folder

enter image description here

2nd Update to the answer:

With every project that you add/reference. Please add support for Docker.

This'll update the docker file.

enter image description here

Community
  • 1
  • 1
Ziaullah Khan
  • 2,020
  • 17
  • 20
  • Following your instructions and I get a new error message: 2>COPY failed: failed to resolve scoped path ..\ClassLibrary2\ClassLibrary2.csproj (): evalSymlinksInScope: \\?\C:\ProgramData\Docker\tmp\ClassLibrary2\ClassLibrary2.csproj is not in \\?\C:\ProgramData\Docker\tmp\docker-builder380976981. Possible cause is a forbidden path outside the build context 2>c:\users\theUser\source\repos\webapplication4\dockerfile : error CTC1014: Docker command failed with exit code 1. – TrevorBrooks Apr 01 '20 at 13:55
  • Repeat Add > Docker Support. This'll keep the `Dockerfile` updated. – Ziaullah Khan Apr 01 '20 at 17:06
0

If you can't access that nuget endpoint then I'd be curious about 2 things:

  1. Maybe you don't mean to be at all
  2. All of your dependencies live in a private repository (a different endpoint)

If both or either of 1 and 2 above are true and you have a NuGet.Config file managing your dependencies then try and remove any hard-coded Package References from your *.csproj file, then try your docker build again.

If you don't have a NuGet.Config and are referencing your project packages directly from your *.csproj file still I would recommend creating one and removing those references from the project file itself. If not for Docker purposes, then at least for the sake of good organization/separation of concerns.

One last thing, make sure that NuGet.Config file is at the same directory level as your Dockerfile. Dockerfiles will not "see anything" not at their same directory level (even if you have proper relative paths in your Dockerfile so it could theoretically see them if it worked that way).

Ryan
  • 1
  • 2
0

I ran into a similar problem with dotnet restore causing the Unable to load the service index for source https://api.nuget.org/v3/index.json.

For me the solution was to set dns in the Docker Engine settings, as described in one of the answers in here: Docker container cannot resolve hosts

FruBlom
  • 23
  • 1
  • 8