'How to run "dotnet publish" with the runtime flag in a multi arch Dockerfile?

I have a set of ASP.NET Core 6.0 services hosted on AWS ECS Fargate. Currently they are running on x64 but I want to experiment using the ARM architecture. Also, I want to produce multi-arch images because developers might need to pull those images locally for troubleshooting purposes.

Currently, my publish stage looks like the following

FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish -r linux-x64 --sc

Note the -r linux-x64.

So here is the question:

Assuming I'm using buildx build to build the image,

docker buildx build -f .\src\apps\API\Dockerfile -t api --platform linux/amd64,linux/arm64 .

How can I pass the proper architecture to the dotnet publish command?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source