ASP.NET Core 1.0 (previously known as ASP.NET 5) is written from ground-up for developing next generation of cloud and web applications that can run cross-platform. ASP.NET Core is currently available in RC1 Update 1 as of this writing and can be downloaded from this link. The coolest thing about ASP.NET Core 1.0 is that, it is open source and we can use it on Windows, Linux and even Mac OSX.
After you have finished building your website/webapp, you need to deploy it. If you are looking for a readymade application developed using ASP.NET Core 1.0, check this article https://www.dotnetcurry.com/aspnet-mvc/1215/building-aspnet-mvc-6-entity-framework-7-app-using-aspnet-5. You can easily deploy an ASP.NET Core application on the Cloud with Microsoft Azure. Usually, while deploying on the cloud, we prefer to create a Virtual Machine (VM). To successfully deploy the web app on this machine, we have to install necessary software’s on the VM e.g. .Net Runtime, IIS, Sql Server, etc. This means that to successfully deploy and execute our application on a VM, we have to configure all dependencies for the application.
What is Docker and where does it fit in?
Docker is an open source project used for automating application deployment inside software containers, by providing a layer of abstraction. Docker allows us to package an application along with all its dependencies into a standardized unit. The Docker Container is used to wrap up a piece of software into a complete filesystem that contains everything that the software needs to run successfully. This has code, runtime, system libraries and system tools which are required to be installed on the server to run the software. Some features of Docker Containers are:
They are based on open standards allowing containers to run on all windows OS and all major Linux OS with support of any infrastructure, e.g. Cloud.
They are lightweight , so all Docker containers running on single machine share same OS Kernel so that they can start instantly and use RAM efficiently.
More information on Dockers can be obtained from this link.
Since ASP.NET Core 1.0 can run on Linux, Docker is becoming popular in the .NET developer community. In this article, we will deploy an ASP.NET Core 1.0 application to Docker.
To implement this, we need to fulfill the following prerequisites:
· Visual Studio 2015 - can be downloaded from this link.
· ASP.NET Core 1.0 - can be downloaded from this link.
· We need Visual Studio 2015 tools for Docker-December preview which can be downloaded from this link.
Install Docker tools for Visual Studio 2015
Using Visual Studio 2015 to create an ASP.NET Core app
Step 1: Open Visual Studio and create a new ASP.NET Application as shown in the following image
Click on OK, this will show another window to select ASP.NET Core 1.0 template. We will select an Empty template as shown in the following image
This application has the Startup.cs class file. Open this class file and in the Configure() method, change the default Hello World line to the line below:
await context.Response.WriteAsync("<h1>Hello, The application is Deployed to Docker Container.</h1>");
Run the application and the following result will be generated:
If you are looking for a readymade application developed using ASP.NET Core 1.0, check this article https://www.dotnetcurry.com/aspnet-mvc/1215/building-aspnet-mvc-6-entity-framework-7-app-using-aspnet-5.
Deployment to the Docker Container
Step 2: Right-click on the project and select the Publish option. This will start the publish wizard. Since we have already installed Visual Studio Docker tools, the Docker Containers option will be displayed on the window as shown in the following image
Step 3: Click on the Docker Containers, a new window will be displayed which will allows us to select an existing VM or create a new VM as shown in the following image
Step 4: Click on New to bring up the Create virtual machine on Microsoft Azure window. In this window, we need to enter the following details:
The above image shows information familiar to developers. The Docker client requires a cryptographic certificate so that it can connect to the remote machine. Since we have installed Visual Studio Docker tool, it will create and manage certificate. Once we click on OK, the Virtual Machine will be created. This will generate a Power Shell script which will be executed. This script will manage certificate creation and resource grouping. This process will be managed using JSON files in the PublishScripts/Templates folder which represents Azure Resource Manager (ARM). ARM is enabled to work with resources for the solution to be deployed. More information about ARM can be gathered from this link. The following diagram shows the resource group creation progress:
Step 5: Once the Host is ready, we can create, upload and run the application container. We will use the publish wizard for the same.
Right-click on the project and select Publish. The following window will be displayed with the connection information as shown in the image
This shows the server URL, image name, port and the certification information generated after running the power script by Visual Studio. The Docker file will be generated by Visual Studio. This will be added to our project. Open this file which contains the following contents:
ADD . /app
Here the From keyword represents the base image of the container. There are various images released for every ASP.NET Core 1.0 release. The current release as of this writing is 1.0.0-rc1-final. A listing of images published by Microsoft can be found from here. The following picture is a snapshot of the images published for ASP.NET Core 1.0.
The Add . /add entry copies the necessary application files in the container, this is necessary to run the application. WORKDIR /app/approot is a working directory for the application. ENTRYPOINT [“./web”] is a command to run when the container starts. All this information is generated by Visual Studio Publish wizard.
Click on Next and publishing will start. After publishing the application, the web site will start. We can see the output in the browser:
The Docker Container provides an easy mechanism to deploy our ASP.NET Core 1.0 application on the cloud. The Docker tools with Visual Studio helps to manage the deployment easily.
Download the entire source code of this article (Github)
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!