In order to enable debugging for your Django app running in a Docker container, follow these steps using Visual Studio (Code):
- Install ptvsd:
pip install ptvsd
- 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).
- To your manage.py, add this:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings") # This already exists
import ptvsd
from your_project import settings
if settings.DEBUG:
import ptvsd
ptvsd.enable_attach(address = ('0.0.0.0', 3000))
execute_from_command_line(sys.argv) # This already exists
-
Be sure to open port 3000 in your
docker
command ordocker-compose.yml
-
Run your app:
python manage.py runserver --noreload --nothreading 0.0.0.0:8000
The --noreload and --nothreading options are necessary for ptvsd, so at this point debugging with live reload is not possible with this method. See also this thread
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"