我正在尝试创建 .NET 6 项目的 docker 映像,但在使用 RAM 的
dotnet restore
时,在 +12GB
期间卡住了。
我的项目结构是:
- backend/
- frontend/
我只是在
cd
中backend/
并运行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"]
解决了,问题出在我的
.csproj
<ItemGroup>
<Watch Include="..\**\*.env" />
</ItemGroup>
我把它改为:
<ItemGroup>
<Watch Include="..\**\*.env" Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'" />
</ItemGroup>
在另一种情况下,我认为我的应用程序卡在恢复上,但它卡在构建上。您还可以将以下内容添加到 dotnet Restore 命令以及 dotnet build 命令的标志中,以便在构建运行时获取更多信息 -v diag
# RUN dotnet restore "Myproject.Ticketing/Myproject.Ticketing.csproj"
# So instead of just the line at the top, add the -v diag flag like below
RUN dotnet restore -v diag "Myproject.Ticketing/Myproject.Ticketing.csproj"
RUN dotnet build "Myproject.Ticketing.csproj" -c Release -o /app/build -v diag
记住,这是在 Dockerfile 中。我所做的是首先将 -v diag 放在 dotnetrestore 上,当时我在查看输出后意识到它实际上已经通过了该阶段。接下来,我将其从恢复中删除,并在构建行上放置相同的标志,这是因为输出了很多信息,所以我只想限制输出。
嗯,结论是,对我来说,输出中写的最后一句话是
警告错误+:NU1605(任务ID:105)
谷歌搜索后给了我
依赖包指定了比恢复最终解决的包版本更高的版本约束。
https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1605
因此,尽管我的应用程序在开发过程中构建并运行良好,但我所依赖的库依赖于 .Net Core 3.1,但我仍在使用 .Net 6,并且正在构建的 docker 容器也是构建在 .Net Core 之上的。网6
别问我它是如何工作的,它就是这样。
我通过阅读这篇文章获得了 -v 标志,这也有助于解决整个挂起问题https://tsuyoshiushio.medium.com/solving-flaky-dotnet-restore-issue-only-on-docker-failed-to-检索信息-cd847573c3f2
有关 dotnet 命令的含义和选项的列表,
eg -v diag 是 --verbosity Diagnostic 的缩写(因此有很多信息,因为您想诊断问题),您可以查看 https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet -恢复#选项
在我的例子中,
.csproj
文件不包含任何<Watch/>
标签,但dotnet restore
步骤仍然卡住了。我在这个GitHub评论中找到了解决方案。在 Dockerfile 中,而不是使用 6.0 版本:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
我用的是8.0版本:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build