Mic*_*idt 5 remote-debugging python-3.x docker visual-studio-code vscode-debugger
我正在尝试使用 debugpy 为 Visual Studio Code 在 docker 中运行的 python 脚本设置本机调试。理想情况下,我只想 F5 并在路上(如果需要,包括构建阶段)。目前,我debugpy.listen(5678)在由 VS 代码编辑器本身内联引起的超时(发生异常:RuntimeError 超时等待适配器连接)或连接被拒绝之间来回跳动。
我从微软提供的文档中创建了一个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 来简化事情,但我不确定此时是否需要一个。
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
小智 5
你必须与配置调试器:debugpy.listen(("0.0.0.0", 5678))。
发生这种情况是因为,默认情况下,debugpy正在侦听 localhost。如果您在另一台主机上有 docker 容器,则必须添加0.0.0.0.
| 归档时间: | 
 | 
| 查看次数: | 2629 次 | 
| 最近记录: |