【问题标题】:How can I get the error message of Python/vscode debugging inside a Docker container如何在 Docker 容器中获取 Python/vscode 调试的错误消息
【发布时间】:2021-10-11 13:29:27
【问题描述】:

我正在使用this workflow 使用 vscode 调试 Docker 容器内的通用 Python 应用程序。

如果我在我的 main.py 中引入错误,例如导入一个不存在的包,并在 VSCode 中按 F5 进行调试,则在构建 docker 映像后,它会静默失败并且不会提供任何有用的错误消息。

例如:

执行任务:docker-run:debug

终端将被任务重用,按任意键关闭它。

如果我删除错误,我有工作代码,并通过手动运行它

docker 运行项目名称

我可以看到代码有效。

但是它仍然无法在 vscode 中调试,静默失败。永远不会到达在第一行设置的断点。 vscode 根本没有产生任何消息。

在这种情况下如何查看错误信息?

Launch.json:

 version: '3.4'

services:
  projectname:
    image: projectname
    build:
      context: .
      dockerfile: ./Dockerfile
    command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 main.py "]
    ports:
      - 5678:5678

docker-compose.debug.yml

version: '3.4'

services:
  projectname:
    image: projectname
    build:
      context: .
      dockerfile: ./Dockerfile
    command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 main.py "]
    ports:
      - 5678:5678

main.py(最小可行):

import time
import requests 
import json

#import package_that_doesnt_exist

time_look_back = 60*15 # last 15min
time_to_fetch=int(time.time()-time_look_back)
print(time_to_fetch)

Dockerfile:

FROM python:3.9.7-alpine3.14

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

RUN apk add postgresql-dev 
RUN apk add musl-dev
RUN apk add gcc

# Install pip requirements
COPY requirements.txt .

RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "main.py"]

【问题讨论】:

  • 我认为你的 launch.json 是错误的。请在我调查您的问题时解决它吗?

标签: python docker visual-studio-code


【解决方案1】:

我尝试了这种方法,希望它适合你;如果不是,我很乐意修复它。

launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Attach (Remote Debug)",
        "type": "python",
        "request": "attach",
        "port": 5678,
        "host": "localhost",
        "pathMappings": [
          {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/app"
          }
        ],
        "justMyCode": false,
        "subProcess": true,
        "redirectOutput": true
      }
    ]
  }

docker-compose.yml

(我通常使用ptvsd在容器内进行调试)

version: '3.4'

services:
  projectname:
    environment:
      - DEBUG=1
    image: projectname
    build:
      context: .
      dockerfile: ./Dockerfile
    command: ["sh", "-c", "pip install ptvsd && python main.py"]
    ports:
      - 5678:5678

Dockerfile中,我在CMD之前暴露了5678端口(只看相关部分)

USER appuser

EXPOSE 5678

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "main.py"]

ma​​in.py

import time
import requests 
import json
import os

if bool(int(os.environ.get('DEBUG',0))):
    import ptvsd
    ptvsd.enable_attach(address=('0.0.0.0', 5678))
    print("--- PTVS ATTACHED, press F5 on VSCode to start Debugging")
    ptvsd.wait_for_attach()

#import package_that_doesnt_exist

time_look_back = 60*15 # last 15min
time_to_fetch=int(time.time()-time_look_back)
print(time_to_fetch)

现在,使用错误包 uncommented 和 env DEBUG=0 启动组合:

使用错误包commented和env DEBUG=0启动组合:

启动组合时出现错误未注释和环境DEBUG=1

然后使用F5开始调试

【讨论】:

  • 感谢您对此进行调查。进行这些更改并按 F5 运行,我收到以下错误:i.imgur.com/pFNy2Ul.png
  • 在按 F5 之前,您是否使用“docker-compose up”启动了容器?
  • 修复了它。再次感谢马蒂亚。
  • 我很高兴蒂姆
猜你喜欢
  • 2023-01-16
  • 2021-03-11
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2011-12-12
  • 2018-12-12
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多