简体   繁体   English

PyCharm远程调试在docker容器中

[英]PyCharm remote debug in a docker container

I'm having an hard time trying to figure out how to setup a remote debug of a python app (Flask) running in a Docker container. 我正在努力弄清楚如何设置在Docker容器中运行的python应用程序(Flask)的远程调试。 Specifically I'm using docker-compose, PyCharm professional and python 3.5. 具体来说,我使用的是docker-compose,PyCharm professional和python 3.5。 This is what I've done so far: 这是我到目前为止所做的:

  • I copied "pycharm-debug-py3k.egg" into the project folder 我将“pycharm-debug-py3k.egg”复制到项目文件夹中
  • I added the following lines in the wsgi.py file: 我在wsgi.py文件中添加了以下行:

     import sys sys.path.append('../pycharm-debug-py3k.egg') import pydevd pydevd.settrace('0.0.0.0', port=8200, stdoutToServer=True, stderrToServer=True) 
  • I created a remote debug configuration in PyCharm (by mapping the path of my local machine project path to the one in the docker container) 我在PyCharm中创建了一个远程调试配置(通过将本地机器项目路径的路径映射到docker容器中的路径)

by running the debug configuration (click on the bug icon) from PyCharm it prints (and hangs): 通过从PyCharm运行调试配置(单击错误图标),它打印(并挂起):

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

...and in the docker container logs I read: ...在docker容器日志中,我读到:

Could not connect to 0.0.0.0: 4200 无法连接到0.0.0.0:4200

What should I do? 我该怎么办? (My goal is to be able to add breakpoints in PyCharm and stop the execution of the docker container app in order to debug it) (我的目标是能够在PyCharm中添加断点并停止执行docker容器应用程序以进行调试)

Use a remote interpreter, this will solve a couple of issues at once. 使用远程解释器,这将立即解决几个问题。 I already answered this here Rich editors in a Docker development environment 我在这里已经在Docker开发环境中回答了这里的Rich编辑器

Add a remote python SDK to your IDE, this will then also resolve all the libraries being installed remotely. 将远程python SDK添加到IDE,这将解析远程安装的所有库。 You will require an SSH connection to get this done though, so install sshd and use the shared developer key I outlined in my answer. 您将需要SSH连接才能完成此操作,因此请安装sshd并使用我在答案中概述的共享开发人员密钥。

Even though this now a bit more effort, I will give you a lot better results, also in the other sections you did not yet encounter and will find, when doing just a remote-port connection. 即使现在这需要更多的努力,我会给你更好的结果,也是在你还没有遇到的其他部分,并且会发现,只做一个远程端口连接。

If you still want to use the port-based debugging, see Docker: MacOSX Expose Container ports to host machine - this explains how you should understand the attach and the listen part. 如果您仍想使用基于端口的调试,请参阅Docker:MacOSX Expose Container端口到主机 - 这解释了您应该如何理解attach和listen部分。

Use host.docker.internal instead of 0.0.0.0 . 使用host.docker.internal而不是0.0.0.0 This will allow docker to decide which IP to use. 这将允许docker决定使用哪个IP。

也许你应该连接到你自己的主机,你是pycharm in。而不是0.0.0.0而不是你自己的IP地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM