This post explains how to run .NET Core application on Docker
.NET Core is a multiplatform framework developed by Microsoft for de development of different type of applications. It is available for Windows, Linux and MacOS and the developer can use .NET Framework utilities (available only for Windows before .NET Core development) to issues like data access, web services, MVC apps, etc.
Docker is a virtualization platform that base on the application container model. A container is a software structure that contains base software and libraries. The developed application is put in this structure and can be moved from one environment to another without compiling it several times, update dependencies, or modify environment configurations. The container has everything that the application needs to run.
In a similar way, we can think of charge containers aimed to transport merchandise in ships. A few years ago, in harbors, there was different infrastructure to transport cars, food, construction materials, etc. The structure used depends on the nature of the merchandise. The container is a universal structure, with standardized measures in which items of any kind can be transported in ships, trains and trucks. The transport means and the infrastructure is adapted to the container and independent of the content.
As the merchandise can move from one place to another, no matter what type is it, an application contained in a container can do so: be moved in different environments and still running.
There are two important concepts in Docker:
- Image: it is a template that contains the developed application and all the dependencies and configurations that it needs to work (OS, libraries, environment variables, files, databases, etc.)
- Container: is an executable instance of the image (can be started, sopped, acceded, modified its files/data, etc.)
This example we see a Web API in C# quite simple, that allows the user list the ATMs in Germany, filtering them by city and/or street. Necessary Docker Files are created, and the container is configured to the application to be acceded.
Previously, you need:
If you don’t have it, we suggest installing them in that order. For Docker installation, you should create an account in the official site and follow the instructions for installation and test everything goes well.
1. Create dotNET Core Web API: you can use PowerShell to execute the commands:
dotnet new webapi -o AtmApi
code -r AtmApi
2. Modify some configurations: Change URL in launchSettings.json file for a propper HTTP Request mapping: api/values for api
Additionally, we delete file Controllers/ValuesController.cs
3. Create necessary classes:
Create a folder Model and add the files: Adress.cs, Atm.cs and GeoLocation.cs.
Create, in the folder Controllers the file AtmControllers.cs with the following code:
- Decorator [ApiController] indicates to the Framework that it is a class that receives HTTP Request and answer them.
- Method Get() is decorated with [HttpGet] to indicate that is who will process GET requests.
- When is invoked, it will return a Collection (IEnumerable) of ATM objects in Json format.
To simplify the example, we take ATM from a Json file that can be found in rhw project repository. We add the code:
WebClient take the Json File and JsonConverter create the list of ATM as C# objects. We use Ok() method to return response code 200 with the filtered values.
To filter by city and street, we add two params to Get() method, with defatult value “” to indicate that they are optional in service invocation. In case the user send any param, the list is filtered.
4. Create Dockerfile: For the proper Docker image creation, we need a file call Dockerfile in project base directory. The file must have this content:
This file contains commands that will be executed when se create the image. For a detailed explanation, see: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
We also create a .dockerignore file to the image has the small size possible. .docekrignore should have:
5. Create the image, with the command:
docker build -t atm api .
6. Create the container, with the command:
docker run — detach –publish 5011:80 — name atm container atm api
run command creates and execute the container. Options specified are:
- — detach: to run the container in background.
- — publish: to associate ports, for this example host machine port 5011 is associated to container port 80.
- — name: to give a name to the container.
- atmapi is the image from which the container to be created.
Conainters (stopped an running) can be listed with docker container ls — all.
Connecting to http://localhost:5011/api/atms will show the list of ATMs:
Complete code on Github is available here: https://github.com/santycerru/AtmApi