Hum …no thanks ! – Lordinaire

Page 2 of 18

Working with Docker in Visual Studio 2017

Docker is a well-know tool in Microsoft ecosystem since few years. You can of course doing great things if you deal with Azure but it isn’t limited to “ops”. Developers can now easily play with Docker inside Visual Studio with some additional tools. Stuff like starting a container becomes so easy that everybody must use it! (I like Docker, a lot!)

Setup & Tools

First, you need to download and install Docker for Windows and select the .NET Core option in the installer of Visual Studio 2017.

Bonus: You can additionally download Kitematic (Visual Docker Container Management) if the CLI is not your friend.

Project sample

Let’s create a new ASP.NET Core 2.0 project of type WebAPI. The main benefit to target a .NET Core project is that it can run on Windows AND Linux platform.

Docker for Windows is able to run both of platforms type for a container but not a mixed use of it. So, you need that all your containers target the same platform, you can’t set a specific platform for a container. That’s the only limitation that I face off (and it can be a huge one…).

You can check the Enable Docker Support now or add it after when the solution will be created. If you doesn’t set the option, le solution explorer looks like that :

To activate the Docker support, right clic on the project and select Add > Enable Docker Support. Visual Studio will now automatically create a new project named docker-compose and a Dockerfile in your project. Well, thank you VS.


FROM microsoft/aspnetcore:2.0
ARG source
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]


version: '3'

    image: webapplication1
    context: ./WebApplication1
    dockerfile: Dockerfile

Start the project

Before starting the project, we need to check if the shared drives settings are set properly because Docker required full access to drives where the source code.

Update 04/11/17 : This is only needed if you use Linux containers with Docker for Windows. You can avoid that by using the option Switching to Windows containers.


To run your project with Docker, you need to set the docker-compose project as startup project. Hit F5 and open the console window : you will see all the steps like downloading the image, creating the container, …

Bonus: You can add breakpoints like you always do and it will work!

Bonus 2: If you launch Kitematic you will see your container with all his settings




Managing health check

Do I need an health check ?

When you have an application who calls several middlewares that manage many services, it might be helpful to monitor all that stuff to know if a service is up or down. You can achieve that by using Health Check. It is nothing more than a system that call a service (or a group of services), each specific interval of time, to test if it responds to a request. For example, it can be just a simple check to a specific route of a WebAPI. If it returns a 200, everything is fine. If it returns a 500, you need to do something because your service is down :)

I need a medic !

Let’s start with a simple architecture :

Give me a favor, help me

Your app call 2 services. In that case, it can be great to monitor both of us. Service1 uses HTTP to communicate with the app so it’s easy to call a web request to have the status of that one. For the Service2 it is a little bit different because it uses a socket and, for simplicity, we just want to use web request. The easy way is to start a tiny web server, in the service, with a single route in order to do the health check.

Great, we have two routes to call to check the status of our services but who will do the job ? Surprise, we need a new web server/console app/whateveryouwant to display a monitoring page that will check each X seconds the status of all the services.

Any feedback, doc ?

We are now very happy to have an amazing page with some red and green dots but it’s not entirely satisfying. What if my app can be notified when the status of a service changes ? Let me explain why it can be useful and terrific for your users. In our app, we want to add a chat that uses Service1. With health checks, you can retrieve the status of all your services, including Service1 who is needed to be able to chat.

Your first idea may to call your monitoring page or directly do a health check on Service1 with a polling system but that’s a bad idea. Your application can be used by thousands (hundreds?) of users simultaneously and each of it will do a health check each X seconds ! That’s not even an option. It may be smarter that your app be notified (push notification) when the status of a service changes, by using WebSocket for example. When you receive the notification, you can enable or disable the chat depending on the status of Service1. Amazing !

As we can see, health checks is not very difficult to deal with and can highly increase your application. Your users will thank you :)

C# implementation sample :



Page 2 of 18

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: