【发布时间】: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