9

I'm trying to setup native debugging for a python script running in docker for Visual Studio Code using debugpy. Ideally I'd like to just F5 and be on my way (including a build phase if needed). Currently I'm bouncing between a timeout caused from debugpy.listen(5678) inlined within the VS code editor itself (Exception has occurred: RuntimeError timed out waiting for adapter to connect) or a connection refused.

I created a launch.json from the documentation provided by microsoft:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Integration (test)",
            "type": "python",
            "request": "attach",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/test",
                    "remoteRoot": "/test"
                }
            ],
            "port": 5678,
            "host": "127.0.0.1"
        }
    ]
}

building the image looks like this so far:

Dockerfile

FROM python:3.7-slim-buster as base
RUN apt-get -y update; apt-get install -y vim git cmake

WORKDIR /
RUN mkdir .cache src in out config log
COPY requirements.txt .
RUN pip install -r requirements.txt; rm requirements.txt

#! TODO: config folder needs to be a mapped volume so they can change creds without rebuild
WORKDIR /src
COPY test   ../test
COPY config ../config
COPY src/   .

#?   D E B U G   I M A G E
FROM base as debug
RUN pip install debugpy
CMD python -m debugpy --listen 0.0.0.0:5678 ../test/edu.employer._test.py

#!   P R O D U C T I O N   I M A G E
# FROM base as prod
# CMD [ "python", "/test/edu.employer._test.py" ]

Some examples I found try to simply things with a docker-compose.yaml, but I'm unsure if i need one at this point.

docker-compose.yaml

services:
    tester:
        container_name: tester
        image: employer/test:1.0.0
        build:
            context: .
            target: debug
            dockerfile: test/edu.employer._test.Dockerfile

        volumes:
            - ./out:/out
            - ./.cache:/.cache
            - ./log:/log

        ports:
            - 5678:5678

which I based off a the CLI command: docker run -it -v $(pwd)/out:/out -v $(pwd)/.cache:/.cache -v $(pwd)/log:/log employer/test:1.0.0;

"critical" parts of my script just listen and wait for the bugger:

from __future__ import absolute_import

# Standard
import os
import sys

# 3rd Party
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()

# 1st Party.  NOTE: All source files are in /src, so we can add that path here for testing
# and batch import all integrations files.  Not very clean however
sys.path.insert(0, os.path.join('/', 'src'))
import integrations as ints

screenshot

2
  • Not sure but this might help you stackoverflow.com/questions/64293300/… Commented Dec 27, 2021 at 11:50
  • Does anybody have a clue how reach that in PyCharm? I am able to use Remote Debugging but I am not able to attach to debugpy of a dockerized Flask server (it listens on 0.0.0.0)? Commented Jul 21, 2022 at 14:33

2 Answers 2

13

You have to configure the debugger with: debugpy.listen(("0.0.0.0", 5678)).

This happens because, by default, debugpy is listening on localhost. If you have your docker container on another host you have to add 0.0.0.0.

Sign up to request clarification or add additional context in comments.

Comments

3

Turns out I needed to create a tasks.json file and provide the details on running the image...

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": ["docker-build"],
            "dockerRun": {
                "image": "employer/test:1.0.0"
                // "env": {
                //   "FLASK_APP": "path_to/flask_entry_point.py"
                // }
            },
            "python": {
              "args": [],
              "file": "/test/edu.employer._test.py"
            }
        }
    ]
}

and define a preLaunchTask:

{
            "name": "Docker: Python",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
              "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/test",
                    "remoteRoot": "/test"
                }
              ],
              //"projectType": "django"
            }
          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.