Docker. NET — My first Hello World!


Preface

Hi, in this tutorial I’ll teach you to code a simple “Hello world” project using the framework ASP.NET Core and pack it inside into a Docker container, for a better, flexible and lean deployment.

This tutorial is dedicated for those who wanted to start a project with these technologies from scratch and they do not find a simply enough tutorial.


ASP.NET Core… what is it?

Before we start talking about ASP.NET Core, we must know what ASP.NET is: this is the model proposed by Microsoft for the development of web applications. Its pages, also known as web forms, consist of standard HTML pages, web controls that are processed on the server and user controls where developers place all the static and dynamic code required by the web page. Here is a image who describes it briefly.

Given the disadvantages raised above, Microsoft released ASP.NET Core; this is a redesign of ASP.NET with architectural changes that result in a leaner, more modular framework. Also it’s open-source, multiplatform and allows building cloud-based, Internet-connected applications.

Why would you use ASP.NET Core?

The main reason why you should use it is because of the lack of dependence on Microsoft technologies: You may find cheaper hosting on non-Windows servers , also, nowadays it is much more common to find servers that have some kind of Linux’s flavor due it comes more prepared for server task administration.

Beyond the freedom that multiplatform provides, the open source feature, makes anyone can improve the source code, making this framework more scalable and ready for the brand new technologies. Plus, the redesign improves performance and includes features such as making multiple instances of an app.


About Docker

Docker is an open source project that is based on the creation of lightweight and portable containers for software applications that can run on any machine with Docker installed, regardless of the operating system that the machine has below, thus facilitating the deployments.

What is a container?

It is something self-contained that it can be carried from one place to another independently, just like the containers carried by the merchandise ships.

Docker, allows me to put in a container all those things that my application needs to be executed and also, the application itself. So I can take that container to any machine that has Docker installed and run the application without having to do anything else, or worry about what software versions that machine has installed

So… Why would you use Docker?

The main reason why you should use it is because it can be found in a scenario in which the application cannot run because it does not have, for example, the appropriate version of .NET Framework, so, you must find that version and install it. Although it is tedious and requires a little time, sometimes these kind of exceptions are very difficult to detect and, many of it are considered bugs when, really, they are not.

The Docker container has all the necessary elements to run the application properly, it’s very useful for testing and we can have running multiple containers as we want.


Getting started

After enough talking, let’s start with our project. The following steps throughout this tutorial are:

  • Install Visual Studio 2017
  • Install Docker
  • Start Codding our hello world
  • Docker integration

Install Visual Studio 2017

Go to the Visual Studio official page and after hover on “Descargar Visual Studio”, click on Community 2017.

After that an installer will be downloaded. When you load it , this window will be appear. I recommend choose the minimum amount of features because they are heavier.

Installer’s main menu

They are (Check all the optional features at right too):

“Desarrollo de la plataforma universal de windows”

“Desarrollo de escritorio de .NET”

“Desarrollo de escritorio de .NET”

“Desarrollo multiplataforma .NET Core”

After picking our options, we wait for the installation to finish and then , restart Windows. With the choosen options, we dont need to install .NET Core apart.

Install Docker

Go to the Docker’s page and click the “Get Docker” button. Depending on the platform,they ‘re many versions of docker, in my case I’ll choose Docker for windows but you have to choose your version according your OS.

Besides, exists two channels of Docker : stable or edge, the difference between them are the actualizations ; stable only upgrades the stable actualizations ,without bugs, and edge actualizes every month with actualizations that could have or not bugs , but , they are the newest. I will select the stable channel.

  • A file will be downloaded in our PC, click on it and, after that, run the setup clicking on the executable file. A window like this will be appear.
  • After finishing the installation, its necessary restart the windows session. Click on “Close and log out” button.

Note: if the error “Hardware assisted virtualization and data execution protection must be enabled in the BIOS” appears, you have to enable virtualization from BIOS. Go to your BIOS’s menu and enable the SVM (Secure virtual machine) property in advanced menu (or something like that, it depends on your motherboard’s fabricant). After that, PC will reboot. On windows features, check all options of Hyper — V features and reboot again. Problem solved.

Start Coding our hello world

Now, after setting up all, we are going to start with the code. My IDE is in Spanish so im going to translate al the paths and important names.

  • Launch Visual Studio 2017 and in a new project dialog (Archivo > nuevo > proyecto or file > new>project) choose the option ASP.NET Core Web Application (Aplicación Web ASP.NET Core) in the language of your preference , I chosen C#.
  • After this dialog, choose Web Application (Aplicación Web) and click on Aceptar.
  • I strongly recommend not check “Enable compatibility with Docker” (Habilitar compatibilidad con Docker) because it could lose the dependencies of the project. I would prefer set it later.
  • So, this is the project solution. Now we‘re going to create our new Hello World page. Right click on Pages folder and select add > new razor page (agregar > Página de razor).
  • Select common > Razor page and click the first option. Set a fancy name, just like HelloWorld and click in “Add” (Agregar) button

We have two files : an HTML view (HelloWorld_Page) and a model .cs HelloWorld.cshtml.cs file.

In the model , create one string variable called message and in the onGet() method assing it a string just like your name or just hello world.

In view we call that variable with the directive @Model.Message and decorate our HTML as we want.

After these modifications, it’s time to run our project. Click on IIS Express and in the web URL add the controller “/HelloWorld”


Docker integration

Yay! We have a working project, now it’s time to pack it into a docker container.

Right Click on project, add > Docker Support (Agregar > compatibilidad con docker) . It will give you the chance to select the containers from your SO’s destination (Windows or Linux). In this image is disable because I selected before.

Note: Sometimes an error occurs because you don’t share your physical drives with Docker, in that case, go to settings > shared drives and check the drives that you want to share. If that option doesn’t appear in your docker, set the container to Linux and in VS project set the destination OS to “Linux”

In the execution menu we’ll see the Docker option. Click on it and wait to start. It will run just like if we run in the IIS.

How can I know if my Docker image was created? Simple, write docker ps in a command prompt or powershell and you ll see the partition name.

Finally we’re going to save this image for the deployment in another PC. In a command prompt write docker images. It will show all the active images.

Then write docker save [image-name] > [file.tar] .In image-name goes the name of the picked image and search it in your PC. The image will be saved as a .tar file mostly compatible with Linux.

In Summary

We learned how to install visual studio, docker, how to start our hello world project, packing it in a docker container and saved it in a .tar file. I really hope that you can put it into practice for your projects, for now on.

I think that it’s a truly complete work, combines very useful tools and adds professionalism to our work. Also, I think that I got extended very much with this post but it worth’s it. Enjoy it!

Final thoughts

I really wanted to make a very quick and simple tutorial. The main idea is that the lector doesnt get bored with tons and tons of text. It isnt a teorical tutorial or a tutorial for an advanced .NET programmer ; it’s just an introduction to a , in my opinion, a very huge world full of posibilities

Resources

The finished example in my github.

Bibliography.

Beginner introduction to ASP.NET

ASP.NET Introduction

Qué es docker

Qué es ASP.NET

.NET Integrado con Docker

Choose ASP.NET Framework

Visual Studio tools for Docker

Docker oficcial page