Debugging django project in docker-compose in VS code

I cannot tune debugging django project from docker-compose in VS code.

Docker-compose:

version: '3'

services:
  helpdesk_web:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: helpdesk_web
    volumes:
      - ./static:/usr/src/app/static
      - media:/usr/src/app/media
    ports:
      - "8000:8000"
      - "5678:5678"
    env_file:
      - ./.env
    restart: always
    depends_on:
      - helpdesk_db

  helpdesk_db:
    image: postgres
    container_name: helpdesk_db
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: something
      POSTGRES_PASSWORD: something
      POSTGRES_USER: something

  nginx:
    build:
      context: ./docker/nginx
      dockerfile: Dockerfile
    container_name: helpdesk_nginx
    restart: on-failure
    depends_on:
      - helpdesk_web
      - helpdesk_db
    ports:
      - "80:80"
    volumes:
      - ./static:/usr/src/app/static
      - media:/usr/src/app/media

volumes:
  postgres_data:
  media:

DockerFile:

FROM python:3.10

# Задаем рабочую директорию(app) внутри контейнера
WORKDIR /usr/src/app

# Запрещаем Python писать файлы .pyc на диск
ENV PYTHONDONTWRITEBYTECODE 1
# Запрещает Python буферизовать stdout и stderr
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip

# Копируем файл из рабочей директории на диске в рабочую директорию(/usr/src/app) контейнера
COPY requirements.txt .

RUN pip install -r requirements.txt

# Копирует все файлы из рабочей директории на диске в рабочую директорию контейнера
COPY . .

RUN chmod +x entrypoint.sh

ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

launch.json:

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Django DEBUG",
            "type": "python",
            "request": "attach",
            "pathMappings": [
              {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/usr/src/app"
              }
            ],
            "port": 5678,
            "host": "0.0.0.0",
          },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "runserver",
            ],
            "django": true,
            "justMyCode": true
        }
    ]
}

manage.py

import os
import sys
from django.conf import settings

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'helpdesk.settings')
    
    if settings.DEBUG: 
        import debugpy
        debugpy.listen(('0.0.0.0', 5678))
        print('Attached!')
    
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

Actually I followed https://testdriven.io/blog/django-debugging-vs-code/ this article in my another project and everything works. But with that docker-compose it doesn't work. Are there any idea why it doesn't work?

Back to Top