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