Monday, January 30, 2023
Debug a container in Visual Studio Code running in Docker
#containers #debugging #docker #dotnet-core #visual-studio-code #web-api
This article is published at GitHub.You can raise issues, create pull requests or even fork the content...its open source.
In this article, you will learn how to debug a container in Visual Studio Code running in Docker.
The following prerequisites will be required to complete this tutorial:
Open a dotnet core web application or api in Visual Studio Code.
Select the Run and Debug tab, and then select create a launch.json file.
Enter D into the search box and then select Docker: Debug in Container.
Add the following JSON to the launch
JSON file, replacing the following values.
Parameter | Value |
---|---|
name | Display name of your choice |
containerName | Your running container |
sourceFileMap (left path) | Path where the application code / dll is published in the docker container (this will be defined in your docker file) |
sourceFileMap (right path) | Path of the project in the Visual Studio Code workspace |
{
"version": "0.2.0",
"configurations": [
{
"name": "Monitored.API",
"type": "docker",
"request": "attach",
"containerName": "monitoredapi",
"platform": "netCore",
"sourceFileMap": {
"/src/Monitored.API": "${workspaceFolder}/Monitored.API"
},
"netCore": {
"debuggerPath": "/remote_debugger/vsdbg"
}
}
]
}
Add the following volume mapping to the service you would like to debug in the docker.compose.override
yaml file.
- ~/.vsdbg:/remote_debugger:rw
Select Terminal > New Terminal.
Run the following docker compose up
command in the Terminal.
docker compose -f docker-compose.yml -f docker-compose.override.yml up -d --build --force-recreate
Select the Run and Debug tab, and then choose the configuration you created in the Add visual studio code launch json file step above from the RUN AND DEBUG configuration list. Select the Start Debugging (play) icon.
Add a breakpoint to your application or api and then send a request that will hit the breakpoint.
Select the Disconnect plug icon to disconnect the debugger from the container.
All my articles are written and managed as Markdown files on GitHub.
Please add an issue or submit a pull request if something is not right on this article or you have a comment.
If you'd like to simply say "thanks", then please send me a so the rest of Twitter can see how awesome my work is.