How to enable docker support ASP.NET applications in Visual Studio

Introduction

In this article, you will know that how to enable docker support for ASP.NET application in Visual Studio. We will create an ASP.NET Core application docker support and also enable docker support in an existing application.

Prerequisites

  • Docker for Windows
  • Visual Studio 2017 or later with the .NET Core cross-platform development workload

Enable Docker support in a new application

You can get Docker support in your project when you create a Visual Studio web project, either. NET Core or the full framework. If you choose the .NET Core framework, you get the option to add Docker support in the new project wizard but for the full framework, we can add Docker support later context menu “Solution Explorer”. See below steps to create a .NET Core project with Linux container support:

clip_image001

Docker tools in Visual Studio understand the difference between. NET Core and the. NET full framework so the generated files will nicely reflect those different targeted platforms.

To add Docker support for the full framework, go through previous post - Containerizing a .NET application

Enable Docker support in a new application

You add Docker support after creating a project is by right-clicking the project in the “Solution Explorer” and then select “Docker Support” option under the Add submenu.

clip_image003

Visual Studio will add DockerFile and .dockerignore to the project that will be used to build a docker container image starts with a reference to the base image dotnet:2.2-aspnetcore-runtime.

clip_image005

Note: To build this container, you need to switch the Docker tools for Windows on your machine to run Linux containers. If it is targeting to different operating system type, then you would get errors during the build since you can't mix Linux containers with Windows containers.

Docker support also added the generated YAML files. YAML files can be used together with docker-compose to execute Docker commands to a set of containers instead of only one at a time so that multiple container can work together for the microservices scenarios.

docker build -f "D:\DevWorkSpaces\GitHub\WebDevLearning\WebDev\WebDev.Containerized.MVCWeb\Dockerfile" -t webdevcontainerizedmvcweb:dev

clip_image007

Build Docker image from CLI

Open command prompt in administrative mode and run the below command  in project folder:

C:\Users\niranjansingh\Source\Repos\WebDevLearning\WebDev\WebDev.AspNETMVC>docker build .

clip_image001

Running application under Docker Environment

For .NET Core framework applications, Just run the application by selecting the Docker option just after the Run arrow button. After that application will build and create Docker image according to the settings provided in the DockerFile.

clip_image009

For my case application is targeting “Linux” and Docker for Windows on my system is configured to run the Windows contains so it will not build my case. So, remember to switch particular target Operating system containers before you build the application.

For a .NET framework application, make docker-compose as startup project. After this modify the .yml files to build and run the contains.

image

You will see Docker Compose button on the place of “Docker” in .NET full framework applications.

image

Click on Debug button to let the docker decompose to build and run the docker image on the bases of yml file configuration.

Containerizing a .NET application

What is Docker?

From Wikipedia:
Docker is a computer program that performs operating-system-level virtualization, also known as "containerization". It was first released in 2013 and is developed by Docker, Inc.

Docker is used to run software packages called "containers". Containers are isolated from each other and bundle their own application, tools, libraries and configuration files; they can communicate with each other through well-defined channels.

All containers are run by a single operating system kernel and are thus more lightweight than virtual machines. Containers are created from "images" that specify their precise contents. Images are often created by combining and modifying standard images downloaded from public repositories.

Unification of container technology

  1. A set of command-line tools to work with containers
  2. A unified way to build Container images
  3. A unified way of maintaining images in a registry
  4. A daemon process that manages the images & networking on a host machine

clip_image001

from: Microsoft doc

What Is a Container?

A container is an isolated, resource controlled, and portable operating environment. A container provides a place where an application can run without affecting the rest of the system and without the system affecting the application.

If you were inside a container, it looks very much like you are inside a freshly installed physical computer or a virtual machine.

Containers are compared to virtual machines, but they are completely different technology.

Below are the benefits of containers over Virtual machines:

  1. Containers provide the isolation of a virtual machine with the lightweight process. Container provides almost the same level of isolation as you see in a virtual machine but is very lightweight in terms of overhead and start-up time.
  2. You can see inside a container and also make changes to it but in case of Virtual machines you have to first start the VM to the Virtual machine architecture and data. You can even make changes in your container, and then the isolation will ensure that nobody else but you can see the changes that you've made.
  3. The container will enable you to fully utilize the host machine resources e.g. CPU, memory, disk, and networking.
  4. Containers does not pre-allocate any resources. It is very light weight comparable to a process. If you run multiple containers on the same host machine, then these are fully isolated from each other and the host.

Containers vs. Virtual Machines

At first look containers and Virtual machines look similar because both uses the hardware and the host operating system. You can run applications on the host operating system, and we can have virtual machines and Containers on the host as well.

Virtual machine has its own operating system and separate application on it. On another hand still have the hardware and, of course, the host operating system interact with the kernel which is responsible for interacting with the hardware handling scheduling of different processes and managing resources like virtual management and CPU cycles.

Containers let us to run our application by sharing the operating system kernel. This kernel has edit capabilities to create isolation between the different containers and never share anything else between the containers. Although Virtual machine provide isolation but using Hypervisor. Virtual machine has its own operating system and applications.

Setting up the Virtual machine operating system and application is a long task but setting up the containers using the images is a faster process. It just requires an image and configure your application on container using these predefined container images.

Container provide faster bootup time rather than the virtual machine. They are up and running within few seconds.

Containers

Virtual Machines

clip_image002

clip_image003

Source: Run Windows Containers

Containerise a .NET application

To create custom image for your application, we require docker tools. In this article, you will know that how to push your containerized application to the docker repository and to Azure App Service.

You can also deploy your application’s container from Visual Studio to Azure Container Registry, and then run it in App Service.

