【问题标题】:Debugging python in docker container using debugpy and vs code results in timeout/connection refused使用 debugpy 和 vs 代码在 docker 容器中调试 python 导致超时/连接被拒绝
【发布时间】:2021-01-08 18:53:08
【问题描述】:

我正在尝试使用 debugpy 为在 docker 中为 Visual Studio Code 运行的 python 脚本设置本机调试。理想情况下,我只想按 F5 并继续前进(如果需要,包括构建阶段)。目前,我在 VS 代码编辑器本身内联的 debugpy.listen(5678) 引起的超时(发生异常:RuntimeError 超时等待适配器连接)或连接被拒绝之间反弹。

我根据 microsoft 提供的文档创建了一个 launch.json:

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"
        }
    ]
}

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

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" ]

我发现的一些示例尝试使用 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

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    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"
                }
            }
        ]
    }
    

    并定义一个 preLaunchTask:

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

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2012-08-22
      • 2018-07-14
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 2021-02-21
      相关资源
      最近更新 更多