Building docker containers from Official .NET SDK images for ARM64 does not work

Jean-Jacques Kujawa 30 Reputation points
2023-02-06T21:31:23.2633333+00:00

Good evening,

Context

I am currently trying to have a Blazor application run from a Raspberry Pi.

I have to create pipelines so that when the devs push their code to a GitHub repo, it is turned into a Docker image that gets pushed to Docker Hub.

For now I do tryouts from the basic Blazor app created with dotnet new blazorserver -o rasp-test command.

Everything works fine should I simply want to compile for linux/amd64 architecture (i.e. not adding target platform to either the dotnet CLI or Docker build).

As soon as I add the --platform option to docker build, the compilation fails.

Ideally I'd love to get a single multi-platform Docker image.

Here are the Dockerfiles I have tried

Completing AMD64 docker image

This works. No --platform option is added. Build with docker buildx builder -f .\Dockerfile -t uchitesting/rasp-test:latest --push . The image successfully pushed to Docker Hub as linux/amd64 image

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["rasp-test.csproj", "."]
RUN dotnet restore "rasp-test.csproj"
COPY . .
RUN dotnet build "rasp-test.csproj" -c Release -o /app/build

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

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

Failing ARM64 image

This fails. The --platform option is added. Build with docker buildx builder -f .\Dockerfile-arm64 -t uchitesting/rasp-test:latest-arm64 .

FROM mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim-arm64v8 AS build
WORKDIR /src
COPY ["rasp-test.csproj", "."]
RUN dotnet restore "rasp-test.csproj" -r linux-arm64
COPY . .
RUN dotnet build "rasp-test.csproj" -c Release -r linux-arm64 --no-self-contained -o /app/build

FROM build AS publish
RUN dotnet publish "rasp-test.csproj" -r linux-arm64 -c Release --no-self-contained -o /app/publish
RUN chmod +x compile-arm64.sh  && ./compile-arm64.sh

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

The error message goes like this on my local machine. I initially tried to use the same versions of the images as the previous Dockerfile

> docker buildx build -f .\Dockerfile-arm64-2 -t uchitesting/rasp-test:latest-arm64 --platform linux/arm64 --load --no-cache .
[+] Building 14.2s (12/17)
 => [internal] load build definition from Dockerfile-arm64-2                                                                                                                                                0.1s
 => => transferring dockerfile: 760B                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.1s
 => => transferring context: 2B                                                                                                                                                                             0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim-arm64v8                                                                                                                     0.2s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8                                                                                                                  0.2s
 => [build 1/6] FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim-arm64v8@sha256:6763c9d715087221fa420b5cd0f54cc9a7476dc3aca8b3626c4f98eb3dddf8d6                                                         0.1s
 => => resolve mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim-arm64v8@sha256:6763c9d715087221fa420b5cd0f54cc9a7476dc3aca8b3626c4f98eb3dddf8d6                                                               0.1s
 => [internal] load build context                                                                                                                                                                           0.3s
 => => transferring context: 26.81kB                                                                                                                                                                        0.2s
 => [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8@sha256:ec84d3017ae8ea48088e0df39a6ac5d4c610f02819d04c8d7db11c78713d28f0                                                       0.1s
 => => resolve mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8@sha256:ec84d3017ae8ea48088e0df39a6ac5d4c610f02819d04c8d7db11c78713d28f0                                                            0.1s
 => CACHED [build 2/6] WORKDIR /src                                                                                                                                                                         0.0s
 => CACHED [base 2/2] WORKDIR /app                                                                                                                                                                          0.0s
 => CACHED [final 1/2] WORKDIR /app                                                                                                                                                                         0.0s
 => [build 3/6] COPY [rasp-test.csproj, .]                                                                                                                                                                  0.4s
 => ERROR [build 4/6] RUN dotnet restore "rasp-test.csproj" -r linux-arm64                                                                                                                                 12.9s
------
 > [build 4/6] RUN dotnet restore "rasp-test.csproj" -r linux-arm64:
#0 12.26 MSBUILD : error : This is an unhandled exception in MSBuild -- PLEASE UPVOTE AN EXISTING ISSUE OR FILE A NEW ONE AT https://aka.ms/msbuild/unhandled. [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :     System.NullReferenceException: Object reference not set to an instance of an object. [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Regex1_TryFindNextPossibleStartingPosition(RegexRunner, ReadOnlySpan`1) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Regex1_Scan(RegexRunner, ReadOnlySpan`1) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at System.Text.RegularExpressions.Regex.ScanInternal(RegexRunnerMode mode, Boolean reuseMatchObject, String input, Int32 beginning, RegexRunner runner, ReadOnlySpan`1 span, Boolean returnNullIfReuseMatchObject) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String inputString, ReadOnlySpan`1 inputSpan, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String input, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at System.Text.RegularExpressions.Regex.Replace(MatchEvaluator evaluator, Regex regex, String input, Int32 count, Int32 startat) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.MetadataExpander.ExpandMetadataLeaveEscaped(String expression, IMetadataTable metadata, ExpanderOptions options, IElementLocation elementLocation) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.ExpandIntoStringLeaveEscaped(String expression, ExpanderOptions options, IElementLocation elementLocation, LoggingContext loggingContext) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionElement(ProjectItemDefinitionElement itemDefinitionElement) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionGroupElement(ProjectItemDefinitionGroupElement itemDefinitionGroupElement) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate() [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate(IEvaluatorData`4 data, Project project, ProjectRootElement root, ProjectLoadSettings loadSettings, Int32 maxNodeCount, PropertyDictionary`1 environmentProperties, ILoggingService loggingService, IItemFactory`2 itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCacheBase projectRootElementCache, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, EvaluationContext evaluationContext, Boolean interactive) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance.Initialize(ProjectRootElement xml, IDictionary`2 globalProperties, String explicitToolsVersion, String explicitSubToolsetVersion, Int32 visualStudioVersionFromSolution, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings, EvaluationContext evaluationContext) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.<>c__DisplayClass60_0.<LoadProjectIntoConfiguration>b__0() [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.InitializeProject(BuildParameters buildParameters, Func`1 loadProjectFromFile) [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildProject() [/src/rasp-test.csproj]
#0 12.26 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildAndReport() [/src/rasp-test.csproj]
------
Dockerfile-arm64-2:9
--------------------
   7 |     WORKDIR /src
   8 |     COPY ["rasp-test.csproj", "."]
   9 | >>> RUN dotnet restore "rasp-test.csproj" -r linux-arm64
  10 |     COPY . .
  11 |     RUN dotnet build "rasp-test.csproj" -c Release -r linux-arm64 --no-self-contained -o /app/build
--------------------
ERROR: failed to solve: process "/bin/sh -c dotnet restore \"rasp-test.csproj\" -r linux-arm64" did not complete successfully: exit code: 1

Test image of .NET SDK 7.0

Build with docker buildx build -f .\Dockerfile-sdk -t uchitesting/rasp-test:test-2 --load . Run with docker run --rm -ti uchitesting/rasp-test:test-2

FROM mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /src
#COPY ["rasp-test.csproj", "."]
#RUN dotnet restore "rasp-test.csproj" -r linux-arm64
COPY . .

ENTRYPOINT ["/bin/bash"]

Files are present and running the compilation commands manually including the target platform runs just fine. That said no mention of target platform on docker command was made. Hence this is not an ARM64 docker image.

Why does build fail when --platform is present?

What would I be doing wrong?
Many thanks.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,407 questions
{count} votes