Now it is time to start creating a container image for ASP.NET MVC application using the windows containers.

Prerequisites

To create a .NET application container image, we require below prerequisites tools:

o Install the latest updates in Visual Studio by clicking Help > Check for Updates.

o Add the workloads in Visual Studio by clicking Tools > Get Tools and Features

Note: In this article, Visual Studio 2019 Preview is used to create to the ASP.NET MVC application.

Create/Open an ASP.NET web app


In Visual Studio, create a ASP.NET MVC project by selecting File > New > Project.

In the New Project dialog, select the template Visual C# > Web > ASP.NET Web Application (.NET Framework).

clip_image005

Define the solution and the project name then press create.

clip_image006

select the MVC template and do not forgot to select Enable Docker Compose support.

Select OK to continue.

clip_image007

Once the project is setup then you will find Dockerfile under the project. It defines the structure of the container and which base image will be used to host your application.

clip_image008

Update the docker file with Azure App Server supported base image so the update docker file would be as below:

FROM microsoft/aspnet:4.7.1
ARG source
WORKDIR /inetpub/wwwroot

COPY ${source:-obj/Docker/publish} .

Here is the list of  supported base image.

Create and Publish to Docker Hub

In the Solution Explorer, right-click the created project and select Publish.

clip_image009

Select Container Registry > Docker Hub > Publish. There is another option also available. You can directly publish this application to Azure App Server or Azure Container registry. Although, we took the publish path from docker registry to Azure App service.

clip_image010

Enter your docker credentials:

clip_image011

Now publish process create the docker image using the base image and bundle your application in to the image.

clip_image012

When process complete you will be able to see the pushed docker image in the docker registry at docker hub.

clip_image013

Now setup the Azure Container service to use this docker image.

Create a Windows container app

Sign in to the Azure portal at https://portal.azure.com with your account.

  1. Choose Create a resource in the upper left-hand corner of the Azure portal.
    clip_image014
  2. In the search box above the list of Azure Marketplace resources, search Containers and then select Web App for Containers.
    clip_image015
  3. Provide an app name and let the default option create a new resource group selected, and click Windows in the OS box.
    clip_image016
  4. By default, wizard Create an App Service plan but you can create your custom by clicking App Service plan/Location > Create new. Give the new plan a name, accept the defaults, and click OK.
  5. Now you will configure the container and provide the detail of the create docker image in the step. Click Configure container. In Image and optional tag, use the repository name you created in Publish to Docker Hub step, then click OK. In previous step I have create repository with name “niranjankala\webdevaspnetmvc” as you can see in above docker hub image.clip_image017
    There is an option for the Kubernetes but Kubernetes is only supported on Linux operating system-based applications.
  6. Click Create and wait for Azure to create the required resources.

Once the deployment complete you are ready to browse your containerized application. Now browse “webdevaspnetmvc.azuresites.net” or whatever application name you chosen during the web app service setup process.

clip_image019

clip_image021

Now your containerized application is up and running.

Running the first .NET Core program in Docker environment

What is .NET Core?

.NET Core is a development platform maintained by Microsoft and the .NET community on GitHub. It is cross-platform, which supports Windows, macOS and Linux and it can be used in device, cloud, and embedded/IoT scenarios.

What is Docker?

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. It leverage the speed in developing, packaging and running portable distributed applications.

Docker for Windows is a Docker Community Edition (CE) app.

Note: It requires Microsoft Hyper-V to run and the current version of Docker for Windows runs on 64bit Windows 10 Pro, Enterprise and Education (1511 November update, Build 10586 or later).

Hardware Virtualization must be enabled. It is prerequisites for Hyper-V and also for Docker also.. You can check it in the Task Manager’s performance tab for CPU.

image

Now we start installing Docker on Windows 10 Pro.

First enable Hyper-V and here are the steps:

  1. Press Windows + R and run appwiz.cpl to open “Programs and Features” section of Control Panel.
    clip_image001
  2. Now click on “Turn Windows features on or off”.

    clip_image003
  3. Check Hyper-V in Windows Features section to enable it.

    clip_image004

I found an interesting and easy to understand moving image(gif) to know that how to enable Hyper-V form control panel.

clip_image005

Installing Docker application

  1. First download Docker InstallDocker.msi installer from here and then double-clickInstallDocker.msi to run the installer.
  2. Follow the install wizard to accept the license, authorize the installer, and proceed with the install.
    clip_image006
  3. Follow the install wizard to accept the license,and click Next.
    clip_image007
  4. Click Finish on the setup complete dialog to launch Docker.
    clip_image008
  5. When Docker start without error then you will see Docker icon in the taskbar.
    clip_image010

and Linux virtual machine in the Hyper-V so we need to switch to Windows containers. It may restart your computer as did with me.

and switch to windows container.

Now time to come for creating the first “Hello World” program on Docker and run it. To work with Docker, we have start “Power Shell” to run the CLI command to create the program.

  1. First run the following command to get a container with the dotnet core tools.
    docker run -it microsoft/dotnet:latest
    
  2. Now create “Hello World” console application:
    dotnet new console -o hwapp
    
  3. It will create a directory with name ‘hwapp’. Go into that directory using ‘cd hwapp’ and run the restore command.
    dotnet restore
    

image

 

You can develop your applications by using any supported language by Docker, like Node.js, Java, Go, etc., Right now Microsoft tram working to make better support for .NET Core and .NET Framework in Docker environments. You can try the official  .NET and ASP.NET Core images available at Docker Hub as I have used one of them in above exercise to run the program on the Docker container.

It is quite interesting that these containers are help full to create different type application and host them to make auto build as read so far about them.