Debugging Django with VS Code inside a Docker container shuts down the container

Viewed 303

I'm following this guide for debugging Django inside a Docker container using VS Code. The only difference I've made was changing ports to 8000 and host to 0.0.0.0 because my docker-compose has it like that. I have no idea why the web container shuts down when I run Django with a breakpoint inside a view because I'm don't really know where the problem is coming from. My only guess is from the vscode/launch.json file that is set up by the following:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Run Django",
      "type": "python",
      "request": "attach",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/app",
          "remoteRoot": "/usr/src/app"
        }
      ],
      "port": 8000,
      "host": "0.0.0.0",
    }
  ]
} 

Any ideas why this is not working or where the problem is coming from?

2 Answers

Are you using Mac or WSL1? I've seen Docker running out of memory on VSCode when using the debugger specially with breakpoints.

Docker virtualization is on OS level, however, to achieve this OS virtualization your machine's Kernel should be the same as your Container's (that's why Windows has WSL2) but MacOS runs on Darwin, not Linux so it needs another layer (LinuxKit VM).

Please check if your Docker settings have this Resources tab. If so, give it some more memory, VSCode debugging is quite heavy in Django.

enter image description here

I ran a debug on port 8002 whilst the docker app ran on 8000, at the same time. Here's how i did it.

With the container up and the app running: find the container and 'Attach visual studio code' create: .vscode/launch.json

{ "version": "0.2.0", "configurations": [

    {
        "name": "Python: Django",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/xxxxxxxxxxxxx/manage.py",
        "args": [
            "runserver",
            "0.0.0.0:8002"
        ],
        "django": true,
        "justMyCode": true
    }
]

} run and debug

Related