
In a previous article, we talked about the use of Docker containers for development purpose, underlining the advantages that the usage of the Containers can bring in the software development process. One of these advantages is the simplification of the deployment process because container images can be deployed in a simple way.
The good news is that we can use containers to host .NET Core applications, so that we can have fun with this technology using our Microsoft skills. Don’t you trust me? Ok, follow these simple steps:
- If you have not installed .NET Core yet, install it from https://dotnet.microsoft.com/download
- In the terminal create a folder, for example with the command mkdir demo, and move to the created folder with the command cd demo
- Execute the command dotnet new webapi to create an API project from a preconfigured template provided by the .NET Core CLI (Command Line Interface)
- Execute the command dotnet run to run the application on your machine
- Open the browser to the address https://localhost:5001/api/values
If there are no errors, you will see the following result:

This project is a simple example of a test API, but we can run this project in a Docker container creating a Dockerfile script in the application root with the following Docker commands:
FROM microsoft/dotnet:sdk as build
RUN mkdir app
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build /app/out/. .
CMD dotnet demo.dll
Starting from the official dotnet:sdk image by Microsoft, that contains all the .NET Core SDK needed for the compilation process, we create an “app” folder (RUN mkdir app) and use it as the working directory (WORKDIR /app). This operation permits us to execute all next command to this folder. We copy all the content of the project to the container (COPY . .), restore the NUGET dependencies (RUN dotnet restore) and publish the application in a folder named “out” with the “Release” configuration (RUN dotnet publish -c Release -o out). All these operations are part of a first step that we named “build” (FROM microsoft/dotnet:sdk as build).
In the second part of the script, we start from the ASP.NET Core runtime image by Microsoft, that contains only the runtime components to execute an ASP.NET Core application (FROM microsoft/dotnet:aspnetcore-runtime). In this image, the app folder already exists, thus we only need to set it as working directory (WORKDIR /app) and copy the result of the previous step from the “out” folder (COPY –from=build /app/out/. .). When the final image runs, the container executes the dotnet command with the demo.dll file as a parameter, generated with the previous publish command (CMD dotnet demo.dll).
We are ready to create our image with the following command:
docker build -t demo:1.0.0 .
The result of the command is the following:

We can run the created image to test it with the command:
docker run -it -p 80:80 demo:1.0.0
The result is the following:

If the application is composed of more than one container, the process of deploy can be a little more complicated, because we need to manage the version of each container and the communication between them. To achieve this aim, the best solution is a container orchestrator, and today, this means Kubernetes.
Kubernetes, or K8s, is an open source container orchestrator started by Google and today maintained from Cloud Native Computing Foundation. You can use it in your on-premise infrastructure or with one of the leading cloud provider like Microsoft, Google, or Amazon. Once you understand basis principles and components behind Kubernetes, you can ask it to deploy your containers, to scale them as you want, to connect them depending on your requirements and security strategy, and update your application with the best strategy that fit your needs.
If you are interested in Docker, Kubernetes and .NET Core and you want a practical guide to start working with these technologies, you can download the book that I have written for Syncfusion for free at the link https://www.syncfusion.com/ebooks/using-netcore-docker-and-kubernetes-succinctly

I have written this book with my typical practical approach, creating from zero a .NET Core application that uses a SQL Server Database, and using Docker both for development and production environment. After the creation of the image, I explain in details how to deploy the application on a Kubernetes cluster installed on our machine. In the last chapter, you can read how to deploy the same application on an AKS (Azure Kubernetes Service) cluster, customizing the data storage type to use the solutions offered by Azure and profit by the monitoring tools offered from the Microsoft Cloud.
Moreover, if you are in Naples, on 26th June, we have organized, with the Cloud Native Computing Foundation meetup of Naples, an evening to spend time together talking about these technologies, starting from a technical session on Docker, Kubernetes and Azure. All the details, here: https://www.meetup.com/it-IT/cncfnapoli/events/261921731/
Happy coding!