Debug Container in Visual Studio Code Running in Docker

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.

Prerequisites

The following prerequisites will be required to complete this tutorial:

Add Visual Studio Code launch JSON file

  1. Open a dotnet core web application or api in Visual Studio Code.

  2. Select the Run and Debug tab, and then select create a launch.json file.

    Visual Studio Code Create Launch JSON File

  3. Enter D into the search box and then select Docker: Debug in Container.

    Visual Studio Code Create Launch JSON File Debug Docker Container

  4. Add the following JSON to the launch JSON file, replacing the following values.

    ParameterValue
    nameDisplay name of your choice
    containerNameYour 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"
          }
        }
      ]
    }
    

Configure docker compose and run a web application or api in a docker container

  1. Add the following volume mapping to the service you would like to debug in the docker.compose.override yaml file.

    - ~/.vsdbg:/remote_debugger:rw
    
  2. Select Terminal > New Terminal.

    Visual Studio Code Open New Terminal

  3. 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
    

    Visual Studio Code Run Docker Compose Up in Terminal

Debug a web application or api in a container

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

    Visual Studio Code Start Debugging Docker Container

  2. Add a breakpoint to your application or api and then send a request that will hit the breakpoint.

    Visual Studio Code Breakpoint Debugging Docker Container

Disconnect Debugger

  1. Select the Disconnect plug icon to disconnect the debugger from the container.

    Visual Studio Code Debugging Docker Container Disconnect Plug

Got a comment?

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.

An unhandled error has occurred. Reload