python - 在 VS Code 中调试 Python Docker
问题描述
我的调试器有问题,我无法完全理解。我的问题是:在容器中运行我的代码时,更改不会持续存在。(我已宣布我的卷)
我的设置如下:我创建了一个 python 虚拟环境,然后使用以下内容创建了一个 python 文件(只需将文件写入磁盘)。然后我通过教程使用 docker 命令来添加我所有的 docker 文件。它创建了一个 docker-compose 和一个 docker-compose.debug。我为这两个撰写文件添加了一个卷,以便我的文件将持续存在。如果我执行 docker-compose up 文件将被执行,我看到一个 test.txt 文件到达我的目录。但是,如果我通过调试器运行,它似乎忽略了我的卷声明,然后我的文件不会出现在我的主机目录中。你有什么想法我做错了吗?
https://code.visualstudio.com/docs/containers/quickstart-python
测试.py
import datetime
text_file = open("test.txt", "w")
text_file.write(str(datetime.datetime.now()))
text_file.close()
码头工人-compose.yml
version: '3.4'
services:
test:
image: test
volumes:
- ${PWD}:/app
build:
context: .
dockerfile: Dockerfile
docker-compose.debug.yml
version: '3.4'
services:
test:
image: test
volumes:
- ${PWD}:/app
build:
context: .
dockerfile: Dockerfile
entrypoint: /bin/bash
command: -c "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 test.py"
ports:
- 5678:5678
dockerfile
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
# Install pip requirements
ADD requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
ADD . /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd 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", "test.py"]
解决方案
我发现了一些或多或少会做我想做的事。使用以下作为模板,我创建了一个远程附加配置文件,该配置文件附加到端口 5678 上的任何进程。然后,无论是我的 Web 应用程序还是普通的 python 文件,我在容器上打开端口 5678,添加 pvtsd,然后指示无论我想调试哪个文件来等待附件。似乎工作得很好。
推荐阅读
- python - Macbook M1 上的 Pip TypeError
- google-colaboratory - 在 Google Colab 中撤消单元格删除
- javascript - 从异步函数获取 e.target 值的问题
- python - 在 jupyter notebook 上导入 csv 数据的问题
- swift - 我们如何获得上一行和下一行的范围?
- python - 如何比较两个列表以保持匹配的子字符串以及如何保留基于子字符串的顺序?
- sql - SQL:检测具有相同键的连续行的连续块
- css - 如何应用纵横比媒体查询
- azure - Azure 数据工厂错误:“附近有不正确的语法”
- python - 如何将模型的输入张量传递给损失函数?