我用的是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