Build Windows Container Images from Visual Studio for ACR

Build Windows Container Images from Visual Studio for ACR
Azure Container Registry is agnostic when it comes to supporting Container Images, they can be both Linux or Windows - Photo by Abby Tait / Unsplash

While Azure Container Registry supports the dual use of both Linux and Windows, it is not always very straightforward how to get Visual Studio (2022) to build up a Windows container image because it defaults to Linux. But there are easy multiple ways to get this done, and once set, we never have to think much about it afterwards.

Build Windows Container Image using Visual Studio and .NET SDK Build

Let's say you have you have your .NET web app or .NET Azure Functions App in Visual Studio ready to deploy and we would like to simply use the publish wizard flow that by default builds a Linux image. We can change this default behaviour by the following:

In the .csproj file, add a PropertyGroup node, for example:

//Do not Use the base SDK 8.0 images or azure-functions -- They Do not work/build
<PropertyGroup>
 <ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:8.0-windowsservercore-ltsc2022</ContainerBaseImage>
 <ContainerRuntimeIdentifier>win-x64</ContainerRuntimeIdentifier>
</PropertyGroup>

This will let our project aware that container image will be based on Windows OS and not default to Linux. We can then easily build the image and set a publish target without even needing a Dockerfile either as we will see below.

Before you deploy, you must edit the Image tag to "windowsservercore-ltsc2022" in the Hosting Edit screen here:

Edit the Image Tag so that deployment to Azure App Service works!!

This edit is crucial because upon attempting to deploy to a platform like Azure App Service later, tags such as 'latest' do not work and windowsservercore-ltsc2022 (or 2019) work correctly on Azure App Service

Next Right click on the project > Publish > New Profile > Azure > Azure Container Registry > (Create or use existing Azure Registry) > .NET SDK > Finish > Close and Publish. We get a successful deployment as Windows in Azure Container Registry.

Image pushed as Windows (Note that the tag has to be NOT latest to be deployed to a platform like Azure App Service)

We are then freely able to use/deploy the image to a host of our choosing (Azure Container Instances, Azure App Service, Azure Kubernetes Service) given we have also allowed ACR Pull Role Assignment permissions on the container registry.

Note that Azure Container Apps DOES NOT SUPPORT WINDOWS container Images

Any further builds of such project will be based on Windows unless changed again in the project settings.