dotnet restore 命令到 Dockerfile 中总是以异常结束

dotnet restore comand into Dockerfile always finish with Exception

在我的解决方案中,我使用 docker。 当我尝试构建我的图像时,每次我收到错误:

正在将构建上下文发送到 Docker 守护进程 77.32MB

Step 1/16 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
 ---> 014a41b1f39a
Step 2/16 : WORKDIR /app
 ---> Using cache
 ---> c3eb79db5e63
Step 3/16 : EXPOSE 80
 ---> Using cache
 ---> f23ddd06f8f8
Step 4/16 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
 ---> 006ded9ddf29
Step 5/16 : WORKDIR /src
 ---> Using cache
 ---> d8016eaadf87
Step 6/16 : COPY ["ProductCatalogApi.csproj", "src/Services/ProductCatalogApi/"]
 ---> 59d2cc3f4102
Step 7/16 : RUN dotnet restore "src/Services/ProductCatalogApi/ProductCatalogApi.csproj"
 ---> Running in 347a48e8ef6f
  Determining projects to restore...

     Retrying 'FindPackagesByIdAsync' for source 'https://api.nuget.org/v3-flatcontainer/system.resources.resourcemanager/index.json'.
      The SSL connection could not be established, see inner exception.
         Received an unexpected EOF or 0 bytes from the transport stream.
      Failed to download package 'System.IO.Compression.ZipFile.4.3.0' from 'https://api.nuget.org/v3-flatcontainer/system.io.compression.zipfile/4.3.0/system.io.compression.zipfile.4.3.0.nupkg'.
The command '/bin/sh -c dotnet restore "src/Services/ProductCatalogApi/ProductCatalogApi.csproj"' returned a non-zero code: 1

Docker文件有下一个代码:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["src/Services/ProductCatalogApi/ProductCatalogApi.csproj", "src/Services/ProductCatalogApi/"]
RUN dotnet restore "src/Services/ProductCatalogApi/ProductCatalogApi.csproj"
COPY . .
WORKDIR "/src/Services/ProductCatalogApi"
RUN dotnet build "ProductCatalogApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProductCatalogApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProductCatalogApi.dll"]

我的 .csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>ShoesOnContainers.Services.ProductCatalogApi</RootNamespace>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>..\..\..</DockerfileContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.10" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.10" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
  </ItemGroup>


</Project>

我一整天都在尝试通过其他方式构建我的形象,但每次我都收到错误。请帮助某人.......

我替换了这个

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

在此

FROM microsoft/aspnetcore-build:2.0.0 AS build

然后替换了这个

FROM mcr.microsoft.com/dotnet/core/sdk:3.1

在此

FROM microsoft/aspnetcore:2.0.0

在我的 .csproj 文件中将 netcoreapp 的版本从 3.1 替换为 2.0。 据我了解,问题出在 docker 个图像版本中。

我遇到了同样的错误,我试过了,成功了。

  • 停止所有 运行 个容器 docker stop $(docker ps -aq)
  • 删除所有被前面命令停止的容器docker rm $(docker ps -aq)
  • 使用docker volume rm $(docker volume ls -q)
  • 删除所有卷
  • 最好使用 docker rmi -f $(docker images -a -q)
  • 清理所有旧的 docker 图片
  • 或者您可以使用 Docker 桌面 UI.
  • 清除所有数据
  • 然后您可以转到 src 目录并使用 docker-compose build 再次构建所有图像,然后使用 docker-compose up

!IMPORTANT Please note, the above commands will clean up all the docker related instances on that environment. So it's not recommended to run these on any production environment or any other environment where docker instances of any other applications are running.

您可以使用下面的行来解决它

RUN dotnet restore --disable-parallel

同时勾选 here