Debug ASP.NET Core on Docker with Visual Studio Code

Debug ASP.NET Core on Docker with Visual Studio Code

Last Thursday I started reading the Free eBook: “Containerized Docker Application Lifecycle with Microsoft Tools and Platform” by Cesar de la Torre. The book is really easy to read and really gives the reader a glimpse on the way to approach the overall application lifecycle when using containers and Microsoft Technologies. One of the interesting things he mentions is the ability to debug your source code inside a container using Visual Studio or Visual Studio Code, so I decided to try it out and Debug ASP.NET Core on Docker with Visual Studio Code

First be aware of the following prerequisites:

PrerequisitesCommand
.NET Core 1.1 SDK for WindowsDownload and install: .NET Core 1.1 SDK for Windows
Docker for WindowsDownload and install: Docker for Windows (stable)
Visual Studio Code and C# PluginDownload and install from here: Code
Node.jsDownload and install from here: Node.js v6.9.2
YeomanFrom a Command Prompt run the following: npm install -g yo
Generator-dockerRun the following command: npm install -g generator-docker
BowerRun the following command: npm install -g bower
GulpRun the following command: npm install -g gulp

Now let’s create an ASP .NET Core application and debug it inside Docker:

Create an ASP .NET Core application


Open a command prompt and run

    md debug.on.docker
    cd debug.on.docker
    dotnet new -t web
    dotnet restore
    code .

Change your application port in Program.cs


Let’s use port 5000 to host the application

   public static void Main(string[] args)
   {
            var host = new WebHostBuilder()
                .UseUrls("http://*:5000")
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
   }

Create the artifacts needed to Debug on Docker


Run the following command in the root folder of your project.

    yo docker

Answer the questions as follows:

What language is your project using? .NET Core
Which version of .NET Core is your project using? rtm
Does your project use a web server? Yes
Which port is your app listening to? 5000
What do you want to name your image? debug.on.docker
What do you want to name your service? debug.on.docker
What do you want to name your compose project? debugondocker

Change dockerTask.ps1


The Yeoman generator is not ready for .Net Core 1.1 so change dockerTask.ps1 line 47 with

   $framework = "netcoreapp1.1"

Change the docker files


Change the fist line of both Dockerfile and Dockerfile.debug in order to use the latest image for .NET Core 1.1

   FROM microsoft/dotnet

Debug your application


Place a break point in your code (i.e. line 14 of Program.cs) and hit F5 in Visual Studio Code.

The first time it will take some time, but your application will run as a container and you’ll be able to debug it!!!

You can get a copy of the code here: https://github.com/cmendible/dotnetcore.samples/tree/main/debug.on.docker

Hope it helps!

Last modified December 12, 2024: new post (bf52b37)