TomPAP
TomPAP

Reputation: 259

Django / Docker / Remote Debug using Pydev

My setup is the following one : - a django server running in a docker with port mapping: 8090:8090 - Eclipse with PyDev

I want to be able to put breakpoint on Pydev (click on a line, step by step)

I found several articles like; http://www.pydev.org/manual_adv_remote_debugger.html

but it's still not working.

1) Should I update the manage.py to "import pydev" ? which lines to add and do I have to copy inside the docker container the pysrc of pydev plugin in order to be able to do the module import ?

2) Is there a port forwarding needed ? python instance running into docker should have access to remote debug server on host machine ?

3) I found article about pycharm and remote debug using ssh ? not possible to do similar with pydev ?

4) How to "link" my local directory and docker "directory" ?

[EDIT] I found the solution

Upvotes: 5

Views: 4140

Answers (2)

C S
C S

Reputation: 1525

Ok, from what you wrote I will assume you have a Django docker container running on your local machine.

  1. From inside your container (e.g. docker-compose exec <container name> bash to get into it)
    pip install pydevd

  2. in Eclipse, put a breakpoint like this:
    import pydevd; pydevd.settrace('docker.for.mac.localhost')

    If you're not using Docker for Mac, you have to do a bit of work to get the IP of your machine from inside of your container, e.g. see this.

  3. go to Debug Perspective and start the PyDev debug server

  4. start your application or test

... and you should see your views for stack, variables, etc., populate as the code stops at the breakpoint.

In Python 3.7, there is now a builtin breakpoint, which you can configure to point to your favorite debugger using an environment variable (the default is pdb):

breakpoint()

It also takes arguments, so you can do:

breakpoint(host='docker.for.mac.localhost')

I found that a bit annoying to type, so I ended up putting inside an app a module that looks like this:

# my_app/pydevd.py

import pydevd

def set_trace():
    pydevd.settrace('docker.for.mac.localhost')

I then set the environment variable for the builtin breakpoint (say in your docker-compose.yml):

PYTHONBREAKPOINT=my_app.pydevd.set_trace

Upvotes: 0

slav
slav

Reputation: 89

I had the similar issue - django project in docker, connect to docker by pycharm 145.1504 & 162.1120 via docker interpreter, run server works OK, but debug is stack after pycharm runs

/usr/bin/python2.7 -u /root/.pycharm_helpers/pydev/pydevd.py --multiproc --qt-support --client '0.0.0.0' --port 38324 --file /opt/project/manage.py runserver 0.0.0.0:8000.

I tried to find out why for a few days, then connected pycharm to docker by ssh connection and everything works fine, run and debug.

Upvotes: 1

Related Questions