简体   繁体   中英

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. Specifically I'm using docker-compose, PyCharm professional and python 3.5. This is what I've done so far:

  • I copied "pycharm-debug-py3k.egg" into the project folder
  • I added the following lines in the wsgi.py file:

     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)

by running the debug configuration (click on the bug icon) from PyCharm it prints (and hangs):

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:

Could not connect to 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)

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

Add a remote python SDK to your IDE, this will then also resolve all the libraries being installed remotely. 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.

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.

Use host.docker.internal instead of 0.0.0.0 . This will allow docker to decide which IP to use.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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