From d0b3f6df29f7886445c644c387b9057f35506688 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 10 Jun 2026 22:43:02 +0200 Subject: [PATCH] add multi-architecture support with Docker Buildx --- StandAlone.NETCoreApp.Local/Dockerfile | 7 ++-- StandAlone.NETCoreApp/Dockerfile | 7 ++-- azure-pipelines.yml | 50 ++++++++++---------------- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/StandAlone.NETCoreApp.Local/Dockerfile b/StandAlone.NETCoreApp.Local/Dockerfile index 1f5c84a..6a4fada 100644 --- a/StandAlone.NETCoreApp.Local/Dockerfile +++ b/StandAlone.NETCoreApp.Local/Dockerfile @@ -1,4 +1,5 @@ -FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env +ARG TARGETARCH LABEL maintainer="Stef Heyenrath" @@ -6,11 +7,11 @@ WORKDIR /app # copy csproj and restore as distinct layers COPY StandAlone.NETCoreApp.csproj ./ -RUN dotnet restore +RUN dotnet restore -a $TARGETARCH # copy everything else and build COPY *.cs ./ -RUN dotnet publish -c Release -r linux-x64 -o out +RUN dotnet publish -c Release -a $TARGETARCH -o out --no-restore # build runtime image FROM mcr.microsoft.com/dotnet/aspnet:8.0 diff --git a/StandAlone.NETCoreApp/Dockerfile b/StandAlone.NETCoreApp/Dockerfile index 0b76a00..0fc2dea 100644 --- a/StandAlone.NETCoreApp/Dockerfile +++ b/StandAlone.NETCoreApp/Dockerfile @@ -1,4 +1,5 @@ -FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env +ARG TARGETARCH LABEL maintainer="Stef Heyenrath" @@ -6,11 +7,11 @@ WORKDIR /app # copy csproj and restore as distinct layers COPY StandAlone.NETCoreApp.csproj ./ -RUN dotnet restore +RUN dotnet restore -a $TARGETARCH # copy everything else and build COPY *.cs ./ -RUN dotnet publish -c Release -r linux-x64 -o out +RUN dotnet publish -c Release -a $TARGETARCH -o out --no-restore # build runtime image FROM mcr.microsoft.com/dotnet/aspnet:8.0 diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 7c212b3..02da62d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -7,39 +7,27 @@ variables: trigger: none pool: - vmImage: 'Ubuntu-latest' + vmImage: 'ubuntu-latest' steps: -- task: UseDotNet@2 - inputs: - packageType: 'sdk' - version: 8.0.x - -- task: DotNetCoreCLI@2 - displayName: Build StandAlone.NETCoreApp - inputs: - command: 'build' - arguments: /p:Configuration=$(buildConfiguration) - projects: $(buildProjects) - - task: Docker@2 - displayName: 'Build Docker [$(imageName)(latest,$(tag)]' + displayName: Login to registry inputs: - command: 'build' - containerRegistry: 'DockerRegistry' - repository: '$(DOCKER_ID)/$(imageName)' - dockerfile: '$(Build.SourcesDirectory)/StandAlone.NETCoreApp/Dockerfile' - tags: | - $(tag) - latest + command: login + containerRegistry: DockerRegistry -- task: Docker@2 - displayName: 'Push Docker [$(imageName)(latest,$(tag)]' - inputs: - command: 'push' - containerRegistry: 'DockerRegistry' - repository: '$(DOCKER_ID)/$(imageName)' - dockerfile: '$(Build.SourcesDirectory)/StandAlone.NETCoreApp/Dockerfile' - tags: | - $(tag) - latest \ No newline at end of file +- script: | + docker run --privileged --rm tonistiigi/binfmt --install all + docker buildx create --use --name builder + docker buildx inspect --bootstrap + displayName: Set up Docker buildx + +- script: | + docker buildx build \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag $(DOCKER_ID)/$(imageName):$(tag) \ + --tag $(DOCKER_ID)/$(imageName):latest \ + --file $(Build.SourcesDirectory)/StandAlone.NETCoreApp/Dockerfile \ + $(Build.SourcesDirectory)/StandAlone.NETCoreApp \ + --push + displayName: 'Build multi-arch Docker image [$(imageName)(latest,$(tag)]' \ No newline at end of file