3

Im trying to debug my Django process from vs code. But I am not able to get it to work. In my manage.py:

import ptvsd
try:
    ptvsd.enable_attach("my_secret", address=('localhost', 3000))
except:
    pass

In my docker-compose:

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
      - "3000:3000"
    depends_on:
      - db

And my debug info in launch.json:

{
  "name": "Attach (Remote Debug)",
  "type": "python",
  "request": "attach",
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/code",
  "port": 3000,
  "secret": "my_secret",
  "host": "localhost"
},

Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
EXPOSE 3000

When starting the debug session I get a message saying: "debug adapter process has terminated unexpectedly". Does anyone have any tips on how to get this working? Im running ptvsd 3.0.0 both on my computer and in the docker container.

3
  • 1
    Is port 3000 exposed by the docker image? Commented Feb 4, 2018 at 19:47
  • Yes it is, Ive added my Dockerfile as well. Commented Feb 4, 2018 at 21:10
  • I'm getting the same error using the most basic configurations taken from the vscode documentation here - donjayamanne.github.io/pythonVSCodeDocs/docs/…. This might actually be a vscode bug, maybe worth opening an issue. Commented Feb 5, 2018 at 4:08

3 Answers 3

2

Three points to check to debug Django in a Docker environment with VSCode:

  • VSCode remote debugging works only with ptvsd==3.0.0 for now (cf. VSCode documentation)

  • With docker-compose, ptvsd needs to be attached to the default route 0.0.0.0 in order to be reached from the host machine (like the Django development server)

  • ptvsd rely on sockets (which can be attached only once to a port) and Django development server reloads the manage.py file after each code changes in the project. So after each code modification, the debug server will fail to attach. To work around this problem, the best solution is to attach the ptvsd debugger in the wsgi.py file, which is only loaded once.

Sign up to request clarification or add additional context in comments.

2 Comments

"Im running ptvsd 3.0.0 both on my computer and in the docker container."
I will try setting up the wsgi.py when I get home, thanks for the tip!
1

In order to enable remote debugging for Django apps in VS Code (e.g. when debugging docker containers), do the following:

  1. Add ptvsd to your requirements.txt file

    ptvsd == 4.3.2

  1. To your launch.json, add this:

    {
          "name": "Remote Django App",
          "type": "python",
          "request": "attach",
          "pathMappings": [
              {
                  "localRoot": "${workspaceFolder}",
                  "remoteRoot": "/remote_root/of/your/app"
              }
          ],
          "port": 3000,
          "host": "localhost"
      }

(Edit the remoteRoot option to reflect your app).

  1. To your manage.py, add this:

    if __name__ == "__main__":                                                    # This already exists
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")  # This already exists

        from django.core.management import execute_from_command_line              # This already exists
        from django.conf import settings

        if settings.DEBUG:
            if os.environ.get('RUN_MAIN') or os.environ.get('WERKZEUG_RUN_MAIN'):
                import ptvsd
                ptvsd.enable_attach(address = ('0.0.0.0', 3000))
                print "Attached remote debugger"

        execute_from_command_line(sys.argv)                                       # This already exists

Note: The third if statement here ensures the debugger does not get attached again after a live reload.

  1. Be sure to open port 3000 in your docker command or docker-compose.yml

  2. Run your app:


    python manage.py runserver 0.0.0.0:8000

Line-by-line debugging

Note: In some (non-Django) cases line-by-line debugging does not work, unless you use double backslashes (\) in your remoteRoot parameter (Viscual Studio Code), even though the remote server runs on Linux. E.g. "remoteRoot": "\\remote_root\\of\\your\\app"

Sources

Source 1
Source 2

Comments

0

Your problem is likely that the webserver is reloading your file and killing your connection. Put a print or log statemet in the start of your settings.py code and see if it is being loaded twice. Then run it with a no-reload flag. ptvsd==3.0.0 does work for as long as it is installed both on host and remote machine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.