How to specify the port of vscode debugger on a Django docker container?

I have a Django application inside a Docker container. I've configured the launch.json and tasks.json files to run my docker-compose file when the debugger runs:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
            "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}/src",
                        "remoteRoot": "/app/src"
                    }
                ],
             "projectType": "django",
             "port": 8000,
            }
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "vscodedjangodocker: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"
            }
        }
    ]
}

docker-compose.yaml

version: '3.8'  # Specify the version at the top
services:
  backend:
    build: 
      context: .
      dockerfile: ./Dockerfile
    container_name: backend
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
    volumes:
      - .:/app

Dockerfile

FROM python:3.12-bullseye

WORKDIR /app/src/

COPY ./src/requirements.txt .

RUN pip install --prefer-binary -r requirements.txt

COPY ./src /app/src

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

The issue I'm facing is that I want the debugger session to run on port 8000 on my local machine, but each time I run the debugger the port that opens is 5500X (i.e. 55001, 55002, etc.). For example, when I click on start debugging button on vscode the container that builds up runs on port 55012:8000.

When I run only docker compose with docker-compose up ---build command it works as expected and the server is running on port 8000 of my machine but without the debugger attached.

How can I specify VSCode debugger that I want my session on port 8000?

I think you have to add "port": 8000 to the configurations key like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                        {
                            "localRoot": "${workspaceFolder}/src",
                            "remoteRoot": "/app/src"
                        }
                 ],
                 "projectType": "django",
                 "port": 8000,
            },
            "port": 8000
        }
    ]
}
Вернуться на верх