23

Is there an easy way to debug a process running inside a Linux container on a remote host from Visual Studio?

Imagine a scenario where we have multiple services deployed on some remote machine, running inside docker containers - dev environment. During testing we detect that the internal state of one of the services becomes currupt and need to figure out what's going on in there. We do not want to docker-compose up locally because we might not be able to simulate the scenario.

Instead we want to attach to the running process inside the container.

I read several articles about how to do this, but they are either about Windows containers (https://www.richard-banks.org/2017/02/debug-net-in-windows-container.html) or are just plain confusing (e.g. do I need to install sshd to connect to it from VS? Do I need to install vsdbg as outlined here https://github.com/Microsoft/generator-docker/issues/130 ?)

I tried installing the vsdbg into the container along with ssh, but Visual Studio is not able to find any Remote Connections (in Debug -> Attach to Process).

Is there any tutorial specificly for VS2017 & remote debugging & Docker Linux containers ?

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20
Tomáš Bezouška
  • 1,395
  • 1
  • 10
  • 25
  • Based on this document and one case, use the vsdbg would be different from the sshd: https://visualgdb.com/tutorials/linux/docker/ and https://stackoverflow.com/questions/44401483/remote-debugging-net-core-linux-docker-container-the-current-source-is-diffe, if possible, you could view them with detailed steps. – Jack Zhai Sep 12 '17 at 10:00
  • Did you find any info on this topic? I have a similar question - https://stackoverflow.com/questions/48661857/how-to-debug-a-net-core-app-runnig-in-linux-docker-container-from-visual-studio – Shrike Feb 07 '18 at 15:55

4 Answers4

0

You can try to debug with gdb and ssh like it is showed on this page: https://learn.microsoft.com/en-us/cpp/linux/connect-to-your-remote-linux-computer but this will treat the process as native/unmanaged process. It is basically for debugging C++ applications.

For remote debugging managed processes C#, VB, F# and so on, you can use "remote debugging tools for VS" but they are only for Windows. So for know you can not remote debug C# application on Linux container. Hope this clarifies the things.

Ivelin Ivanov
  • 148
  • 3
  • 14
0

To expose those you would have to install the remote debugging tools. Generally there is a different version for each visual studio when using windows, but this should work from 2017 and later editions of visual studio.

https://learn.microsoft.com/en-us/visualstudio/debugger/remote-debugging-dotnet-core-linux-with-ssh?view=vs-2019

ICodeGorilla
  • 588
  • 1
  • 5
  • 26
0

I did something like this for my django app.

maybe your code isn't in python but the logic is the same:

  • run server in debug mode
  • config to map your local code with the remote one
  • start debug in vscode

in docker create a script that starts server according de value of APP_MODE_DEBUG:

if [[ -n "${APP_MODE_DEBUG-}" && "$APP_MODE_DEBUG" == "yes" ]];then
    echo 'WARNING: running in dev mode'
    echo 'IMPORTANT: remember start server with vscode debug'
    pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000
    
else
    echo 'running in prod mode'
    gunicorn myapp.conf.wsgi:application --conf /app/server_conf.py
fi

if server starts in debug mode, you need play debug in vscode with a configuration like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "MyApp: Debug",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/MyApp",
                    "remoteRoot": "/app"
                }
            ],
            "justMyCode": false
        }
    ]
}

** IMPORTANT:

  • remember change localhost with you remote server and remoteRoot with the location of code inside the container.
  • enable access to port debug in order to access from local

Now you able to play debug in vscode enter image description here

Adán Escobar
  • 1,729
  • 9
  • 15