Search code examples
python-3.xdockervisual-studio-coderemote-debugging

Python Remote Debugger Doesn't Debug My Code in VS Code


I'm running a flask app on Python 3.6 inside of a Docker container. I'm trying to debug the flask app from VS Code on my Mac.

I've got the remote debugger attaching, although none of my breakpoints get hit, and all appear as "unverified" breakpoints in VS Code.

When I run the remote debugger, it breaks at line 587 of flask's __init__.py, in Resource.dispatchRequest(), which reads resp = meth(*args, **kwargs). Attempting to step forward, step in, or step out yields no results. Hitting continue multiple times (about 20) will proceed to the next request from my test script.

The debugger never stops on my breakpoints in my code and it shows my breakpoints as unverified breakpoints. When the debugger is stuck in the flask request code, the call stack shows methods from my code in gray and says "Unknown Source" next to them. I'm unable to step into them in the call stack, although I can see the local variables in the "Variables" window.

How can I get VS Code to debug my code?


Solution

  • In the launch.json the Attach section has the following default:

    {
        "name": "Python: Attach",
        "remoteRoot": "${workspaceFolder}",
        ...
    }
    

    This default doesn't make sense for a Docker container, unless you're copying your code into it under the exact same path it's located at on the host machine.

    My Dockerfile copies my code into the container as follows:

    COPY . /app/
    

    So the Attach section of launch.json needs to be:

    {
        "name": "Python: Attach",
        "remoteRoot": "/app/",
        ...
    }
    

    And now I can debug my code.