How do I debug a python container in intellij? How do I debug a python container in intellij? docker docker

How do I debug a python container in intellij?


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 57588Use the following code to connect to the debugger:import pydevdpydevd.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.6WORKDIR /appENV PYTHONPATH=/app:/app/debugCOPY pycharm-debug-py3k.egg /app/debugCOPY debug_test.py /app/CMD python debug_test.py

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

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