question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to setup a remote debugger with vscode

See original GitHub issue

Description

Hi, I am trying to set up the vscode debugger with a remote container for FastAPI. Here’s my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Attacher",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/app"
                }
            ]
        }
    ]
}

In my docker-compose file, I export the ports 5678 (debugger) and 8000 (api) and the command is: uvicorn myapp.main:app --host 0.0.0.0 --port 8000 --reload .

In main.py, I’ve added the followling lines before uvicorn.run(...):

import ptvsd

ptvsd.enable_attach(address=('0.0.0.0', 5678), redirect_output=True)

When I try to run the debugger, I have this error happening in the vscode console (Help > Toggle Developer Tools):

  ERR timeout after 500 ms: Error: timeout after 500 ms
    at t.RawDebugSession.handleErrorResponse (file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2798:1003)
    at file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2798:434
    at async t.RawDebugSession.shutdown (file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:2796:536)
    at async file:///Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js:5182:409

In my process I checked a lot of resources to no avail such as:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
TheCDCcommented, Jan 15, 2021

@Kludex I used your solution as a template and I can debug and hit breakpoints, thank you.

Launch.json

{
  "configurations": [
    {
      "name": "Python: Remote Attach",
      "type": "python",
      "request": "attach",
      "port": 5678,
      "host": "localhost",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/app",
          "remoteRoot": "/app"
        }
      ]
    }
  ]
}

docker-compose.debug.yml

version: "3.3"
services:
  backend:
    environment:
      - SERVER_NAME=${DOMAIN?Variable not set}
      - SERVER_HOST=https://${DOMAIN?Variable not set}
      # Allow explicit env var override for tests
      - SMTP_HOST=${SMTP_HOST}    
    env_file:
      - .env
    build:
      context: ./backend
      dockerfile: backend.dockerfile
      args:
        INSTALL_DEV: ${INSTALL_DEV-false}
    command:
      [
        "sh",
        "-c",
        "pip install debugpy && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000",
      ]
    volumes:
      - ./backend/app:/app
    ports:
      - 8000:8000
      - 5678:5678


networks:
  traefik-public:
    # For local dev, don't expect an external Traefik network
    external: false

Command to run backend in debug mode

docker-compose -f docker-compose.debug.yml up

5reactions
Kludexcommented, Jan 15, 2021

You’re welcome. 😗

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visual Studio Code Remote Development
Visual Studio Code Remote Development allows you to use a container, remote machine, or the Windows Subsystem for Linux (WSL) as a full-featured...
Read more >
SSH, Remote and Live Debugging in VS Code with Lightrun
VS Code offers a variety of tools for debugging difficult scenarios - in local environments, in far-away remote servers and beyond the ...
Read more >
How do attach to a remote Java debugger using Visual Studio ...
Create a Debug Configuration like below and press F5 to debug : "version": "0.2.0", "configurations": [ { "type": "java", "name": "Debug ...
Read more >
Debugging - vscode-docs
To debug your app in VS Code, you'll first need to set up your launch configuration file - launch.json . Click on the...
Read more >
Azure functions remote debugging in Visual Studio Code
Microsoft Q&A is the best place to get answers to all your technical questions on Microsoft products and services. Community. Forum.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found