.Net, Azure, Cloud technology, Docker, HyperV

What is Docker and how to run an ASP.Net Core Web app on Docker (Bonus: Debugging also)

What is Docker and why is it so popular ?

Docker is a container platform which helps in deploying applications as clean and self sufficient containers that eliminates “works on my machine” problems when running of different machines. It helps creating lean containers which Enterprise customers use in delivery software’s faster and more securely to different platforms both Linux and Windows.

But what is a container?

Its a package in which all the dependencies required to run a software is packed, mind you its different from VM because it does not bundle a full operating system, but only the libraries and settings required to make the software work.

Lets see that with an example image from Docker website

containers-vs-vm
Image taken from here

So from the above example we can see that VM contains a full OS for an app whereas container contains only the App and its dependencies and running on top of the Docker engine. Which makes it very lean and very quick to spin off.

Terminologies

  • Container Image : Its a package with all the dependencies like framework, and the settings needed for the deployment and execution which is needed by the container runtime.
  • Container : An instance of an application which is created based on a container image. When you are scaling an application then you add more container by making an instance of the same image.
  • Tag : As the name suggest, its a tag or a name you give to the container image to identify itself.
  • Dockerfile : A text file giving instructions on how to build a container image.
  • Build : Its not usual what we programmers understand when said build meaning compiling the source code, here build means creating a container image with the files provided by following the instructions given in the Dockerfile. You do this with command docker build .
  • Docker Engine : The system which lets you create and run container on top of it.
  • Repository : Its a pool of related Container images. Same container image may have different variants, some with libraries only some with full SDK’s . Also different variants for different OS (linux or windows).
  • Registry : A place which hosts repositories which in turn has different container images. The most common one is Docker Hub . Companies can have their private registry also.
  • Docker Trusted Registry (DTR) : Its a registry service which can be installed on premises so that it remains private to the organization. It can be also attached to the Continuous Integration system. Its connect to the LDAP and AD of the organization. More to read here .
  • Cluster : A collection of Docker hosts. You can create this cluster using tools like Docker swarm , DC/OS, Kubernetes.
  • Orchestrator : A tool to manage Docker cluster and host. It provides command line and graphical interface. It helps in managing the containers and its networks, configurations and also load balancing so that load is distributed to different nodes. Same tool are used for this purpose (Docker swarm, DC/OS, Kubernetes)

Why Docker is so popular?

docker-interest
Google Trends

Docker has been becoming very popular for simple reasons,

  • Its the simplest way to test your application in different operating systems
  • If it works in a container then you can be sure it works in other system also, or it should work.
  • Its faster to spin off a container which make its excellent choice to scale a service.
  • Its very light weight which make it very efficient in terms of resource consumption.
  • More important if you are willing to manage by yourself , then there is a community edition which is free.

Ok now lets get started.

For this post I will work on Windows 10.

Prerequisites

  • Install Docker for windows from here
  • Install Visual Studio 2017 with .Net Core support (at the time of this post I was using .Net core 1.1)
  • Make sure you have Hyper V enabled. Docker needs that.

