microsoft / vscode-python-debugger

Python debugger (debugpy) extension for VS Code.
https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy
MIT License
41 stars 17 forks source link

"That port is already in use" when debugging a Django dev container #224

Closed bhrutledge closed 3 weeks ago

bhrutledge commented 4 months ago

I'm new to Dev Containers, so it's very possible I'm missing something.

I've created a devcontainer.json for running the Django service defined in my existing docker-compose.yml. That's working fine; I can reopen in the container and use my app. I've also added a launch.json using the standard "Django" debugging configuration. Expand the "Details" below to see those files.

When I run "Start Debugging", I get the following output in the terminal:

app@0d6fa73d0cc7:~$  cd /home/app ; /usr/bin/env /home/app/venv/bin/python3 /home/app/.vscode-server/extensions/ms-python.debugpy-2024.0.0-linux-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher 57017 -- /home/app/manage.py runserver 8001 
Performing system checks...

System check identified no issues (0 silenced).
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.
Error: That port is already in use.

I'm guessing this is because manage.py is already running on port 8001, which makes sense. However, I can't find any documentation for how to start (or restart) the process (or container) in "debug mode" instead.

In `docker-compose.yml`: ```yml version: "4.0" services: django: build: context: . command: python manage.py runserver 0.0.0.0:8001 volumes: - .:/home/app ``` In `.devcontainer/django/devcontainer.json`: ```json { "name": "Django container", "dockerComposeFile": [ "../../docker-compose.yml" ], "service": "django", "workspaceFolder": "/home/app", "shutdownAction": "none", "postStartCommand": "git config --global --add safe.directory ${containerWorkspaceFolder}", "customizations": { "vscode": { "extensions": [ "ms-python.python", "ms-python.debugpy", "ms-python.vscode-pylance", "ms-python.isort", "ms-python.black-formatter", "batisteo.vscode-django" ] } } } ``` In `.vscode/launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "Django", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/manage.py", "args": [ "runserver", "8001" ], "django": true }, ] } ```
ridoo commented 2 months ago

You may want to override the default command/entrypoint, e.g. with sleep infinity. This would make the container do nothing when dropping you into the started container. Then you can start debugging using the vscode laucher.

paulacamargo25 commented 3 weeks ago

Thanks for your reply @ridoo, ill close the issue for now.