Skip to content

Debbuging in Docker

cybersecuritylearning edited this page Sep 30, 2024 · 11 revisions

Setup to make VSCode able to debug directly from docker

First step

On the docker-compose.yaml file, we have to set, on the python server cyberpython, the number of workers to be 1 (--workers) in the gunicorn command also we have to set the numbers of replicas from 2 to 1. We have to expose a port from the same image, like 9001:9001.

Step two

We have to add some code on the /src/server/wsgi.py file.

import debugpy

debugpy.listen(('0.0.0.0', 9001))
debugpy.wait_for_client()
print("connected")

Step three

We have to add an entry in the launch.json file like follows:

     {
 "name": "Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "0.0.0.0",
                "port": 9001
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/src/server",
                    "remoteRoot": "."
                }
            ],
            "justMyCode": true
        },

Now, you can go to script files and put your breakpoints like normal and run the Remote Attach playbook from the debug section of VSCode.

When you deploy your app, comment all the code from the debbuging part and from the docker-compose and put again 16 workers at gunicorn and the number of replicas from 1 to 2.

VERY Important

After you do all the setup, you have to run the debug config from vscode when you start your docker and you access the app from browser. Even though you don't any breakpoints it's a must, otherwise it hangs.

Clone this wiki locally