How to Deploy .NET 4.x Web App Docker Image to Azure Web App Container using Azure DevOps Pipeline

Tharaka Madhusanka
5 min readAug 22, 2021

Hi folks, in this article I’ll bring you how to deploy .net 4.x Web App/API Docker Image to Azure Web App Container using Azure DevOps Pipeline. I had to spent days of sleepless nights to figure the out the approach for this, and as there was no direct solution for this, I thought to share the way I solved this with you :) Here I do not suppose to explain what Docker is.

And In this article I am going to discuss solution for Two Problems,

  1. How to deploy .NET 4.x Web App to Azure Web App Container using Azure DevOps Pipeline
  2. How to overcome the limitation of using OS GDI Calls inside the Azure Web App, for Telerik Reporting Custom Fonts. [https://www.telerik.com/forums/problem-with-roboto-font-in-exported-pdf-5e1c56fefa71]

Prerequisites

  • Understanding of Docker
  • Azure Portal — Understanding of Web Container, Azure Container Registry
  • Azure DevOps Account
  • .NET 4.x
  • Docker Desktop Installed

and I assume that you may have the knowledge on Azure Container Registry, Azure Web Containers & Subscription Plans.

Problem

First let’s create sample web app project on .net 4.x, for here I am using .net 4.7.

.NET 4.7 ASP NET MVC Web App

Now right click on the DockerSample.csproj and in the pop up menu select Add then Select Docker Support.

This will create Dockerfile inside your project, which includes the steps to build the docker image.

using this Dockerfile you can deploy your image to Azure Container sitting inside the Visual Studio. Refer https://docs.microsoft.com/en-us/visualstudio/containers/hosting-web-apps-in-docker?view=vs-2019

But the problem comes when you try to deploy the image using this Dockerfile in Azure DevOps Pipeline. If you try to build the image using this Dockerfile inside Azure Pipeline you’ll get following error.

Azure DevOps Build Failure

What this error says, it cannot find source or obj/Docker/publish, but if you build this inside visual studio, this will build the Docker image successfully.

So as the error says, it cannot find what this source is when inside Azure Pipeline. So what this source is, I had to dig internet days and nights to find what this source is :( But I could find out very clean definition for this source in one of the Microsoft Documents.

Dockerfiles created by Visual Studio for .NET Framework projects (and for .NET Core projects created with versions of Visual Studio prior to Visual Studio 2017 Update 4) are not multistage Dockerfiles. The steps in these Dockerfiles do not compile your code. Instead, when Visual Studio builds a .NET Framework Dockerfile, it first compiles your project using MSBuild. When that succeeds, Visual Studio then builds the Dockerfile, which simply copies the build output from MSBuild into the resulting Docker image. Because the steps to compile your code aren’t included in the Dockerfile, you can’t build .NET Framework Dockerfiles using docker build from the command line. You should use MSBuild to build these projects. https://docs.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2019

So now it’s clear that default Dockerfile doesn’t define the steps to build the Visual Studio Project, instead it is built silently and copies those build files as the source by visual studio itself. Thus, it is clear that cannot use this default Dockerfile in side the Azure Pipeline as it has no step to build the Visual Studio Project.

So we have to write custom Dockerfile which includes the steps to build the project and to copy build files, instead of default Dockerfile.

Solution

Create custom Dockerfile includes the following steps

  1. Restore Nuggets
  2. Build the project using Msbuild as a Release
  3. Create Docker Image

So the custom Dockerfile is,

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8 AS build
WORKDIR /app
COPY *.sln .
COPY DockerSample/*.csproj ./DockerSample/
COPY DockerSample/*.config ./DockerSample/
RUN nuget restoreCOPY DockerSample/. ./DockerSample/RUN msbuild /p:Configuration=Release -r:FalseFROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
WORKDIR /inetpub/wwwroot
COPY --from=build /app/DockerSample/. ./

Note — This docker file should be in the root level of the solution.

Now your application structure looks like following,

Dockerfile in the root level

you can build & run the image in the local environment using the following docker command (I assume that you may have installed docker locally)

docker build -t dockersample.
docker run -d -p 8080:80 --name dockersample dockersample
docker build -t dockersample.
docker run -d -p 8080:80 --name dockersample dockersample

Azure DevOps Pipeline

So in above we figured out the exact way we can build the Dockerfile including the missing the steps in default Dockerfile. So, here will how we can build & release this image in Azure DevOps.

For this we need 3 steps,

  1. Build the Image
  2. Push the build image to Container Registry
  3. Deploy the image to Azure Web App Container
Build Docker Image
Push the above image to Container Registry. I have used Azure Container Registry
Deploy the image to Azure Web App Container

Note For the 3rd step you must have created the Azure Web App Container in Azure portal.

That’s all for this :D

This approach is low cost compared to Azure Cloud Service (VM) to overcome the above Telerik Problem.

So, Read this and raise your comments and clap and share if you enjoy this :)

--

--