How to remote debug python code in a Docker Container with VS Code

后端 未结 2 605
半阙折子戏
半阙折子戏 2021-01-31 11:20

I\'ve just registered for this question. It\'s about if it\'s possible to remote debug python code in a Docker Container with VS Code. I am having a completely configured Docker

相关标签:
2条回答
  • 2021-01-31 11:51

    works with vscode 1.45.0 & later. for reference files https://gist.github.com/kerbrose/e646aaf9daece42b46091e2ca0eb55d0

    1- Edit your docker.dev file & insert RUN pip3 install -U debugpy. this will install a python package debugpy instead of the deprecated one ptvsd because your vscode (local) will be communicating to debugpy (remote) server of your docker image using it.

    2- Start your containers. however you will be starting the python package that you just installed debugpy. it could be as next command from your shell.

    docker-compose run --rm -p 8888:3001 -p 8879:8069 {DOCKER IMAGE[:TAG|@DIGEST]} /usr/bin/python3 -m debugpy --listen 0.0.0.0:3001 /usr/bin/odoo --db_user=odoo --db_host=db --db_password=odoo
    

    3- Prepare your launcher file as following. please note that port will be related to odoo server. debugServer will be the port for the debug server

    {
        "name": "Odoo: Attach",
        "type": "python",
        "request": "attach",
        "port": 8879,
        "debugServer": 8888,
        "host": "localhost",
        "pathMappings": [
            {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/mnt/extra-addons",
            }
        ],
        "logToFile": true
    
    0 讨论(0)
  • 2021-01-31 11:56

    Yes, this is possible - when the Python app is running in a Docker container, you can treat it like a remote machine.

    In your Docker image, you'll need to make the remote debugging port available (e.g. EXPOSE 3000 in the Dockerfile), include the ptvsd setup in your Python app, and then publish the port when you run the container, something like:

    docker run -d -p 3000:3000 my-image
    

    Then use docker inspect to get the IP address of the running container, and that's what you use for the host in the launch file.

    0 讨论(0)
提交回复
热议问题