In my previous post I showed you how to debug asp.net core apps running in Docker containers using VS 2017. In this post, let’s see how simple it is to debug asp.net core apps without Visual Studio 2017. One of the interesting and inviting aspects of Asp.Net core development is its Cross Platform goodness. That means, technically you don’t need a windows machine to develop applications targeting asp.net core. You could very well use a Mac/Linux machine to perform end-to-end development. For non-windows users, where Visual Studio 2017 is not available, VS Code comes for the rescue and that’s exactly what I will be using for docker debugging and development as well.

Prerequisites:

  1. Visual Studio Code
  2. Dotnet core
  3. Node js – to install other npm packages
  4. Yeoman – scaffolding tool
  5. Yeoman Docker Generator – to scaffold required dockerfile & docker-compose files

Step 1 : Create a new asp.net core mvc application

Creating a new dotnet core project on a non-windows machine can be done by using dotnet cli.


dotnet new -t web

Created new C# project in /Users/swami/Projects/DotnetCore/docker-debug-demo.

To verify everything is fine, run the below two commands to restore the packages and build the application


dotnet restore

Restoring packages for /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Restoring packages for tool 'Microsoft.AspNetCore.Razor.Tools' in /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Restoring packages for tool 'Microsoft.EntityFrameworkCore.Tools' in /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Restoring packages for tool 'Microsoft.Extensions.SecretManager.Tools' in /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Restoring packages for tool 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' in /Users/swami/Projects/DotnetCore/docker-debug-demo/project.json...
Writing lock file to disk. Path: /Users/swami/Projects/DotnetCore/docker-debug-demo/project.lock.json
/Users/swami/Projects/DotnetCore/docker-debug-demo/project.json
Restore completed in 14297ms.

dotnet build
Project docker-debug-demo (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling docker-debug-demo for .NETCoreApp,Version=v1.0
Compilation succeeded.

0 Warning(s)
0 Error(s)
Time elapsed 00:00:02.4744234

Step 2 : Add Docker support

The easiest way to add docker support is by using yeoman docker generator. Simply by running the command yo docker and answering the subsequent questions, you can add docker support for your project.


? 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? 8000

? What do you want to name your image? docker-debug-demo

? What do you want to name your service? docker-debug-demo

? What do you want to name your compose project? dockerdebugdemo

create .vscode/launch.json

create Dockerfile.debug

create Dockerfile

create docker-compose.debug.yml

create docker-compose.yml

create dockerTask.ps1

create dockerTask.sh

create .vscode/tasks.json

We noticed your project.json file didn't use portable .pdb files. We've fixed that for you.

Your project is now ready to run in a Docker container!

Run dockerTask.sh to build a Docker image and run your app in a container.

Open the folder in VS Code by typing code . from the terminal and take a look at the folder structure. Yo docker command would have added the below docker related files:

screen-shot-2016-12-17-at-6-01-18-pm
Fig 1.0 Files added for docker support + debugging
  • Dockerfile – contains instructions to build docker image
  • Dockerfile.debug – contains instructions to build & debug docker images
  • docker-compose.yml – contains instructions to orchestrate the service
  • docker-compose.debug.yml – contains instructions to orchestrate & debug
  • dockerTask.ps1 – Powershell scripts to be used in Windows environment for triggering debugging tasks
  • dockerTask.sh – Bash script to be used in non-windows environment for triggering debugging tasks

You should also see the below files under .vscode folder under your root directory of the project.

  • launch.json – used by VSCode to launch the debugging activities
  • task.json – used by VSCode to identify the tasks available for execution
  • settings.json

Step 3 : Deploy & Debug the app running in container

To run the application in VS Code, navigate to the Debug View by pressing (shift + command + D on Mac) / (shift + control + D) and click on the play button. This should start building the docker image by using Dockerfile.debug and then run the application by triggering docker-compose.debug.yml

Apply breakpoints in any of the controller methods and when you navigate to the same, you should see the application stops at the breakpoint within VS Code.

VSCode-Docker-Debug.gif
Fig 1.1 VS Code debugging asp.net core apps running in Docker

That’s it! Its that simple to debug dockerized apps using VS Code. I personally found this feature very useful for dockerized developments.

Advertisements