Lets see some commands to work with Docker

  • docker version docketversion'
  • docker images : this should list all the docker images you have it in your system. Initially it will be empty or one dummy image.
  • docker pull imageid:version : this will pull the docker image with which we work on. Lets use ubuntu 17.10 for this example. So the command  will be
    • docker pull ubuntu:17.10
  • docker run -it id : Check the below image dockerrunBasically, I first check for the images, then I use command docker run -it c6 , where -it means interactive that means I want the command prompt to get started for ubuntu else it will run and exit immediately, then c6 is the minimum characters from the IMAGE ID which makes it unique to other image ids. So this is now running ubuntu and you can try ‘cat /etc/*release‘ to check the version of ubuntu. To exit you can just type exit.

Lets build the Asp.Net core app.

  • Launch Visual Studio 2017 and create a Asp.Net Core web application and select Web App and un-check the option Enable Docker support. Also I have named the project as DockerExample.
  • Change the code of the Home controller a bit so that it looks like this controllerchange.PNG
  • Run the app on IIS server, you should see the following output when you click the About page.aboutpagewindows
  • Now lets publish this app to a folder which we can then publish it to Docker. But before that lets create a Dockerfile which will tell how to build the container image. To do that right click on the project and add a text file without file extension .txt, it should be just with name Dockerfile. Add the content of the Dockerfile like below dockerfile
  • In the file the 1st line means that take the container image microsoft/aspnetcore:1.1 , it contains the runtime to run , 3rd line says that copy all the  files from the current directory to the host directory, 4th line tell the entry point and which command to use to execute in our case its dotnet command.
  • One more important thing, is to set the properties of the Dockerfile to be Copy to output directory always. Right click on the Dockerfile from the visual studio and set the property Copy to Output Directory to be Copy always. So that when we publish, then Dockerfile also gets copied.
  • Now lets publish the app, to do that click on publish
  • On the publish window select the Folder option and browse to a folder location, in my case I have used the following location C:\Users\sherry.ummen\Desktop\DockerExample  and click publishpublishfolder.PNG
  • Now lets go to the command prompt and build the docker image. To do that first lets change the directory to be C:\Users\sherry.ummen\Desktop\DockerExample and type the following command. docker build -t sherry/dockerexample:1.0 .  . Where option -t is for tag.dockerbuild
  • If all is well at your end , then lets run the web app. Type the following command docker run -d -p 8000:80 sherry/dockerexample:1.0  where option -d is to run in background and -p is for port. dockerrunwebapp
  • Lets launch the app , open your favorite browser and navigate to 127.0.0.1:8000 and navigate to About page, you should see this aboutpagelinux
  • As you can see that app is running on linux container.
  • You can stop the service by typing the following commands dockerwebappstop

Now the Bonus part. Debugging Asp.Net Core web app running on docker.

  • First lets add the Docker support to our project, for that first you would have to delete the Dockerfile from the project, because a new one will be added automatically when we add docker support.
  • Now right click on the project and select Add -> Docker Support
  • Your solution should look like this
    dockersupport.PNG
  • You will notice that Docker file already created for us.
  • Now go to Home controller and add a break point at the beginning of About
  • Now click on the run button rundocker
  • Visual studio will do all the publish and launching the app for you, and if everything has gone well then you should hit the break point, when you navigate to About page. breakpoint

That’s all! . As you can see Asp.Net Core and Docker works so well and with Visual studio you get its great power of debugging. Can’t wait to explore more.

Thanks

.Net, Azure

Integrating SSL to Azure Websites

freessl

So after very very long I have finally got some time to share something. Today I will share how to integrate SSL with Azure Website; trust me it can be a bit of pain to get this working specially if you are quite new to this. So in this article I will explain it with all pictures; which I feel is the best way to explain or learn.

So to try this two things is a must.

  • You should have a registered domain. Do not use *.azurewebsites.net for registering SSL since almighty Microsoft provides the SSL support for azurewebsites.net domain. So if you are planning to use *azurewebsites.net then you do not need to have a seperate SSL certificate.
  • The SSL Provider we are going to use here i.e RapidSSL send a mail to the domain administrator mail id which is actually a set of email id’s which RapidSSL support. Here is the list of all supported email id’s (admin@yourdomain.com,administrator@yourdomain.com,
    hostmaster@yourdomain.com,webmaster@yourdomain.com,
    postmaster@yourdomain.com)

Ok Lets get started not.

First go to the URL www.freessl.com and click on TRY button to try with freessl or you are quite sure to buy it then just buy. I will use the free version which is valid for 1month.

Now on clicking on the TRY button you will get series of forms to be filled for which I have attached the screenshots.

freessl_pop1

freessl_pop2

freessl_csr

Now this is the point we need to generate the CSR (Certificate Signing Request). We can do it using IIS Manager or OpenSSL as well. But lets use IIS though we would openssl in later stage but for this purpose lets use IIS Manager.

So press start in windows OS and then type IIS; and you will get IIS Manager. If you do not find it then go to Control Panel -> Program Features -> Turn windows features on or off.

So if you find the IIS manager then you just follow these snapshots

iis_open_feature

iis_create_cert_dialog1

iis_create_cert_dialog2

iis_create_cert_dialog3

Now after you have save this file. Open this file in the Notepad and copy the content and paste it to the freessl website ; here

freessl_csr

Now after pressing the submit you follow these

freessl_continue1

freessl_continue2

freessl_continue_domain_approver

freessl_orderno

After this you should get a mail on the Domain email id you have selected after clicking the link provided on that you should get your CSR to the registered email address. And if you look at the end of the mail you will see the Web Server certificate. Copy that and save it as myserver.crt. Now right click on this file and press install certificate.

Now we need to generate the .pfx file which is needed to be uploaded to the azure. So lets do that.

First lets get the private key; which we get from teh CSR request we made

On the start serach mmc.exe then File -> Add/Remove SnapIn; select Certificates and then Local Computer.

Now go to folder level Personal -> Certificates and you should find the certificate www.whynotme.com.

Click on next and select yes then again next and select to PKCS #12. and then the location of the file and save it as file name privatekey.pfx

Now we need to generate a .pfx file which is password protected and accepted by Azure website. So lets use OpenSSL to generate this.

We need 3 files for this

  1. Private Key file
  2. Web Server Cert from RapidSSL
  3. Intermediate or Chain Cert as bundle which you can get it here and save it as Intermediate.pem

Now we have the private key as .pfx file but we need .key file to generate the final .pfx file. So lets extract the .key file from privatekey.pfx.

You can get the Open SSL installer from here. INstall it in C:\openssl drive with all its binaries. Open command prompt and type the following

set OPENSSL_CONF=c:\OpenSSL-Win64\bin\openssl.cfg

And to generate the .key file

C:\>.\OpenSSL-Win64\bin\openssl.exe pkcs12 -in privatekey.pfx -nocerts -out privatekey.pem

C:\>.\OpenSSL-Win64\bin\openssl.exe rsa -in privatekey.pem -out private.key

Now we have all the files lets generate the final .pfx file which should be uploded to Azure by just running the following command

C:\>.\OpenSSL-Win64\bin\openssl.exe pkcs12 -export -out whynotme_ssl.pfx -inkey private.key -in myserver.crt -certfile intermediate.pem

So the final file which should be uploaded is whynotme_ssl.pfx

Go to your azure website -> Configure; search for Upload Certificate.

ssluploadcert

ssluploaddlg

In the ssl bindings section of the CONFIGURE tab, use the dropdowns to select the domain name to secure with SSL, and the certificate to use. You may also select whether to use Server Name Indication (SNI) or IP based SSL.

sslbindings

  • IP based SSL associates a certificate with a domain name by mapping the dedicated public IP address of the server to the domain name. This requires each domain name (contoso.com, fabricam.com, etc.) associated with your service to have a dedicated IP address. This is the traditional method of associating SSL certificates with a web server.
  • SNI based SSL is an extension to SSL and Transport Layer Security (TLS) that allows multiple domains to share the same IP address, with separate security certificates for each domain. Most modern browsers (including Internet Explorer, Chrome, Firefox and Opera) support SNI, however older browsers may not support SNI. For more information on SNI, see the Server Name Indication article on Wikipedia

Click Save to save the changes and enable SSL.

Thats it. Now try https://yourdomain.com

Hope it works for you as it worked for me. Please leave a comment good or bad or improvements all are appreciated.

Reference : Enable HTTPS for an Azure web site