Skip to content

Instantly share code, notes, and snippets.

@gnschenker
Last active July 18, 2020 19:52
Show Gist options
  • Save gnschenker/b7e2ea5973f7db454890d3a566768fe1 to your computer and use it in GitHub Desktop.
Save gnschenker/b7e2ea5973f7db454890d3a566768fe1 to your computer and use it in GitHub Desktop.
Line-by-line debugging .NET Core app running in Docker container using VS Code

Configuring VS Code for Debugging

Here is how to configure VS Code for line-by-line debugging a .NET Core 2.1 application in a Docker container

  1. Start microsoft/dotnet:2.1-sdk interactively

    docker run --rm -it \
        -v ${PWD}:/app \
        -w /app \
        -p 5000:5000 \
        --name my-app \
        --hostname dotnet \
        microsoft/dotnet:2.1-sdk
  2. Install the debugger files needed for remote debugging inside the continer

    apt-get update && apt-get install -y unzip
    curl -sSL https://aka.ms/getvsdbgsh | \
        /bin/sh /dev/stdin -v latest -l ~/vsdbg
  3. Create a new web project with dotnet new web -n web.

  4. Add the following line to the Program class:

    	.UseUrls("http://*:5000")
  5. Run the application inside the container:

    dotnet run -p web
  6. Add the following launch command to the launch.json file in VS Code:

    {
        "name": ".NET Core Docker Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "${command:pickRemoteProcess}",
        "pipeTransport": {
            "pipeProgram": "docker",
            "pipeArgs": [ "exec", "-i", "my-web" ],
            "debuggerPath": "/root/vsdbg/vsdbg",
            "pipeCwd": "${workspaceRoot}",
            "quoteArgs": false
        },
        "sourceFileMap": {
            "/app/web": "${workspaceRoot}/web"
        },
        "logging": {
            "engineLogging": true
        }
    }
  7. In VS Code switch to the debugger pane

  8. Select the correct debug configuration from the dropdown (.NET Core Docker Attach)

  9. From VS Code launch the debugger by hitting the green right arrow in the debugger

  10. In the drop down select the correct process. The process should be looking like this:

    dotnet <process ID>
    dotnet exec /app/web/bin/Debug/netcoreapp2.1/web.dll

    Where in my case <process ID> is equal to 629, but this will be different in your case

  11. Set a break point in the Startup class at line 30.

  12. From the host do a curl localhost:5000 and notice that the break point is hit.

@uroscavic
Copy link

Hi,

shouldn't the 2 values in lines:
--name my-app
and
"pipeArgs": [ "exec", "-i", "my-web" ],
be the same (either both my-app or both my-web)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment