首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用debugpy和vs代码在docker容器中调试python会导致超时/连接被拒绝

使用debugpy和vs代码在docker容器中调试python会导致超时/连接被拒绝
EN

Stack Overflow用户
提问于 2020-09-22 23:46:04
回答 2查看 3.4K关注 0票数 5

我正在尝试使用debugpy为在docker for Visual Studio Code中运行的python脚本设置本机调试。理想情况下,我只想使用F5,然后就可以上路了(如果需要的话,还包括一个构建阶段)。目前,我在VS代码编辑器本身内嵌的debugpy.listen(5678)导致的超时(已发生异常:等待适配器连接时RuntimeError超时)或连接被拒绝之间来回切换。

我从微软提供的文档中创建了一个launch.json:

launch.json

代码语言:javascript
复制
{
    "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"
        }
    ]
}

到目前为止,构建镜像如下所示:

Dockerfile

代码语言:javascript
复制
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" ]

我发现的一些例子试图用docker-compose.yaml来简化事情,但我不确定在这一点上是否需要一个。

docker-compose.yaml

代码语言:javascript
复制
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

它是基于CLI命令的:docker run -it -v $(pwd)/out:/out -v $(pwd)/.cache:/.cache -v $(pwd)/log:/log employer/test:1.0.0;

我的脚本的“关键”部分只需监听并等待bugger:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2020-11-05 04:43:50

您必须使用:debugpy.listen(("0.0.0.0", 5678))配置调试器。

这是因为,默认情况下,debugpy正在侦听本地主机。如果您将docker容器放在另一台主机上,则必须添加0.0.0.0

票数 5
EN

Stack Overflow用户

发布于 2020-09-23 00:56:00

原来我需要创建一个tasks.json文件,并提供运行镜像的详细信息……

tasks.json

代码语言:javascript
复制
{
    // 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"
            }
        }
    ]
}

并定义一个preLaunchTask:

代码语言:javascript
复制
{
            "name": "Docker: Python",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
              "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/test",
                    "remoteRoot": "/test"
                }
              ],
              //"projectType": "django"
            }
          }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64013251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档