Debug Dockerized .NET Core Apps with VS Code

In this post, I will be following suit with some of my more recent posts (here & here) and will be discussing the concept of containerizing .NET Core apps with Docker.  However, in this post I will be deviating a bit from the previous series in order to show you how to use VS Code in order to debug a .NET Core console app running in Docker.  This deviation was due to some popular demand and also inspired by my own interest in developing .NET Core and Docker on my Mac.

VS Code is a lightweight and open-source code editor built on top of the popular Electron framework.  It is not as robust as the full Visual Studio IDE.  Instead, VS Code takes a much more lightweight approach while still providing a number of the same great features:

  • IntelliSence
  • Debugging support for .NET, Python, Node, and many more

 

Beyond the out-of-the-box features, the IDE can easily be extended using modules.  The good news is the current repository of modules is rapidly growing.

 

Brief Note on the Contents of this Post


 

As mentioned previously, this post will provide a step-by-step tutorial and walkthrough of how to generate a sample .NET Core app and then add Docker debugging support.

If you would prefer to just browse the code, please refer to the following GitHub repository: http://github.com/jixer/dockernetcore

In addition, my next post in the Docker series provides less overview and just points you to the resources to get the GitHub sample working: VS Code .NET Core and Docker Debugging Continued.

 

Pre-Requisites


Before we can start with this tutorial, we’ll need to install a couple things:

 

Once VS Code is installed, open the program and navigate to the Extensions section (square icon on left side).  Search for and then install the C# extension.  This will be necessary in order to have C# debugging support:

image002

 

NPM Pre-Requisites


If you installed all of the pre-requisites listed in the previous section, then you should have the Node Package Manager (NPM) installed.  We will be using this to install a few important packages, which will help us scaffold our .NET Core debug-able Docker project.

The first package we need to install is Yeoman.  Yeoman is a package that provides support for numerous generator built by the community.  These generators are used to scaffold projects and provide templates to auto generate files.  To install Yeoman, open a command terminal and run the following

npm install -g yo

**NOTE: From OSX, prefix this command with sudo

 

Next, we’ll install two useful generators: Yeoman Generator for Docker and Yeoman Generator for .NET Core

On Windows:

npm install -g generator-docker
npm install -g generator-aspnet

 

On OSX:

sudo npm install -g generator-docker
sudo npm install -g generator-aspnet

 

We’ll explore these two generators in the next section.

 

Setup the Console App


Let’s put the Yeoman generators to the test.  From within the command window, navigate (using “cd”) to the directory you wish to house our test project.  Once there, run the following command:

yo aspnet

After running this, select the following options

image004

By running the aspnet generator for Yeoman, we created a folder named “VSCodeDebugging” that contains a templated structure of a .NET Core console application.

Now fire up VS Code and then open (File | Open) the directory that we created in the previous step.

image005

Once you’ve opened the directory, you should see several files that were created by the generator that give us a starting point for our .NET Core console application.

image008

Explore these files for a moment and then navigate to the next section where we’ll tie in Docker and debugging support.

 

Docker Debugging Generator


For full information on VS Code debugging, refer to the following site: https://code.visualstudio.com/docs/editor/debugging

Lucky enough for us, we don’t have to configure the debugging support for Docker by hand and will be leveraging the Yeoman generator that we installed earlier.

From within VS Code, open the integrated terminal:

image010

In the terminal window, type the following Yeoman command to run the generator named “docker”:

yo docker

Answer the questions as follows:

image012

After running this command, you should see a number of files generated in our project:

image014

Navigate the files in the .vscode folder as well as the “Dockerfile.debug” and “dockerTask.*” files.  The tasks file in the .vscode folder provides an entry point for the .sh and .ps1 scripts.  These scripts provide abstractions around the docker command line tools and will be the main entry point for all the tasks to kick start containers and debugging.

 

Debugging the App


Before we do anything else, let’s modify the code in Program.cs:

using System;

namespace VSCodeDebugging
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string motd = "Hello World!";
            Console.WriteLine("MOTD: {0}", motd);
        }
    }
}

 

After adding the new code on lines 9 and 10, place a breakpoint on line 10.

image016

Next, let’s initiate debugging.  To start debugging press [F5] in Windows or [fn] + [F5] if you’re using a Mac.  Once you’ve started the debugging related tasks, pay attention to the output:

image018

You should start to see a number of tasks running.  If you watch the output closely, you should see these steps doing the following:

  • Building the image using “Dockerfile.debug”
  • Running docker-compose … up

 

After everything has finished building and being composed, the program should start and you should eventually end up at the breakpoint we set in Program.cs:

image020

There are a couple things to take note of.  On the left hand side, you should see a section that shows us the Local VARIABLES pertaining to the section of code where our break point is.

image022

To see one other interesting feature, hover over the “motd” variable.  It should popup the value under your cursor:

image024

 

Pretty cool right?  We’re debugging a .NET Core console app that is running within Docker!

 

Conclusion


Although this was just a simple Hello World console application, the opportunities this presents are pretty amazing.  Try going through the tutorial again, but when you use “yo aspnet”, try generating and debugging a Web API or MVC application instead.

Although you could easily debug locally prior to composing the Docker containers, debugging within the containers enables you to really test complex compositions prior to pushing the containers to QA.  For example, imagine that you’re building a Web API that consists of a backend MongoDB Docker container.  Instead of requiring that you install MongoDB on your machine, you can just debug the entire solution in one show (including both the MongoDB container and your Web API .NET Core container).

Make sure to navigate the underlying code for this solution via my next blog post in the series: VS Code .NET Core and Docker Debugging Continued.  The example provided in the next post builds upon this example and introduces debugging support for a .NET Core MVC app.  I hope you find this topic interesting and continue on to my next post.  Please use the comments below to send my your feedback.  Also remember that sharing is caring; please use the social icons below to share with your friends and co-workers.

Tags: , , , , ,

Reader's Comments »

  1. […] week, we take a look into the Docker 1.12 native integration of Swarm Mode, debug Dockerized .NET core apps with VS Code, and learn why Docker CEO Ben Golub is leading the charge as the #1 IT disruptor. As we begin a new week, let’s recap our […]

  2. […] 3. Debug Dockerized .NET Core Apps: A step-by-step tutorial and walkthrough of how to generate a sample .NET Core app and then add Docker debugging support by Chris Myers.   […]

  3. […] Blogged By Chris » Debug Dockerized .Net Core Apps With Vs Code […]

  4. […] Insights & Semantic Logging for Service Fabric Microservices by Andrej Medic. ASP.NET Debug Dockerized .NET Core Apps with VS Code by Chris Myers. Introduction to Authentication with ASP.NET Core and Access services inside […]

  5. By Abdul Rauf on August 25, 2016 at 3:56 am

    Cool

  6. […] Blogged By Chris » Debug Dockerized .NET Core Apps with VS Code – […]

  7. By Brian on August 28, 2016 at 3:54 pm

    Thanks for posting this, Chris! Big help.

Leave a Reply