You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在VS Code中使用自定义Docker镜像调试Python函数应用的配置疑问

VS Code自定义Docker镜像调试Python函数应用配置问题

我无法掌握在VS Code中使用自定义Docker镜像调试Python函数应用的正确配置方法,操作流程为构建Docker镜像、运行该镜像并启动调试器。目前未找到针对Python函数应用的专属文档,仅能查询到Flask应用的调试示例。

当前配置文件

launch.json

{
    "configurations": [
      
        {
            "name": "Docker: Python - General",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                  {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/home/site/wwwroot"
                  }
                ],
                "projectType": "general"
              }
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "test:latest",
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "platform": "python",
            "dependsOn": [
                "docker-build"
            ],
            "python": {
                "debugPort": 5678,
                "file": "./document-processor-func/__init__.py"
            }
        }
    ]
}

配置调整建议

针对Python函数应用(如Azure Functions)的Docker调试,需要对现有配置做以下修改:

  • 修正launch.json的项目类型
    将projectType从general改为azureFunctions,让VS Code识别为函数应用调试场景:

    "python": {
        "pathMappings": [
          {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/home/site/wwwroot"
          }
        ],
        "projectType": "azureFunctions"
    }
    
  • 完善tasks.json的docker-run配置
    函数应用默认使用7071端口提供服务,需映射调试端口5678,同时调整启动逻辑为函数应用专属启动命令:

    {
        "type": "docker-run",
        "label": "docker-run: debug",
        "platform": "python",
        "dependsOn": ["docker-build"],
        "net": "bridge",
        "ports": [
            { "hostPort": 7071, "containerPort": 7071 },
            { "hostPort": 5678, "containerPort": 5678 }
        ],
        "python": {
            "debugPort": 5678,
            "args": ["host", "--port", "7071"]
        }
    }
    
  • 确保Dockerfile包含调试依赖
    在Dockerfile中添加debugpy安装命令,保证容器内可启动调试器:

    # 安装debugpy用于远程调试
    RUN pip install debugpy
    
  • 验证路径映射一致性
    确认remoteRoot与Dockerfile中函数代码的工作目录一致,若镜像使用了自定义目录,需同步修改pathMappings中的remoteRoot。

内容的提问来源于stack exchange,提问作者Maverick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:53:20