Docker Community Forums

Share and learn in the Docker community.

Django Vscode debugging

Hello. I’ve been sitting for 2 days trying to get the answer - nothing helped. Can anyone explain me, how to force Visual Studio Code to debugg django docker app? Tried to use basic configuration (add docker files to workspace), but it doesnt work. When im running my app, docker can see my .env file to fill all hidden variables (for example secret_key), but when tried to to run debbuger, it seems to not use enviroment variables - django getting the error: secret key cant be empty. When i tried to put clear text as a secret key, django saying that it got problem with database connection. As i mentioned, my app works fine when using docker-compose run. Can anyone help me configure it? To be honest, i dont know what piece of code should i put here (used the default docker-compose.debug.yaml, launch.json and tasks.json).

I don’t know who is familiar with django on this forum. I only used it once years ago when Docker was probably not even an idea. Not to mention vscode.

I assume you searched for a solution on the internet. Can you share what you found? I would search for “vscode django debug docker”

Since not everyone who can be a potential help knows what the defaults are, they should try to find it or recreate your environment. You can get more help if you share everything you know. If you don’t want to insert code, share a link for example.

Thank you for reply. About django, i think its not problem connected wth it, becouse as i said, my container works fine. It doesnt seems to use env file with debugger mode.

If we are talking about my research, i found only tutorial on VisualStudio website, but it is not tutorial with every cases (like using .env file).

You asked me about code, so im sending ones. I hope someone could help me, becouse at this point i cant see why docker is so good, if you cant even debug apps :confused:

docker-compose.debug.yml

version: '3.4'

services:
  electronicshop:
    image: electronicshop
    build:
      context: .
      dockerfile: ./Dockerfile,
    command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"]
    ports:
      - 8000:8000
      - 5678:5678

docker-compose.yml

version: '3.4'

services:
  electronicshop:
    image: electronicshop
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - ./electronic_shop:/electronic_shop
    command: >
      sh -c "python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    env_file:
      - .env
    environment:
      - DB_HOST=db
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=&{DB_PASSWORD}
      - DEBUG=1
    depends_on:
      - db
    restart: always

  db:
    image: postgres:14.1-alpine
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    ports:
      - 5454:5454/tcp

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}/electronic_shop",
                        "remoteRoot": "/electronic_shop"
                    }
                ],
                "projectType": "django"
            },
            "envFile": "${workspaceFolder}/electronic_shop/electronic_shop/.env"
        }
    ]

}

tasks.json

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

If this post is not connected directly with Docker, then sorry for spam.