Overview

Containerization has become increasingly popular over the past few years. Not because it's some buzzword, but because it makes shipping software more efficient. In any environment, the goal should be to provide value to everyone that uses the software. Containerization provides a quick way to get the software into the hands of the users while not sacrificing quality.

The Goal

In this blog post you will:

  • Build a .NET Core web app
  • Confirm the web app works
  • Build a Docker image from that web app
  • Build an Azure Container Registry to store our image
  • Deploy the Docker image via CI/CD to Azure WebApps with Azure DevOps
Before building the Docker image, ensure you are using Linux containers if you are working on Windows 10. Open up the Docker application and select Switch to Linux containers....

Prerequisites

If you would like to follow allow with this blog post, you must have:

  • .NET Core installed which you can find here.
  • Visual Studio Code (VS Code) 1.42.1 or above which you can download here.
  • An Azure subscription. If you do not have one, you can sign up for a 30-day free trial here.
  • Azure Command-Line Interface (AZ CLI) installed and configured. To download AZ CLI click here. For instructions on setting up AZ CLI, click here.
  • An Azure DevOps organization and project. If you do not have Azure DevOps, follow these instructions to create an organization.
  • Docker version 2.2.0.4 or later installed on your desktop which you can find here.
  • Code to use for this blog post can be found here. This code will need to be cloned to Azure repos if you would like to follow along in the Azure DevOps portion of this tutorial.

Opening the Web App

In this section you will take a look at the pre-created web app found in the prerequisites section under the GitHub code base.

Creating the Web App

Create a new directory called web on the desktop for the webapp to reside in and change directory (cd) into the web directory from the terminal.

Open up a terminal and run dotnet new to see the options available to create. In the options, you will see ASP.NET Core Web App as shown in the screenshot below.

Run dotnet new webapp to create the webapp. The newly created web apps .NET configurations will be available in the web directory.

Run dotnet publish to pack the application and it's dependencies into the directory

Run dotnet run to run the webapp locally.

Open up a web browser and go to localhost:5001. This port is the default port for a .NET Core web app and shown above in the terminal out.

Packaging The .Net Core Web App into a Docker Image

Now that the .NET Core web app has been tested, you are ready to package it into a Docker image. For this, you'll need a Dockerfile.

Open up VS Code and paste in the following Dockerfile.

If you have not used a Docker file before, please visit this tutorial.
FROM mcr.microsoft.com/dotnet/core/sdk AS build-env
WORKDIR /appdir
EXPOSE 80
EXPOSE 443

# Copy csproj and restore as distinct layers
COPY web/*.csproj ./
RUN dotnet restore

# Copy .NET Core Web App. Release packs the application and its dependencies into a folder for deployment to a hosting system
COPY ./web ./
RUN dotnet publish -c Release -o out

# Build the runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet
WORKDIR /appdir
COPY --from=build-env /appdir/out .
ENTRYPOINT ["dotnet", "web.dll"]

Save the Dockerfile to the web directory that contains the .NET Core web app and cd to the directory.

Run docker build -t webapp . from the directory that contains the Dockerfile. I've named mine webapp.

The docker image is now created successfully.

Creating an Azure Container Registry (ACR)

Now that the Docker image is created, you need a place to store the Docker image. ACR (Azure Container Registry) will store the Docker image.

Open up a terminal and run the following command. This will create an ACR to store the docker image.

az acr create -g your_resource_group -n name_of_acr --sku basic

Now that the registry is created, you will need to log into it from the terminal.

az acr login --name mjlreg

The docker image now needs to be tagged with the registry name and pushed to ACR (Azure Container Registry). Run the following commands to tag and push the docker image.

docker tag webapp name_of_acr.azurecr.io/ms/webapp

docker push webapp name_of_acr.azurecr.io/ms/webapp

Log into the ACR portal and you will see the docker image.

Copy the registry path (everything after docker pull) as you'll need it in the upcoming section.

Deploying the Web App in Azure DevOps

Open up Azure DevOps and choose one of your projects.

Go to pipelines and click the blue New pipeline button as show in the screenshot below.

Choose Azure Repos under the Where is your code? section shown in the screenshot below.

Under the Configure your pipeline section shown in the screenshot below, choose Starter pipeline.

The YAML pipeline you will use is below.

trigger:
- master
pool:
  vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: 'your_sub_name'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      param(
          [string]$RG = your_resource_group,
          [string]$appPlanName = your_app_plan_name,
          [string]$appName = your_web_app_name
      )

      az appservice plan create -g $RG -n $appPlanName --sku B1 --is-linux
      az webapp create -g $RG -p $appPlanName -n $appName -i docker_image_registry_path_in_acr

You are now ready to run the YAML pipeline. Click the blue Save and run button as shown in the screenshot below.

Once the YAML pipeline has ran successfully, you will see an output similar to the screenshot below.

After the YAML pipeline has run successfully, go to App Services in Azure and you will see the web app running.

Click on the URL as shown in the screenshot below and the URL will take you to the running web app.

Congratulations! You have successfully deployed a web app in a container.

Summary

In this blog post you took a hands-first approach to containerizing a .NET Core web app. Containerization has changed the way many developers work today in both testing and production environments. By using a container to build the web app, you were able to ship quickly, efficiently, and not worry about cost.