在Docker构建中,Dotnet Restore失败

17

你好,我正在尝试将一个 ASP NET Core 2.1 应用程序进行 Docker化 ,但在执行 dotnet restore 时,docker build 失败了。

我已经查看了其他关于这个问题的帖子,链接为:
Nuget连接尝试失败"无法加载源的服务索引"

那里提供的解决方案对我没有帮助。

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

输出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

2
我遇到了相同的问题,但是是在.NET Core 2.2 Windows镜像上。我也尝试了其他线程,但没有成功。奇怪的是,我可以从Visual Studio 2017中运行Docker。但是当我在命令行中运行时(即使以管理员身份运行),我只有Nuget连接问题。 - Dane Vinson
我遇到了类似的问题。根据这个线程 https://github.com/NuGet/Home/issues/6742 上的讨论,我似乎在使用 --disable-parallel 选项添加到 dotnet restore 中更成功。 - Emil
4个回答

4

试试这个

docker network ls

应列出网络ID,请尝试运行docker build。

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

尝试使用列出的所有网络,它将与其中一个一起工作,即主机网络。


这是我在面对这个烦人的“nuget restore”网络错误时找到的唯一解决方案。谢谢! - StratMN
1
我正在从WSL2(Ubuntu 20.04.3 LTS)运行docker build,并尝试使用docker network ls中显示的所有网络,但仍然无法与任何网络正常工作。一些会立即超时,而另一个则需要一段时间。 - BearsEars
结果发现我的问题是由于我在WSL2中的DNS设置不太正确。在下面发布另一个答案... - BearsEars

2

尝试更新您的DNS服务器以进行修复(默认应为8.8.8.8)。

我在“设置” -> “网络” -> “DNS服务器”中找到了这个信息。


1

我在尝试在运行在 Windows 机器上的 WSL2(Ubuntu 20.04)中构建 Docker 镜像时遇到了相同的错误。

在我的情况下,我确认我可以从 WSL2 访问 nuget:

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

但是,如果我在Docker容器中尝试相同的请求,它无法解析主机名(这只会挂起):

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

这表明DNS存在问题,由于Docker使用您的“主机”DNS配置,因此我关注了我的WSL2 DNS。我使用的是WSL2使用的默认DNS。这意味着/etc/resolv.conf是自动生成的...我按照这里的说明将其关闭:https://superuser.com/questions/1533291/how-do-i-change-the-dns-settings-for-wsl2 然后,在/etc/resolv.conf中手动配置的DNS是我们公司的内部DNS服务器。重新启动WSL2和Docker后,我能够在Docker容器中解析api.nuget.org。

0

https://improveandrepeat.com/2019/10/how-to-activate-tls-1-2-on-windows-server-2008-r2-and-iis-7-5/ - GregC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接