Skip to content

Instantly share code, notes, and snippets.

@ptupitsyn
Created June 15, 2023 13:05
Show Gist options
  • Save ptupitsyn/1c0f4d6186f63c0f09689745242ebee5 to your computer and use it in GitHub Desktop.
Save ptupitsyn/1c0f4d6186f63c0f09689745242ebee5 to your computer and use it in GitHub Desktop.
Working .NET multi-arch Docker build (ARM + Intel)
#!/bin/sh
docker buildx build --platform linux/amd64,linux/arm64 .
# See https://devblogs.microsoft.com/dotnet/improving-multiplatform-container-support/
# 8.0-preview-alpine works too
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-preview AS build
ARG TARGETARCH
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore -a $TARGETARCH
# copy everything else and build app
COPY . .
RUN dotnet publish -a $TARGETARCH --no-restore -o /app
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0-preview
# Needed when csproj is .NET 7
ENV DOTNET_ROLL_FORWARD=Major
ENV DOTNET_ROLL_FORWARD_PRE_RELEASE=1
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["./dotnet-arm"]
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<!-- We use SDK 8 to build, but the project can use .NET 7 or 6. -->
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>dotnet_arm</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Console.WriteLine("Hello, World!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment