Search code examples
dockerazure-devops

Docker Image Pull Failed with error "image operating system "windows" cannot be used on this platform" on Azure Devops "windows-latest"


While trying to build the below Docker Image in Azure DevOps using the Microsoft hosted agent "windows-latest" or "windows-2019" getting below error

enter image description here

Docker file content is

# escape=`

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019

# Install Powershell
RUN powershell -Command "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine"

# Install Nuget package provider
RUN powershell -Command "Install-PackageProvider -Name NuGet -Force -Scope AllUsers"

WORKDIR /azp/

COPY ./start.ps1 ./

CMD powershell .\start.ps1

Devops Pipeline YAML

pool:
  vmImage: windows-2019

stages:
  - stage: Build
    jobs:  
    - job: BuildWindows
      steps:
      - task: AzureCLI@2
        inputs:
          azureSubscription: 'XXXXXX'
          scriptType: 'bash'
          scriptLocation: 'inlineScript'
          inlineScript: az acr build --image devops/windows:latest --registry registryName --no-push .
          workingDirectory: '$(System.DefaultWorkingDirectory)/images/windows'

Did tried changing the agent from "windows-latest" to "windows-2019" but of no help

Also, after further research found out that windows-latest or any windows agent has by default use Linux container. So is there any way to switch container to windows in Microsoft hosted agents?


Solution

  • I can reproduce the same issue when using the az acr build command to build windows image.

    enter image description here

    By default, the command will use linux OS to build the docker image.

    To solve this issue, you need to add the --platform windows argument to the az acr build command.

    For example:

    az acr build --image devops/windows:latest --registry xx --platform windows --no-push .
    

    Yaml sample:

    pool:
      vmImage: windows-2019
    
    stages:
      - stage: Build
        jobs:  
        - job: BuildWindows
          steps:
          - task: AzureCLI@2
            inputs:
              azureSubscription: 'XXXXXX'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: az acr build --image devops/windows:latest --registry xx --platform windows --no-push .
              workingDirectory: '$(System.DefaultWorkingDirectory)/images/windows'
    

    Result:

    enter image description here

    For more detailed info, you can refer to the doc: az acr build