How to hit Vscode breakpoints in unit tests from within a docker-compose setup running Django

Viewed 855

What I'm trying to do seems simple enough, but it's been crazy hard to actually get there. I have a Django application that runs in a docker-compose environment, and I want to run and debug the unit tests with breakpoints in Vscode.

Since this is a big project with a team that doesn't necessarily use vscode, I can't add libraries willy-nilly (like ptvsd, for example). I'm hoping there's a magic configuration for tasks.json and launch.json that will makes things work.

I have a container for a postgres database, one for django, and one for redis, all defined in docker-compose.yml. There's a command that I can run in the terminal that will run the tests, it's:

docker-compose run --rm app python manage.py test 

where app is the django app container. I'd love to be able to run this command in such a way that it can hit breakpoints in vscode.

My incomplete stab at the launch.json file looks like this:

{
  "configurations": [{
    "name": "Docker: Python - Django",
    "type": "docker",
    "request": "launch",
    "preLaunchTask": "compose-for-debug",
    "python": {
      "pathMappings": [{
        "localRoot": "${workspaceFolder}",
        "remoteRoot": "/app"
      }],
      "projectType": "django"
    }
  }]
}

And my tasks.json:

{
  "version": "2.0.0",
  "tasks": [{
      "type": "docker-build",
      "label": "docker-build",
      "platform": "python",
      "dockerBuild": {
        "tag": "dockerdebugging:latest",
        "dockerfile": "${workspaceFolder}/Dockerfile",
        "context": "${workspaceFolder}",
        "pull": true
      }
    },
    {
      "type": "docker-run",
      "label": "docker-run: debug",
      "dependsOn": [
        "docker-build"
      ],
      "python": {
        "args": [
          "test",
          "--nothreading",
          "--noreload"
        ],
        "file": "manage.py"
      }
    }
  ]
}

I think I need to convert the build task to a docker compose task somehow, but I just can't figure out how its done. It may also make sense to run the containers in the terminal, and somehow make vscode attach to them with breakpoints enabled.

Even some help with how to approach this would be great. I know it's a tricky one.

0 Answers
Related