Building multi-platform containers for dotnet applications error NETSDK1047

huangapple go评论71阅读模式
英文:

Building multi-platform containers for dotnet applications error NETSDK1047

问题

I am currently trying to build a multi-platform dotnet docker image for .NET 7 using the improved multi-arch build pattern described in this devblog post. However, when I run this build command:

docker buildx build --tag exampleapiproject --platform linux/amd64,linux/arm64/v7,linux/arm64/v8 -f ExampleApiProject\Dockerfile ExampleApiProject

... I get the following error message from the builder:

------
> [linux/amd64 build 6/7] RUN dotnet publish --arch amd64 --no-restore -o /app:
#0 2.350 MSBuild version 17.6.1+8ffc3fe3d for .NET
#0 3.610 /usr/share/dotnet/sdk/7.0.302/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(266,5): error NETSDK1047: Assets
file 'project.assets.json' doesn't have a target for 'net7.0/linux-x64'. Ensure that restore has run and that you have included 'net7.0' in
the TargetFrameworks for your project. You may also need to include 'linux-x64' in your project's RuntimeIdentifiers. [ExampleApiProject.csproj]
------

I also checked the project.assets.json file and it does include the net7.0/linux-musl-x64 as well as the net7.0 target.

Build platform:

I am working on a Windows x64 host system with Docker version 23.0.5, build bc4487a.

Project File:

The example project was just created from the webapi template with minimal api and without https support.

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

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
  </ItemGroup>

</Project>

Dockerfile:

Also, I am using the alpine-non-root Dockerfile from the aspnetapp samples using the aspnet:7.0-alpine image instead of aspnet:8.0-preview-alpine.

# See https://github.com/dotnet/dotnet-docker/blob/main/samples/aspnetapp/Dockerfile.alpine-non-root

# Prepare build environment
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
ARG TARGETARCH
WORKDIR /src

# Copy project and restore for target platform
COPY *.csproj .
RUN dotnet restore --arch $TARGETARCH

# Copy everything else and build app
COPY . .
RUN dotnet publish --arch $TARGETARCH --no-restore -o /app

# Prepare runtime environment
# Enable globalization and time zones:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/enable-globalization.md
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine as final

WORKDIR /app
COPY --from=build /app .

USER $APP_UID
ENTRYPOINT ["./aspnetapp"]

Solution attempts:

Non-alpine Images:

I also attempted the same build using the normal versions of the images (the 7.0 instead of the 7.0-alpine) and also attempted the build using the 8.0-preview-alpine tag with the same errors.

Build only single platform:

I also attempted to run buildkit only on a single platform at a time (like --platform linux/amd64) but I get the same error message.

I tried this for all 3 different platforms, but with the same result (except of course the target being net7.0/linux-arm64, net7.0/linux-x64, or with alpine the *-musl-* targets).

英文:

I am currently trying to build a multi-platform dotnet docker image for .NET 7 using the improved multi-arch build pattern described in this devblog post. However when I run this build command:

docker buildx build --tag exampleapiproject --platform linux/amd64,linux/arm64/v7,linux/arm64/v8 -f ExampleApiProject\Dockerfile ExampleApiProject

... I get the following error message from the builder:

=&gt; ERROR [linux/amd64 build 6/7] RUN dotnet publish --arch amd64 --no-restore -o /app
------
 &gt; [linux/amd64 build 6/7] RUN dotnet publish --arch amd64 --no-restore -o /app:
#0 2.350 MSBuild version 17.6.1+8ffc3fe3d for .NET
#0 3.610 /usr/share/dotnet/sdk/7.0.302/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(266,5): error NETSDK1047: Assets
file &#39;/src/obj/project.assets.json&#39; doesn&#39;t have a target for &#39;net7.0/linux-x64&#39;. Ensure that restore has run and that you have included &#39;net7.0&#39; in
the TargetFrameworks for your project. You may also need to include &#39;linux-x64&#39; in your project&#39;s RuntimeIdentifiers. [/src/ExampleApiProject.csproj]
------

I also checked the project.assets.json file and it does include the net7.0/linux-musl-x64 as well as the net7.0 target.


Additional Information:

Build platform

I am working on a Windows x64 host system with Docker version 23.0.5, build bc4487a.

Project File

The example project was just created from the webapi template with minimal api and without https support.

&lt;Project Sdk=&quot;Microsoft.NET.Sdk.Web&quot;&gt;

  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
    &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Microsoft.AspNetCore.OpenApi&quot; Version=&quot;7.0.5&quot; /&gt;
    &lt;PackageReference Include=&quot;Swashbuckle.AspNetCore&quot; Version=&quot;6.4.0&quot; /&gt;
  &lt;/ItemGroup&gt;

&lt;/Project&gt;

Dockerfile

Also I am using the alpine-non-root Dockerfile from the aspnetapp samples using the aspnet:7.0-alpine image instead of aspnet:8.0-preview-alpine:


# Prepare build environment
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
ARG TARGETARCH
WORKDIR /src

# Copy project and restore for target platform
COPY *.csproj .
RUN dotnet restore --arch $TARGETARCH

# Copy everything else and build app
COPY . .
RUN dotnet publish --arch $TARGETARCH --no-restore -o /app

# Prepare runtime environment
# Enable globalization and time zones:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/enable-globalization.md
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine as final

WORKDIR /app
COPY --from=build /app .

USER $APP_UID
ENTRYPOINT [&quot;./aspnetapp&quot;]

Solution attempts:

Non-alpine Images

I also attempted the same build using the normal versions of the images (the 7.0 instead of the 7.0-alpine) and also attempted the build using the 8.0-preview-alpine tag with the same errors.

Build only single platform

I also attempted to run buildkit only on a singe platform at a time (like --platform linux/amd64) but i get the same error message.

I tried this for all 3 different platforms, but with the same result (except of course the target being net7.0/linux-arm64, net7.0/linux-x64 or with alpine the *-musl-* targets).

答案1

得分: 1

I'm stupid and this cost me so much time...

TL;DR: .dockerignore文件丢失


默认情况下,.dockerignore文件在项目根目录中生成,但我从项目目录构建了我的项目。对我来说,主要问题是dockerignore文件的行为与gitignore有点不同,即dockerignore文件必须构建上下文根目录,而不是项目根目录,因为docker(显然)不会检查父目录中的该文件。

因此,在项目目录和构建上下文中缺少.dockerignore文件。这导致我的本地bin和obj文件夹暴露给了构建上下文(其中还包含一个Windows x64 dotnet restore)。直观地,我现在认为,在dockerfile中的restore命令应该覆盖掉那个,但似乎并没有。

解决方案

移动dockerignore

将dockerignore文件移动到项目目录中。

更改构建上下文

从项目根目录构建docker镜像,并像Visual Studio默认情况下那样引用以项目位置为前缀的文件。

英文:

I'm stupid and this cost me so much time...

TL;DR: The .dockerignore file was missing


By default the .dockerignore file is generated in the project root, but I built my project from the project directory. Main problem for me was, that the dockerignore file behaves a little bit different than the gitignore in that the dockerignore file must be at the build context root, and not the project root as docker does (obviously) not check parent directories for that file.

The .dockerignore was thus missing in the project dir and the build context. This caused my local bin and obj folders to be exposed to the build context (which also contained a windows x64 dotnet restore). Intuitively I now think, that the restore command in the dockerfile should have overwritten that, but it seems not.

Solutions

Move dockerignore

Move the docker ignore file into the project directory.

Change build context

Build the docker image from the project root and refer to files prefixed with the project location as Visual studio does by default.

huangapple
  • 本文由 发表于 2023年5月26日 00:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定