Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code debugs python 2.7 app in a docker container with python3

I'm trying to debug my app in a docker container. The app is written in paython 2.7 but VS Code tries to debug it with python 3. Therfore, it cannot resolve packages and throws execptions.

Any idea?

My configurataions:

tasks.json

    {
      "version": "2.0.0",
      "tasks": [
        {
          "type": "docker-run",
          "label": "docker-run: debug",
          "dependsOn": ["docker-build"],
          "dockerRun": {
            "containerName": "account",
            "image": "account:latest",
            "env": {},
            "volumes": [
              {
                "containerPath": "/code",
                "localPath": "${workspaceFolder}"
              }
            ],
            "ports": [
              {
                "containerPort": 8086,
                "hostPort": 8086
              },
              {
                "containerPort": 8085,
                "hostPort": 8085
              }
            ]
          },
          "python": {
            "args": ["--config=/code/config/account.conf.localstage"],
            "file": "skoobe-accountd"
          }
        },
        {
          "label": "docker-build",
          "type": "docker-build",
          "dockerBuild": {
              "context": "${workspaceFolder}",
              "dockerfile": "${workspaceFolder}/Dockerfile.arm.dev",
              "tag": "account:latest"
          }
        }
      ]
    }

launch.json

    {
      "configurations": [
        {
          "name": "Debug Account",
          "type": "docker",
          "request": "launch",
          "preLaunchTask": "docker-run: debug",
          "python": {
            "pathMappings": [
              {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/code"
              }
            ],
            "projectType": "general"
          }
        }
      ]
    }

settings.json

    {
        "python.linting.pylintEnabled": true,
        "python.pythonPath": "/usr/bin/python"
    }

Dockerfile

    FROM python:2.7.18

    ENV AWS_ACCESS_KEY_ID="xxx"
    ENV AWS_SECRET_ACCESS_KEY="xxx"
    ENV AWS_DEFAULT_REGION="xxx"
    ENV SQS_QUEUE="xxx"
    ENV SQS_CHANGE_QUEUE="xxx"


    # RUN mkdir -p /root/.ssh && \
    #    chmod 0700 /root/.ssh


    # RUN apt-get update && \
    #    apt-get install openssh-server -y

    WORKDIR /code/
    ADD requirements.txt /code/
    RUN pip install -r requirements.txt

    ADD . /code/

    WORKDIR tools/
    RUN python setup.py install
    WORKDIR /code/
    # RUN pip install -r requirements.txt

    CMD ["python", "accountd", "--config=/code/config/account.conf.localstage"]
like image 956
Super Hornet Avatar asked Sep 14 '26 05:09

Super Hornet


1 Answers

I found a solution after some investigation:

I modfied lunch.json to the following:

    {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/code"
                }
            ]
          }
        ]
    }

Then I added the following lines into the entypoint Python file:

import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))

After running the docker container, from VS code IDE, I could run in debug mode.

like image 101
Super Hornet Avatar answered Sep 15 '26 18:09

Super Hornet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!