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

The docker plugin has a debug port for connecting to a container
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?

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 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
And then you will have your code breakpointed at the main line of your file

Related

Running a Python debugger in a Docker Image

I recently followed the following tutorial to try to debug python code in a Docker container using VSCode:
https://www.youtube.com/watch?v=qCCj7qy72Bg&t=374s
My Dockerfile looks like this:
FROM ubuntu as base
#Do standard image stuff here
#Python Debugger
From base as debugger
RUN pip3 install debugpy
ENTRYPOINT ["python3","-m","debugpy","--listen","0.0.0.0:5678","--wait-for-client"]
I have alternately tried copying the tutorial exactly and using the following ENTRYPOINT instead:
ENTRYPOINT ["python3","-m","debugpy","--listen","0.0.0.0:5678","--wait-for-client","-m"]
I have also configured a VSCode remote attach debug instance to launch.json:
{"name":"Python: Remote Attach","type":"python","request":"attach","connect":{"host":"5678","port":5678},"pathMappings":[{"localRoot":"${workspaceFolder}","remoteRoot":"."}]},
I want the debugger to either debug the current file alone in isolation, or run a file I use to run the entire project, called init.py with the debugger in the docker container.
Currently, when I build and run the docker container with
docker run -p 5678:5678 CONTAINERNAME python3 /home/init.py
It hangs and times out on the Visual Studio side.
In the video, he uses this to run the python unittest module, which is why I tried taking out the -m from the end of the command in my modified version. However, it looks like debugpy doesn't know what to do. I have tried running the docker instance before the remote debugger, or the remote debugger after the docker instance, but the error remains and the debug does not work. How can I remote debug into a docker instance using VSCode?
EDIT:
Thank you FlorianLudwig for pointing out that my original code used commas for the IP rather than the periods required.
I have edited the question to reflect this change. It removed issues where python complained about a malformed address, but it seems I am still having some sort of connection issue to the debugger.
EDIT2:
I think I figured out what caused the connection issue. It appears the visual studio default is to use the same host as the port number in question. I changed my host to 0.0.0.0 and I was able to debug by running the container then connecting to it via Visual Studio Debugging.
In your Dockerfile:
"0,0,0,0:5678" should be "0.0.0.0:5678"
To make it a valid ip address. 0.0.0.0 basically means "any" ip address.

Unable to migrate Django db when using docker container

On my Windows 10 machine, I am developing a database manager. Because the backend uses LDAP and the required development libraries are only available for Linux, I want to use Docker to set up an environment with the appropriate libs.
I managed to write a Dockerfile and compose file, that launch the (currently very basic) Django app in a Docker container with all the libs necessary.
I would like to play around with the django-ldapdb package and for that I want to apply the migrations.
When I open PyCharm's terminal and try to execute python manage.py migrate, I get an error telling me that the module ldapdb is not found. I suspect this is because the command does not use the remote Docker interpreter I set up with PyCharm.
The other thing I tried is using PyCharm's dedicated manage.py console. This does not initialize properly. It says the working directory is invalid and needs to be an absolute path, although the path it shows it the absolute path to the project.
I have to admit that I have no idea how this remote interpreter works and I don't see any Docker container running, so I might have not understood something properly here. I even tried running the app using PyCharm's Django run config, which started a container, but still I get the same errors.
I googled a lot, but I couldn't find more infos about remote interpreters nor something solving my issue.
The only way I managed to do this, is by executing the command inside the container.
To get inside a container named contr, use the docker command
docker exec -ti contr /bin/bash

How can I execute my tests in Debug in PyCharm using a Docker Python Interpreter?

I configured PyCharm Python Interpreter as a remote Docker Python Interpreter:
I do not have any problem to run my tests using this configuration.
But when I try to run them in Debug, execution freeze to this waiting for connection... status:
So my questions are:
What am I doing bad?
How can I execute my tests in Debug in PyCharm using a Docker Python Interpreter ?
This problem is due to pydevd hardcoded host ip address in PyCharm sources (10.0.0.2).
So if you do not use VirtualBox to manage and run your docker-machine, you will not be able to easily launch your tests in debug.
It should be patched in PyCharm 2016.3 for docker interpreter and in PyCharm 2016.3.1 for docker-compose interpreter.
Awaiting this release, a workaround exist here, on JetBrain website.

Running pudb inside docker container

I prefer pudb for python debugging. I am building python applications that run inside docker container.
Does any one know how to make pudb available inside docker container?
Thank you
You need to have pudb installed on the Docker container (this may be done adding this line to the Dockerfile: RUN pip install pudb).
You need to have the ports where you will connect to pudb open. E.g.
For a Dockerfile: add EXPOSE 6900.
For docker-compose the syntax is different:
ports:
- "6900:6900"
You need to add a line to set_trace where you want the entry point to be in the Python code. E.g.
from pudb.remote import set_trace; set_trace(term_size=(160, 40), host='0.0.0.0', port=6900)
When the code is running and reaches that point, you can connect into it with a telnet client and use pudb as you normally would to debug. In the case above, from another terminal type telnet 127.0.0.1 6900.
You can find a repository with a full working example here: https://github.com/isaacbernat/docker-pudb

Restarting and debugging python application in virtual machine

I use Windows host and Ubuntu guest with VirtualBox. Flask application sources are shared from host to guest using VirtualBox shared folders and my virtualenvs and nginx are on guest.
Now I should open SSH session to restart my Flask application (debugging with reload enabled, but in case of syntax error it needs a rerun) and I have no debug capabilities.
I used to use PyDev and Windows installation of Python with debug and restart right from IDE and miss this.
For debugging you can use the remote debugger: http://pydev.org/manual_adv_remote_debugger.html (just point it to your main box IP and things should work).
Now, currently PyDev does not support launching a process through ssh, so, that'd be a new feature request (which you can request at: https://sw-brainwy.rhcloud.com/tracker/PyDev).

Categories

Resources