我用的是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
标签:load,index,service,0.0,webapiEF,build,csproj,dotnet From: https://www.cnblogs.com/rex2023/p/18448737