docker 构建卡在 dotnet 恢复上

docker build stuck on dotnet restore

我正在尝试创建 .NET 6 项目的 docker 图像,但在使用 +12GB RAM 时卡在 dotnet restore 期间。

我的项目结构是:

- backend/
- frontend/

我只是 cdbackend/ 和 运行 docker build .

这是当前控制台的输出:

[+] Building 276.4s (15/19)
 => [internal] load .dockerignore                                                                                                           0.0s 
 => [internal] load build definition from Dockerfile.server                                                                                 0.0s 
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0                                                                           0.6s 
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0                                                                        0.6s 
 => [stage-1 1/3] FROM mcr.microsoft.com/dotnet/aspnet:6.0@sha256:9ca180a6a0a0ec39209437e5e0986caf17b7d91473d9c34bb6191e47a7b500aa          0.0s 
 => [build-env 1/6] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:ca4344774139fabfb58eed70381710c8912900d92cf879019d2eb52abc307102           0.0s 
 => [internal] load build context                                                                                                           0.2s 
 => => transferring context: 3.69kB                                                                                                         0.2s 
 => CACHED [stage-1 2/3] WORKDIR /app                                                                                                       0.0s 
 => CACHED [build-env 2/6] WORKDIR /app                                                                                                     0.0s 
 => CACHED [build-env 3/6] COPY *.csproj ./                                                                                                 0.0s 
 => [build-env 4/6] RUN dotnet restore                                                                                                    270.2s

我的csproj:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>1701;1702;1705;1591;10102;</NoWarn>
    <DefaultItemExcludes>**\node_modules\**;$(DefaultItemExcludes)</DefaultItemExcludes>
  </PropertyGroup>

  <ItemGroup>
    <Watch Include="..\**\*.env" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
    <PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
    <PackageReference Include="dotenv.net" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="5.0.12" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

</Project>

这是我的 Dockerfile:

# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

ARG Config=Debug
ENV ASPNETCORE_URLS=http://*:5000

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything
COPY . .

# Publish
RUN dotnet publish -c ${Config} -o /app/publish

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/publish .
ENTRYPOINT ["dotnet", "myapp.dll"]

我的净用量:

这是一个及格结果(1 小时)建筑:

已解决,问题出在我的.csproj

 <ItemGroup>
    <Watch Include="..\**\*.env" />
  </ItemGroup>

我改成了:

  <ItemGroup>
    <Watch Include="..\**\*.env" Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'" />
  </ItemGroup>