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

标签 python-3.x docker visual-studio-code remote-debugging vscode-debugger

我正在尝试使用 debugpy 为 Visual Studio Code 在 docker 中运行的 python 脚本设置 native 调试。理想情况下,我只想 F5 并在路上(如果需要,包括构建阶段)。目前我在由 debugpy.listen(5678) 引起的超时之间跳来跳去内联在 VS 代码编辑器本身中(发生异常:运行时错误超时等待适配器连接)或连接被拒绝。
我从微软提供的文档中创建了一个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"
        }
    ]
}
到目前为止,构建图像看起来像这样:
文件
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
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;我的脚本的“关键”部分只是倾听并等待错误:
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

最佳答案

您必须使用以下命令配置调试器:debugpy.listen(("0.0.0.0", 5678)) .
发生这种情况是因为,默认情况下,debugpy正在监听本地主机。如果你在另一台主机上有你的 docker 容器,你必须添加 0.0.0.0 .

关于python-3.x - 使用 debugpy 和 vs 代码在 docker 容器中调试 python 导致超时/连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013251/

相关文章:

python - 每次测试后如何重置 Django 测试数据库 ID?

visual-studio-code - 用于 Visual Studio Code 的 LESS 格式化程序

python-3.x - 将数据框“扩展”为矩阵索引

python - 两人骰子游戏的得分问题

docker - Docker:使用--volumes-from备份数据文件

WordPress、Git 和 Docker

angular - 为什么 vscode 删除我的空间?

f# - VSCode FSharp Interactive 终端中的奇怪字符

python - 从 str 和 Enum 继承有哪些注意事项

python - 如何创建新的 Set Redis?