Visual Studio Code-如何在Docker容器中远程调试python代码 [英] Visual Studio Code - how to remote debug python code in a Docker Container
问题描述
我正在尝试在VSC中远程调试dubug python:
Iam trying to remote dubug python in VSC:
这是main.py文件:
It is main.py file:
print('Hello, World')
debug.py:
import ptvsd
ptvsd.enable_attach('my_secret', address=('0.0.0.0', 7102))
ptvsd.wait_for_attach()
Dockerfile:
Dockerfile:
FROM python:3.6-slim
EXPOSE 7102
RUN pip install ptvsd
WORKDIR /app
COPY . .
CMD ["python", "debug.py"]
和它是launch.json文件:
and it is launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app",
"port": 7102,
"secret": "my_secret",
"host": "172.17.0.3"
}
]
}
这是用于构建和启动容器的命令:
It is a command to build and start container:
docker build -t python-for-debug。
docker build -t python-for-debug .
docker run -it -p 7102:7102 python-for-debug
docker run -it -p 7102:7102 python-for-debug
运行调试器时,我得到:
vs代码屏幕
When I run the debugger I get: screen of vs code
在调试控制台上没有任何错误,没有输出,没有问题。没有任何输出用于运行容器。没有Docker日志
No any errors at debug console, not output, not problems. No any output for running container. No docker logs
VS代码版本:1.15.0
Docker版本17.06.0-ce,build 02c1d87
VS Code version: 1.15.0 Docker version 17.06.0-ce, build 02c1d87
推荐答案
您应该使用ptvsd 3,因此将Dockerfile中的RUN行更改为:
Hi you should use ptvsd 3, so change the RUN line in Dockerfile to:
RUN pip3 install ptvsd==3.0.0
也可以在本地运行ptvsd安装:
also run locally ptvsd installation:
$pip3 install ptvsd==3.0.0
有关更多信息,请访问 https:// code.visualstudio.com/docs/python/debugging#_remote-debugging
这对我有用,希望对您有所帮助。
for more info go to https://code.visualstudio.com/docs/python/debugging#_remote-debugging this worked for me, hope it helps.
这篇关于Visual Studio Code-如何在Docker容器中远程调试python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!