mark_h
mark_h

Reputation: 5477

How can I build a Docker image in an Azure Devops pipeline while specifying the architecture as linux/arm64/v8?

I currently have an Azure Devops build pipeline that creates an image build on the amd64 architecture. When I try to run this on a Raspberry Pi it fails with an error stating that the image needs to be built using linux/arm64/v8 architecture.

Here is my YAML

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - 'XXXXX/*'

variables:
  tag: '$(Build.BuildId)'
  dockerrepo: 'xxxxxxx'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@2
      inputs:
        containerRegistry: 'xxxxx Container Registry'
        repository: $(dockerrepo)
        command: 'buildAndPush'
        Dockerfile: '*/Dockerfile'
        tags: |
          $(tag)

The Docker file builds c# .net6 console app.

How can I get the pipeline to build the image with a specified architecture so that it can run on a Raspberry Pi 4?

EDIT

Also tried supplying platform argument as advised by @szombati_attila but this fails to create an image failing with an invalid argument error

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@2
      inputs:
        containerRegistry: 'xxxx Container Registry'
        repository: $(dockerrepo)
        command: build
        arguments: '--platform linux/arm64/v8'
        Dockerfile: '*/Dockerfile'
        tags: |
          $(tag)
    - task: Docker@2
      inputs:
        containerRegistry: 'xxxx Container Registry'
        repository: $(dockerrepo)
        command: push
        Dockerfile: '*/Dockerfile'
        tags: |
          $(tag)

Upvotes: 0

Views: 2772

Answers (1)

szombati_attila
szombati_attila

Reputation: 11

I would recommend splitting the buildAndPush into two different commands. Because you cannot add any additional argument when you are using the buildAndPush command. This is because the buildAndPush command is a combination of two docker commands (build and push) and the additional argument input would be ambiguous.

After the split, you will have two tasks and the arguments now can be added to the build task.

- task: Docker@2
  displayName: BuildDockerContainer
  inputs:
    command: build
    containerRegistry: registry
    repository: repo
    tags: latest
    arguments: add any label that you want here and supported by Docker (--platform linux/amd64,linux/arm64)

- task: Docker@2
  displayName: PushDockerImageToRegisitry
  inputs:
    command: push
    containerRegistry: registry
    repository: repo
    tags: latest

Documentations:

Azure DevOps Docker-V2:

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/docker-v2?view=azure-pipelines&tabs=yaml

Docker platform flag:

https://docs.docker.com/build/building/multi-platform/

Upvotes: 1

Related Questions