Visual Studio is the most productive IDE I have ever used so far. I have been using VS since the 2003 version and the amount of improvements this IDE has gone through till now is absolutely stunning. No other IDE gives so much importance to Productivity than VS, Period. Though there are umpteen number of changes in the latest version (VS 2017) right from installation experience, to a lot of tooling improvements, one aspect which I loved the most is the Docker Support for Asp.Net Core applications. Deploying and debugging an asp.net core application running within a Docker container have never been so easy. In this post, I will guide you how to add Docker Support for Asp.Net core application in VS 2017 and debug the application running within the Linux container.

Prerequisites :

Below are the two components that are required to be installed in the machine.

The installation behaviour of VS 2017 is completely new and different than the previous versions. With this version, you get to choose what kind of workloads you want to install and only those get installed. So, to get dotnet core tooling and docker support you have to select that workload from the installation screen as shown below:

vs2017_dotnetcore_tools

Fig 1.0 Installing .Net core and Docker tools in VS 2017

Step 1 : Create a new asp.net core project

Click File ->New Project -> .Net Core -> ASP.NET Core Web Application

aspnetcorestart
Fig 1.1 Add new ASP.Net core project

Step 2 : Enable Docker Support for Asp.Net core application

As you can see in the screenshot below, once you select the template of your application (Web API or MVC based WebApplication), we get an option to Enable Docker support. It’s that easy to dockerize your asp.net core application. In case you don’t want to enable docker stuffs now, you can always uncheck and then configure later.

vs2017_dockersupport
Fig 1.2 Enable Docker Support while project creation

In case, you didn’t check the “Enable Container (Docker) Support” while creating the application, or if you want to add docker support for an existing application, all you need to do is, just right click the project and Add -> Docker Project Support / Docker Solution Support as shown below. If you have a solution with multiple asp.net core projects, you can select Docker Solution Support

vs2017_adddockersupport
Fig 1.3 Add Docker Support after project creation

Once the Docker Support is enabled for the application, VS will automatically add the highlighted files in the below screenshot

vs2017_dockerfiles
Fig 1.4 Docker files added by Visual Studio

Also the debug toolbar will show the new option “Docker” next to the Play button.

vs_docker_debug_options

Step 3 : Deploy the app to containers and Debug

Simply select “Docker” as the destination and click the play button. If you are running the application for the first time, it might take some time to pull the docker image specified in the Dockerfile, from the next run, it should be up and running faster. Add breakpoints to the appropriate files and you should see the breakpoint getting hit as you browse through those pages in the application

giphy

As you can see, this is definitely a great tooling experience for docker in VS. This helps a lot in debugging and troubleshooting errors in your apps that runs within containers. Once everything is done, you can very well build an image for the application that will be guaranteed to run in any machine that has docker in it. This is just one of the various features that VS 2017 offers and there’s still a lot more in Docker tools, more on those in a future post

Advertisement