【问题标题】:Debugging Python Docker in VS Code在 VS Code 中调试 Python Docker
【发布时间】:2020-09-30 05:09:22
【问题描述】:

我的调试器有问题,我无法完全理解。我的问题是:在容器中运行我的代码时,更改不会持续存在。 (我已经声明了我的卷)

我的设置如下:我创建了一个 python 虚拟环境,然后使用以下内容创建了一个 python 文件(只需将文件写入磁盘)。然后我通过教程使用 docker 命令来添加我所有的 docker 文件。它创建了一个 docker-compose 和一个 docker-compose.debug。我为这两个撰写文件添加了一个卷,以便我的文件将持续存在。如果我执行 docker-compose up 文件将被执行,我看到一个 test.txt 文件到达我的目录。但是,如果我通过调试器运行,它似乎忽略了我的卷声明,然后我的文件不会出现在我的主机目录中。你知道我做错了什么吗?

https://code.visualstudio.com/docs/containers/quickstart-python

Test.py

import datetime

text_file = open("test.txt", "w")
text_file.write(str(datetime.datetime.now()))
text_file.close()

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

【问题讨论】:

    标签: python docker visual-studio-code vscode-debugger


    【解决方案1】:

    我发现了一些或多或少会做我想做的事。使用以下作为模板,我创建了一个远程附加配置文件,该配置文件附加到端口 5678 上的任何进程。然后,无论是我的 Web 应用程序还是只是一个普通的 python 文件,我在容器上打开端口 5678,添加 pvtsd,然后指示无论我想调试哪个文件来等待附件。看起来效果不错。

    https://code.visualstudio.com/docs/containers/debug-python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多