从Docker容器进行dotnet恢复失败

svujldwt  于 2023-01-16  发布在  Docker
关注(0)|答案(5)|浏览(288)

我已经研究了过去几天,我发现没有一个研究帮助我解决这个问题,包括重新启动Docker,重新启动Docker服务,重新启动Visual Studio,删除Nuget,调整代理设置,调整Nuget.config等。
最终,我得到的错误信息是Unable to load the service index for source https://api.nuget.org/v3/index.json.,但请原谅我,我给出了重现此错误的执行步骤。
当我在Visual Studio 2019中创建一个ASP.NET Core Web应用程序,然后将Docker支持添加到项目(右键单击项目,选择Add -〉Docker Support...)时,会创建一个Docker文件,如下所示:

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"]

当我构建Docker映像时(右键单击Dockerfile,选择Build Docker Image),它构建得很好,我得到:

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 ==========

但是,如果我向解决方案添加一个.Net核心类库,将该类库作为Web应用程序的项目引用添加,然后再次添加Docker支持,我将得到一个新的Docker文件,如下所示:

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"]

当我再次构建Docker映像时,这一次我得到了一个错误,看起来像这样:

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 ==========

x1c 0d1x应该很容易重现。为什么它没有类库引用就能工作,然后在添加引用时出错?我需要用额外的东西更新Dockerfile吗?我一直在绕圈子,关于这个主题的其他帖子都没有这样措辞,也没有给我任何适合我的答案。

wvt8vs2t

wvt8vs2t1#

实际错误似乎是:

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

这意味着nuget在尝试下载依赖项时无法访问端点。
这里列出了许多解决方案
https://github.com/NuGet/Home/issues/2880
并且还
Nuget connection attempt failed "Unable to load the service index for source"

xuo3flqw

xuo3flqw2#

无法复制。这是我的文件。

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"]

其他行:

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

我认为您的csprojsln文件在不同的文件夹中。当使用Visual Studio时,请确保您选择了该选项:* 将解决方案和项目文件放在同一文件夹中 *

第二次更新答案:

对于您添加/引用的每个项目。请添加对Docker的支持。
这将更新Docker文件。

mwyxok5s

mwyxok5s3#

你是不是有什么代理人?公司的或者其他的。
我能够通过在我的Dockerfile中设置http_proxy来绕过这个问题。

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

完整的Dockerfile示例如下:

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"]
fzwojiic

fzwojiic4#

如果您无法访问Nuget端点,那么我会对两件事感到好奇:
1.也许你根本就不想
1.您的所有依赖项都位于私有存储库(不同的端点)中
如果上述1和2中的任一个或两者都是真的,并且您有一个NuGet.Config文件管理您的依赖项,请尝试从您的 *.csproj文件中删除任何硬编码的包引用,然后再次尝试您的docker build
如果你没有NuGet.Config并且直接从你的 *.csproj文件引用你的项目包,我仍然建议你创建一个并从项目文件本身删除那些引用。如果不是为了Docker的目的,那么至少是为了良好的组织/关注点的分离。
最后一件事,确保NuGet.Config文件与您的Dockerfile位于同一目录级别。Dockerfile不会“看到”不在同一目录级别的任何内容(即使您在Dockerfile中有正确的相对路径,因此理论上它可以看到它们,如果它这样工作的话)。

mf98qq94

mf98qq945#

我遇到了一个类似的问题与dotnet恢复导致的Unable to load the service index for source https://api.nuget.org/v3/index.json
对我来说,解决方案是在Docker引擎设置中设置DNS,如这里的答案之一所述:Docker container cannot resolve hosts

相关问题