cookiecutter / cookiecutter-django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
https://cookiecutter-django.readthedocs.io
BSD 3-Clause "New" or "Revised" License
12.17k stars 2.91k forks source link

Support docker compose and dockerfile for vscode debug mode. #5451

Open quroom opened 1 month ago

quroom commented 1 month ago

Description

What are you proposing? How should it be implemented?

Create diffrent docker compose and django dockerfile for vscode debug mode with debugpy like official docker compose debug setting It would be slightly different. But I am not sure adding different files are best way for vscode debugger preset. However I am sure it's helpful who faces same issue in wsl or another os.

Rationale

Why should this feature be implemented?

2540 ,

SaboorNisha commented 1 month ago

Creating separate Docker Compose and Django Dockerfile configurations specifically for VS Code debug mode using debugpy sounds like a practical approach. Having these settings would indeed help developers who use different environments like WSL or other operating systems.

However, instead of adding entirely new files, maybe we could explore adding conditional settings or comments within the existing Docker Compose and Dockerfile to keep things more organized. That way, we could support the VS Code debugger setup without cluttering the project with multiple files. What do you think?

quroom commented 1 month ago

Creating separate Docker Compose and Django Dockerfile configurations specifically for VS Code debug mode using debugpy sounds like a practical approach. Having these settings would indeed help developers who use different environments like WSL or other operating systems.

However, instead of adding entirely new files, maybe we could explore adding conditional settings or comments within the existing Docker Compose and Dockerfile to keep things more organized. That way, we could support the VS Code debugger setup without cluttering the project with multiple files. What do you think?

Thanks for great insight!

As you said, If we can add conditional settings within the Docker Compose and Dockerfile, It's best. I guess It might be possible by ENV setting like ENV DEBUGPY_ON or something with True / False or 1 / 0 value. It would install debugpy package in django container contionally by DEBUGPY_ON value, also run django app server with debugpy. But I am not sure I didn't try it yet. However it sounds like it's possible. And we need to add some documents for this.

Your second suggestion(working with comments) is not great.. It makes user confusing first using cookiecutter-django. If it's experimental function , It would be OK. but production level comment / comment out for logic is not great option. If there would be more options like comment/comment out, the project code would be ugly.

I hope your opinion again :)

quroom commented 1 month ago

I tried conditional settings for vscode remote debug with docker. Simply, I just added DEBUGPY_ON arg to docker compose file, Ofcourse should open debugpy port(like 5678)

docker-compose.local.yml

services:
  django: &django
    build:
      args:
        - DEBUGPY_ON=${DEBUGPY_ON:-0}
...
    ports:
        - '8000:8000'
        - '5678:5678'
...

And we can easily turn on by command DBUGPY_ON=1 docker compose ...

compose/local/django/start
...
if [ $DEBUGPY_ON == 0 ]; then
    exec python manage.py runserver_plus 0.0.0.0:8000
else
    python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000
fi
compose/local/django/dockerfile
...
# set env for runtime
ENV DEBUGPY_ON=${DEBUGPY_ON}

# conditional installation of debugpy
RUN if [ "$DEBUGPY_ON" = "1" ]; then \
  pip install --no-cache-dir debugpy -t /tmp; \
  fi
...
.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Remote Attach",
      "type": "debugpy",
      "request": "attach",
      "django": true,
      "connect": {
        "host": "localhost",
        "port": 5678
      },
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}",
          "remoteRoot": "/app"
        }
      ]
    }
  ]
}

And then that vaule will determine whether remote debug mode or not in dockerfile and start script. Becuase we need to install debugpy and also runserver with debugpy. And after running django container, we can attach remote debug by vscode python remote debugger.

It works well in my local machin(wsl ubuntu 22.04 and vscode remote debugger) So I would like to make PR.

I guess PR should include code changes to create docker-compose / dockerfile / start script, also document for how to use.

How do you think this idea? I just would like to confirm so I mention, If I distrub you , I am sorry :( @browniebroke

1beb commented 1 month ago

@quroom would love to see how you implemented this. In particular the launch.json file.

quroom commented 1 month ago

@quroom would love to see how you implemented this. In particular the launch.json file.

@1beb This is what you asked. Also I updated previous comment.

launch.json is almost same as default.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Remote Attach",
      "type": "debugpy",
      "request": "attach",
      "django": true,
      "connect": {
        "host": "localhost",
        "port": 5678
      },
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}",
          "remoteRoot": "/app"
        }
      ]
    }
  ]
}