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

1    md debug.on.docker
2    cd debug.on.docker
3    dotnet new -t web
4    dotnet restore
5    code .

Change your application port in Program.cs


Let’s use port 5000 to host the application

 1   public static void Main(string[] args)
 2   {
 3            var host = new WebHostBuilder()
 4                .UseUrls("http://*:5000")
 5                .UseKestrel()
 6                .UseContentRoot(Directory.GetCurrentDirectory())
 7                .UseIISIntegration()
 8                .UseStartup<Startup>()
 9                .Build();
10
11            host.Run();
12   }

Create the artifacts needed to Debug on Docker


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

1    yo docker

Answer the questions as follows:

1What language is your project using? .NET Core
2Which version of .NET Core is your project using? rtm
3Does your project use a web server? Yes
4Which port is your app listening to? 5000
5What do you want to name your image? debug.on.docker
6What do you want to name your service? debug.on.docker
7What 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

1   $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

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!