Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging python in docker container using debugpy and vs code results in timeout/connection refused

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

like image 297
Michael Schmidt Avatar asked Sep 22 '20 15:09

Michael Schmidt


People also ask

How do I debug Python code in VSCode?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


2 Answers

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.

like image 157
Wason1797 Avatar answered Oct 09 '22 18:10

Wason1797


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"
            }
          }
like image 34
Michael Schmidt Avatar answered Oct 09 '22 18:10

Michael Schmidt