首页 > 解决方案 > Visual Studio Code 使用 python3 在 docker 容器中调试 python 2.7 应用程序

问题描述

我正在尝试在 docker 容器中调试我的应用程序。该应用程序是用 paython 2.7 编写的,但 VS Code 尝试使用 python 3 对其进行调试。因此,它无法解析包并抛出异常。

任何想法?

我的配置:

任务.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"
          }
        }
      ]
    }

启动.json

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

设置.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"]

标签: pythondockervisual-studio-code

解决方案


经过一番调查,我找到了解决方案:

我修改lunch.json为以下内容:

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

然后我将以下行添加到实体 Python 文件中:

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

从 VS 代码 IDE 运行 docker 容器后,我可以在调试模式下运行。


推荐阅读