Containerizing A .NET Core Application Using Docker, ACS And Kubernetes - Part One

In this part we will look towards creating a .Net core basic web API. In part 2 we will look at how to containerize the application using docker. In part 3 we will set up a kubernetes cluster using Azure container service and connect to it using kubectl client. 

In the final part we will be running our application inside the cluster.

Prerequisites

  • Azure subscription
  • A machine with Docker installed and a Docker registry to push our Docker image.
  • A running kubernetes cluster in azure container service.

Brief introduction to .Net core

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

For more information about .Net core please check the official Microsoft site.

  • .NET Core
    Check out the "Getting Started" tutorials to learn how to create a simple .NET Core application. It only takes a few…docs.microsoft.com

Let’s begin the hands on part by creating a basic .Net core API using command line tool:

Step 1 Download the .Net core SDK

You can download the SDK from here for the OS of your choice.

Step 2 Initialize the code

Open the command prompt and use the command line tool to initialize a web API application

dotnet new webapi –n demoservice



Folder structure in VS code

If we look at the demoservice directory, we will be able to find a Controllers directory, inside which there is a ValuesController.cs which is created by default. It’s pretty much the same as asp.net web API controller with Get, Post, Put and Delete methods.

Step 3 Restore the dependent packages

Once you have created the application the next step is to restore the dependencies using dotnet restore

 
dotnet restore

The restore command uses NuGet to download all the dependent packages and project specific tools mentioned in the project file necessary to run the application.

Step 4 Build and run your application

Once restore is complete we can now build and run our appllication with a single command.

dotnet run



dotnet run

The dotnet run command uses dotnet build command to build the application from the source code and then runs your application in a single command.

You can check all the .Net core CLI tool commands below.

Step 5 Check your application in action

To check if the application is working check http://localhost:5000/api/values in your browser.


Note

If you need an IDE for development and debugging, I would recommend using Visual studio code which is pretty light weight and will give you a great development + debugging experience. You can download the latest IDE from below.

In part 2 we will look at how to containerize the above application using Docker and push the image to the docker hub registry.