I've just recently begun trying to wrap my head around Docker and have managed to get a development machine up and running. What i'm now trying to do is to be able to use the debugger in Visual Studio Code in my python application (specifically Django).
I've tried following the limited documentation of the python extension for VS Code which explains the parameters for remote debugging.
FROM python:3.5.2
RUN apt-get update \
--no-install-recommends && rm -rf /var/lib/apt/lists/* \
&& mkdir -p /code \
EXPOSE 8000
WORKDIR /code
COPY requirements.txt /code
RUN /bin/bash --login -c "pip install -r requirements.txt"
ADD . /code
CMD []
version: '2'
services:
db:
image: postgres
web:
build: .
volumes:
- .:/code
ports:
- "8000:8000"
command: bash -c "./wait-for-it.sh db:5432 && python manage.py migrate && python manage.py runserver 0.0.0.0:8000 --noreload"
depends_on:
- db
{
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"localRoot": "${workspaceRoot}",
"remoteRoot": "/code",
"port": 8000,
"secret": "debug_secret",
"host": "localhost"
}
I've also added the line ptvsd.enable_attach("debug_secret", address = ('0.0.0.0', 8000))
to one of the project files
When ever I start the debugger nothing happens and it looks like VS Code is waiting for a breakpoint to hit. But it never does.
Any ideas?
I have tried using different ports for the debugger aswell as exposing the new ports in docker-compose.yml
without any success. It looks like the attach is successfull because the debugger doesn't crash but no breakpoint is triggered. I'm really stuck on this one.
See answer from theBarkman. I'll add that I was unable to use a secret to get this working. I did the following:
manage.py
import ptvsd
ptvsd.enable_attach(secret=None, address=('0.0.0.0', '3000'))
launch.json
{
"name": "Attach Vagrant",
"type": "python",
"request": "attach",
"localRoot": "${workspaceRoot}",
"remoteRoot": "/code",
"port": 3000,
"secret": "",
"host":"localhost"
}
I've had the most success remote debugging dockerized Django projects by throwing the ptvsd code into my manage.py file and turning off Django's live code reload.
Since Django essentially spins up 2 servers when you runserver
(one for that live code reloading, and the other for the actual app server`, ptvsd seems to get really confused which server it should watch. I could sort of get it to work by waiting for attachment, try/excepting the enable_attach method or breaking into the debugger - but breakpoints would never work, and I could only seem to debug a single file at a time.
If you use the django flag --noreload
when spinning up the server, you can throw the ptvsd inside the manage.py file without all the waiting / breaking into the debugger nonsense, and enjoy a much more robust debugging experience.
manage.py:
import ptvsd
ptvsd.enable_attach(secret='mah_secret', address=('0.0.0.0', 3000))
run teh server:
python manage.py runserver 0.0.0.0:8000 --noreload
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With