Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
quroom opened this issue Oct 11, 2024 · 6 comments
Open

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

quroom opened this issue Oct 11, 2024 · 6 comments

Comments

@quroom
Copy link
Contributor

quroom commented Oct 11, 2024

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 ,

@quroom quroom changed the title Support debug mode docker compose and dockerfile for vscode debug mode. Support docker compose and dockerfile for vscode debug mode. Oct 11, 2024
@SaboorNisha
Copy link

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
Copy link
Contributor Author

quroom commented Oct 14, 2024

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
Copy link
Contributor Author

quroom commented Oct 15, 2024

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
Copy link

1beb commented Oct 20, 2024

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

@quroom
Copy link
Contributor Author

quroom commented Oct 21, 2024

@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"
        }
      ]
    }
  ]
}

@SaboorNisha
Copy link

This approach seems very practical! By using the DEBUGPY_ON argument and conditional settings, it provides a clean way to toggle the VS Code debugger functionality without introducing extra files. I like how you've made it easy to switch between debug and regular modes via the environment variable. The use of conditional installation of debugpy and the custom start script ensures that it’s only added when needed, keeping things lightweight.

Including documentation on how to use the setup is a great idea, as it will make it easy for others to integrate this into their workflows. I think your idea for a PR is solid—adding the necessary Docker Compose, Dockerfile, and start script changes along with the usage instructions will definitely be helpful to others. Looking forward to seeing the PR!
quroom
No worries about disturbing anyone, this is a helpful discussion! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@1beb @quroom @SaboorNisha and others