How to Debug Inside a .NET Container with VS Code?
Debugging containerized .NET applications can be tricky, but VS Code makes it straightforward with tasks.json and launch.json. By configuring these files, you can automate setup steps like installing debugging tools and seamlessly attach the VS Code debugger to your running container.
tasks.json: Automating Debug Tool Installation
The tasks.json file allows you to automate essential setup tasks for debugging. Below is an example task for installing the vsdbg debugging tool inside your .NET container:
{
"version": "2.0.0",
"tasks": [
{
"label": "Install vsdbg in Docker Container",
"type": "shell",
"command": "docker",
"args": [
"exec",
"-i",
"your_container",
"sh",
"-c",
"test -f /vsdbg/vsdbg || (apt update && apt install curl -y && curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /vsdbg)"
],
"problemMatcher": [],
"group": "build"
}
]
}
launch.json: Attaching the Debugger
The launch.json file defines how to connect the VS Code debugger to your running container. Below is an example configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker .NET Core Attach - your_container",
"type": "coreclr",
"request": "attach",
"preLaunchTask": "Install vsdbg in Docker Container",
"processId": 1,
"sourceFileMap": {
"/app": "${workspaceFolder}"
},
"pipeTransport": {
"pipeProgram": "docker",
"pipeArgs": [
"exec",
"-i",
"your_container"
],
"debuggerPath": "/vsdbg/vsdbg",
"pipeCwd": "${workspaceFolder}",
"quoteArgs": false
}
}
]
}