如何在intellij中调试python容器?

时间:2017-10-10 05:42:11

标签: python docker intellij-idea intellij-plugin

docker插件有一个用于连接容器的调试端口 enter image description here

我有一个python应用程序,但根据docs调试端口仅支持java。

如何在intellij中设置断点并调试我的python容器?有没有办法让python容器连接到intellij python调试器?

编辑:我正在运行Windows 10,Windows的docker,容器是linux。也许我需要为intellij Python调试器手动设置某种远程调试?另外,不妨问一下,我是否必须拥有专业版进行远程调试,或者是否有使用社区的解决方法?

1 个答案:

答案 0 :(得分:7)

您可以使用Python远程调试来实现。打开配置窗口,然后单击+ - > Python远程调试

Python Debugger Option

然后你设置一个端口或保持空白,以便Pycharm找到一个可用的端口。

Python Remote Debug

然后单击Debug图标以启动调试服务器,该服务器将显示以下类型的消息

Starting debug server at port 57588
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('localhost', port=57588, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...

现在你需要在docker中设置pydev调试。您需要pycharm-debug-py3k.egg才能获得此信息。对我来说,我复制到我当前的Dockerfile文件夹,如下所示

cp "/Users/tarun.lalwani/Library/Application Support/IntelliJIdea2017.2/python/pycharm-debug-py3k.egg" .

根据安装的IntelliJ版本,您的位置会发生变化。之后,我们需要编辑Dockerfile

FROM python:3.6
WORKDIR /app
ENV PYTHONPATH=/app:/app/debug
COPY pycharm-debug-py3k.egg /app/debug
COPY debug_test.py /app/
CMD python debug_test.py

构建debug_test.py时,顶部会有以下行

import pydevd
pydevd.settrace('docker.for.mac.localhost', port=55507, stdoutToServer=True, stderrToServer=True)
  

注意:我在使用docker for mac时使用了docker.for.mac.localhost,但如果使用Docker for windows,则使用docker.for.win.localhost。对于工具箱或Linux,您将添加机器的IP

由于它是docker,我们可能希望保持端口固定而不是像我那样动态。现在我们构建docker文件并运行它。

这将在pycharm中打开一个弹出窗口,单击自动检测以检测源映射

Auto detect

然后,您的代码将在文件的主线上打破

Debug Local Execution Remote

相关问题