如何解决 构建dotnet docker镜像时报错:error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.

我用的是docker desktop,Builders设置:desktop-linux

以下是我的dotnet项目的Dockerfile内容

复制代码
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["NuGet.Config", "."]
COPY ["webapiEF.csproj", "."]

RUN dotnet restore "./webapiEF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "webapiEF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "webapiEF.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "webapiEF.dll"]
复制代码

使用这样的命令来执行构建镜像

docker build -t gxy/aspnetefapi:prod .

报出的错误:

复制代码
=> ERROR [build 5/8] RUN dotnet restore "./webapiEF.csproj"                                                                                                                        476.2s
------
 > [build 5/8] RUN dotnet restore "./webapiEF.csproj":
2.136   Determining projects to restore...
128.8 /src/webapiEF.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
243.0 /src/webapiEF.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
351.6 /src/webapiEF.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
475.9 /src/webapiEF.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
------
Dockerfile:15
--------------------
  14 |     COPY ["webapiEF.csproj", "."]
  15 | >>> RUN dotnet restore "./webapiEF.csproj"
  16 |     COPY . . 
ERROR: failed to solve: process "/bin/sh -c dotnet restore \"./webapiEF.csproj\"" did not complete successfully: exit code: 1
复制代码

经过检查,宿主机和docker容器中对nuget文件"https://api.nuget.org/v3/index.json"的访问都是正常的

说明网络不存在问题,再经过网上搜索类似的问题,找到一个答案
https://github.com/dotnet/core/issues/8337#issuecomment-1488941949
DNS解析存在问题,我在Docker Engine中增加设置“dns”:["8.8.8.8"]后,镜像构建成功了!

成功构建的输出

复制代码
[+] Building 0.4s (19/19) FINISHED                                                                                                                                    docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                  0.0s
 => => transferring dockerfile: 1.01kB                                                                                                                                                0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0                                                                                                                  0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                                                     0.0s
 => => transferring context: 382B                                                                                                                                                     0.0s
 => [build 1/8] FROM mcr.microsoft.com/dotnet/sdk:6.0                                                                                                                                 0.0s
 => [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:6.0                                                                                                                               0.0s
 => [internal] load build context                                                                                                                                                     0.0s
 => => transferring context: 916B                                                                                                                                                     0.0s
 => CACHED [base 2/2] WORKDIR /app                                                                                                                                                    0.0s
 => CACHED [final 1/2] WORKDIR /app                                                                                                                                                   0.0s
 => CACHED [build 2/8] WORKDIR /src                                                                                                                                                   0.0s
 => CACHED [build 3/8] COPY [NuGet.Config, .]                                                                                                                                         0.0s
 => CACHED [build 4/8] COPY [webapiEF.csproj, .]                                                                                                                                      0.0s
 => CACHED [build 5/8] RUN dotnet restore "./webapiEF.csproj"                                                                                                                         0.0s
 => CACHED [build 6/8] COPY . .                                                                                                                                                       0.0s
 => CACHED [build 7/8] WORKDIR /src/.                                                                                                                                                 0.0s
 => CACHED [build 8/8] RUN dotnet build "webapiEF.csproj" -c Release -o /app/build                                                                                                    0.0s
 => CACHED [publish 1/1] RUN dotnet publish "webapiEF.csproj" -c Release -o /app/publish /p:UseAppHost=false                                                                          0.0s
 => CACHED [final 2/2] COPY --from=publish /app/publish .                                                                                                                             0.0s
 => exporting to image                                                                                                                                                                0.0s
 => => exporting layers                                                                                                                                                               0.0s
 => => writing image sha256:84fc67cc69c5daf547996cf09e76cb393976aa9aad6299c50269866b88c7b28b                                                                                          0.0s
 => => naming to docker.io/gxy/aspnetefapi:prod
复制代码

 

posted @   蜀南蓝玉  阅读(243)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示