Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug a python container in intellij?

The docker plugin has a debug port for connecting to a container enter image description here

I have a python application, but according to the docs the debug port is only supported for java.

How can I set breakpoints and debug my python container in intellij? Is there some way I could have the python container connect to the intellij python debugger?

Edit: I'm running Windows 10, docker for Windows, and the container is linux. Perhaps I need to manually setup some kind of remote debugging for the intellij Python debugger? Also, might as well ask, must I have the professional version for remote debugging or is there a workaround using community?

like image 707
red888 Avatar asked Oct 10 '17 05:10

red888


1 Answers

You can do that using Python Remote Debugging. Open the configurations window and click on + -> Python Remote Debug

Python Debugger Option

Then you either set a port or keep it blank for Pycharm to find a available port.

Python Remote Debug

Then click on Debug icon to launch the debug server which will show below kind of message

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...

Now you need to setup pydev debugging inside docker. You will need the pycharm-debug-py3k.egg for this. For me I copied to my current Dockerfile folder like below

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

The location for your will change based on the IntelliJ version installed. After that, we need to edit our 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

The debug_test.py when built will have below lines at the top

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

Note: I have used docker.for.mac.localhost as I use docker for mac, but if use Docker for windows then use docker.for.win.localhost. For toolbox or linux you will add the IP of your machine

Since it is docker, we probably want to keep port fixed instead of dynamic like I did. Now we build the docker file and run it.

This will open a popup in pycharm, click autodetect to detect the source mappings

Auto detect

And then you will have your code breakpointed at the main line of your file

Debug Local Execution Remote

like image 103
Tarun Lalwani Avatar answered Sep 28 '22 08:09

Tarun Lalwani