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
Then you either set a port or keep it blank for Pycharm to find a available port.
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 usedocker.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
And then you will have your code breakpointed at the main line of your file