How to debug a dockerized Django app with PyCharm

Viewed 421

I've a Django app and its dependencies running in docker containers (on Windows 10 with WSL2). This is working fine but I'd like to be able to debug it through PyCharm Pro. Here's my web container in the docker-compose

  # The main container - containing the Django code
  web:
    build: ./web
    ports:
      - "8000:8000"
    depends_on:
      - postgres #wait for postgres to be started, not for ready
      - redis
      - npm_watch
    volumes:
      - ./web:/usr/src/app
      - ./web/collectstatic:/usr/src/app/collectstatic
    env_file: .env
    environment:
      DEBUG: 'true'
      LOCAL: 'true'
    command: su myuser -c "/usr/local/bin/python manage.py runserver 0.0.0.0:8000"

I configured a Docker Compose interpreter targeting the Django app container and created a Django Server Run/Debug configuration see below (up command option is to have all containers started)

enter image description here

When starting the docker-compose, the web container exits almost immediately with exit code 0. After manually restarting that node, it stays up but visiting the homepage only display a connection error.

When connecting to that container I can see the following process running, but only the port 53240 was listened to. Container is not listening on port 8000 python -u /opt/.pycharm_helpers/pydev/pydevd.py --port 53240 --file /opt/project/web/myproject/settings.py runserver --noreload 0.0.0.0:8000

I also tried to add a Docker configuration but it seems that you cannot debug it (debug bottom panel logs nothing)

Any idea how to make this work ? I couldn't find a lot examples for this kind of setup.

Or is it impossible this way and I need to either use remote debugging with PyDevd or run de Django app outside the docker env ? That would be a bummer ... Would it work on other IDEs like Visual Studio Code ?

Thanks :-)

1 Answers

It looks like the unusual project structure screwed with PyCharm. My project contains docker configuration, one of which contains the Django app :

  • MyProject
    • nginx
    • redis
    • postgres
    • web
      • myapp
      • otherapps

Pydevd is running its own command to start the server (after --file) and ignores the command defined in docker-compose. It appears that it relies on the way the Django framework is configured in settings. The Manage Script property was weirdly set to settings.py instead of C:/Users/Martin/workspace/myproject/web/manage.py. This helped pydevd know it should use manage.py instead of settings.py

I also had to specify the environment variable DJANGO_SETTINGS_MODULE in the Django Server configuration, with value myapp.settings instead of settings

I set the Working directory to C:\Users\Martin\workspace\myproject\web in Django Server configuration. It looks like it was using C:\Users\Martin\workspace\mypoject\web\myapp. When changing this, the default folder when connecting with docker-compose exec web bash changed accordingly

After those changes it worked like a ... charm :). Here's the command that is used when starting the container

python -u /opt/.pycharm_helpers/pydev/pydevd.py --multiprocess --qt-support=auto --port 56563 --file /opt/project/web/manage.py runserver 0.0.0.0:8000

I also marked the web folder as source for PyCharm but I'm unsure that helped

Related