Debug Python with VSCode Remote - Containers

Viewed 1125

I have running container with Python app and WSGI configuration that accepts bunch of GET parameters. The goal is requesting the app through browser(or curl) and get working breakpoints using VSCode Remote - Containers extension.

Host OS: Fedora 28, Container OS: CentOS 7

Docker details:

- Docker v18.09.9
- No Dockerfile(just running container)
- Docker network using
- Container IP is 172.18.0.2
- App directory is mounted from host filesystem

App details:

- No frameworks
- Just Python3 with several modules
- Virtual environment using

IDE details:

- VSCode v1.38.1
- Remote - Containers extension
- Python extension(installed in attached container also)

In VSCode I can Attach to Running Container, then open app directory that mounted earlier and edit code, install packages etc. It's ok, but I stuck with debug configuration, predefined items(Flask, Django etc) don't match my workflow.

How to tweak launch.json file for that purpose?

1 Answers

You should specify which web framework you're using. I'm using pyramid and debugging works just fine.

Here's how I did it:

  • As you said, attach to the running container
  • Use "Open folder..." to open the path where the sources of the app are located. You should NOT add more folders to the workspace, that will not work.
  • Install the Python extension in the running container. If you open the extensions bar, you should have a button saying Install in attached container...
  • From the command palette select View: Show Debug
  • Click on the gears icon in the debug configuration and you should be able to select Python as language.
  • Either way you'll end up with a launch.json file that you can edit.

This is what I have in the file, for Pyramid:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Pyramid Application",
            "type": "python",
            "request": "launch",
            "module": "pyramid.scripts.pserve",
            "args": [
                "--reload",
                "--server-name",
                "docker",
                "${workspaceFolder}/development.ini"
            ],
            "pyramid": true,
            "jinja": true,
            "justMyCode": false,
        }
    ]
}

Notice the "justMyCode": false,. This is quite important if your projects has dependencies from other local projects that get installed when you build the container.

For example, if you have in the requirements file a reference to your git repository, that will be installed in a system directory and by default the debugger will not step into the functions there. This option makes it so that the debugger gets into it.

Related