Xamarin Studio was the de facto IDE for developing cross platform mobile apps on a Mac. Visual Studio for Mac which was launched in late 2016, replaced Xamarin Studio. While Xamarin Studio was a mobile first IDE, its successor is a mobile first and cloud first IDE that supports various workloads which weren’t present in XS. That means, with VS for Mac, you can build not only mobile apps, but also Asp.Net Core apps that are fully cross platform. VS for Mac also provides excellent debugging support for the asp.net core web apps. With the recent improvements to the IDE, VS for Mac supports containerizing the asp.net core app from right within the IDE. It also enables developers to debug the apps, running within docker containers. In this post, I will walk you through how to containerize an asp.net core with the help of VS for Mac and how to debug the web app running within the container.

Prerequisites:

Step 1 : Create a new Asp.Net Core Web application

Creating a new asp.net core is as simple and easy as clicking File -> New Solution. You should be seeing a section called .Net Core which contains all the different project templates for .net core and asp.net core applications. For this demo, I will be using ASP.Net MVC based template. The screenshots below illustrate the same

1.1 Select the Project Template

Screen Shot 2017-12-02 at 3.25.51 PM
Fig 1.1 .Net Core Project templates

1.2 Select the .Net Core SDK version

Screen Shot 2017-12-02 at 3.26.08 PM
Fig 1.2 Select .Net Core SDK version

1.3 Provide the name for the project & Choose Version Control

Screen Shot 2017-12-02 at 3.26.35 PM
Fig 1.3 Select Project Name and Version Control

1.4 Default Project structure

Screen Shot 2017-12-02 at 3.28.43 PM
Fig 1.4 Default Project structure

Once the project is created, VS will automatically restore the dependent Nuget Packages. Just to make sure everything is fine, you can just run the app to see if its showing up properly.

Step 2 : Containerizing the Asp.Net Core MVC application

Adding docker support for the MVC application is very simple. Just right click on the project and select Add -> Add Docker Support.

Screen Shot 2017-12-02 at 3.29.52 PM
Fig 2.0 Adding Docker Support

Once the operation is complete, you should see another project “docker-compose” added to the solution. Also, VS for Mac would have added a couple more files in the actual project

  • .dockerignore – used to specify the files/folders that need to ignored while building the docker image
  • Dockerfile – used to specify the steps required to build a docker image.

Step 3 : Running and Debugging the app inside the container

Now that we have two projects in the solution, choose the docker-compose project as the startup project and click on the play button.

Screen Shot 2017-12-02 at 3.50.35 PM
Fig 3.0 Choosing the startup project

VS for Mac will run the docker-compose project, which in-turn uses the Dockerfile to build the image and then run the image in a container. To make sure if the application is running within a container, open Terminal and run the below command.

Swamis-MacBook-Pro:~ swami$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fe66362970c aspnetcore_docker_demo:dev "tail -f /dev/null" 7 minutes ago Up 7 minutes 0.0.0.0:32769->80/tcp  dockercompose12688170413189 597694_aspnetcore_docker_demo_1

Now, you can place a breakpoint inside any of the controller’s method and access the view to see the application breaking the execution at the breakpoint you have placed.

vs4mac-docker-debugging

That’s it, now you can debug your applications running inside the docker containers, make sure everything is working as expected and then publish the image to docker hub or azure container registry for further deployments.

In case, you are interested in knowing how to debug asp.net core apps running inside Docker containers from VS Code or VS 2017 check out my previous posts.

Advertisements