Create and Run a Docker Container for ASP.Net MVC4 Application using Azure Windows Server 2016 VM

In this tutorial, we are going to see how to create a Docker Image for an ASP.Net MVC4 application. We will containerize the created image, run it locally and test it. For hosting an ASP.Net MVC4 application we need IIS, so we need a Windows Container (IIS doesn’t run on Linux container). To work with Windows Containers, we need either Windows 10 (Anniversary update) or Windows Server 2016. We are going to use Windows Server 2016 Datacenter VM running in Azure as our development environment.

Lets get started by creating a Windows Server 2016 Datacenter VM in Azure.

image

Fill in the required details like Name and Password.

image

For Size, I have selected DS2_V2 Standard 2 Core 7GB RAM. I left out all the other default settings and created the virtual machine. Remote desktop in to the VM.

Lets enable Container support on our VM. Run Powershell (Run as administrator) and run below command.

Install-WindowsFeature Containers

image

Restart the VM using Restart-Computer –Force. Once restarted, we have to install Docker Engine using PowerShell. First we will download Docker, un-zip it and add it to our path

Invoke-WebRequest https://download.docker.com/components/engine/windows-server/cs-1.12/docker.zip -OutFile “$env:TEMP\docker.zip” –UseBasicParsing
Expand-Archive -Path “$env:TEMP\docker.zip” -DestinationPath $env:ProgramFiles 
$env:path += “;c:\program files\docker”
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;C:\Program Files\Docker”, [EnvironmentVariableTarget]::Machine)

image

Install Docker with following commands.

dockerd.exe –register-service 
Start-Service docker

image

If we do a docker version, we will see that Docker has been installed and running successfully.

image

Now lets pull IIS Docker image from Docker Hub.

docker pull microsoft/iis

image

Run the image using below command.

docker run -d -p 80:80 microsoft/iis ping -t localhost

image

As the Containers 80 port is channeling to VM’s 80 port, we need to create a inbound security rule for VM at Azure portal.

NOTE: Ping –t is used as a watch to the Docker through which container will be kept running in the background. Otherwise the container will quit immediately after it is executed. We will use a different approach (demonstrated below of this tutorial) when actually working with an ASP.net MVC4 solution. We use a infinite while loop as suggested here.

Navigate to Resource Groups, Select Network Security Group of our VM.

image

Select the Inbound Security Rules. click Add.

image

Create a new rule for HTTP 80 as shown below.

image

Now when we browse VM’s Public IP Address, we should see Default IIS Page.

image

To test with actual ASP.Net MVC4 application, lets first install Visual Studio 2017 RC on our VM.

Download VS 2017 RC from https://www.visualstudio.com/vs/. Select ASP.Net and Web Development workload and click Install.

image

After installation, open Visual Studio 2017 RC and create a new Web Application (MVC) with .Net 4.6.1.

image

Run the application and we should see following Home screen.

image

Right click the solution in VS 2017 and select publish. Select Folder.

image

Click Publish.

image

Navigate to published folder \bin\Release\PublishOutput. Create a Dockerfile with following contents (Remember this file should not have any extension).

FROM microsoft/iis

RUN [“powershell.exe”, “Install-WindowsFeature NET-Framework-45-ASPNET”] 
RUN [“powershell.exe”, “Install-WindowsFeature Web-Asp-Net45″]

COPY . c:\\web-app

EXPOSE 8081

RUN powershell New-Website -Name ‘my-app’ -Port 8081 -PhysicalPath ‘c:\web-app’ -ApplicationPool ‘.NET v4.5′

ENTRYPOINT powershell

CMD Write-Host IIS Started… ; \ 
    while ($true) { Start-Sleep -Seconds 3600 }

First of all it uses microsoft/iis as the base image. Then it enables ASP.Net 4.5 and Web features for IIS. It copies all the content from current directory (which is PublishFolder) to C:\\web-app. It then exposes 8081 port of the container. Finally it will create a new IIS Website at port 8081 and map it to C:\web-app directory (where we copied all artifacts previously). .Net v4.5 application pool will be used. Finally we will put a infinite loop with one hour, this is required to keep the container awake.

Once above Docker file is ready, we will build our Docker image. Make sure PowerShell is in the context of PublishFolder directory.

docker build . -t ramiramilu/aspnetapp

image

If we look at docker images, we will have two images now.

image

Lets run the created image.

docker run -d -p 8081:8081 ramiramilu/aspnetapp

image

As we are using 8081 port, we should add a inbound security rule for our Azure VM (in the similar way how we added for port 80 earlier in this tutorial).

image

Navigate to the URL, we should see our ASP.Net MVC4 application up and running.

image

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...