在Visual Studio Code中远程调试Docker化的Node.js Azure Function
解决Node.js Azure Function Docker容器远程调试失效问题
现有配置回顾
Dockerfile
# To enable ssh & remote debugging on app service change the base image to the one below FROM mcr.microsoft.com/azure-functions/node:4-node18-appservice # FROM mcr.microsoft.com/azure-functions/node:4-node18 COPY . /home/site/wwwroot RUN npm install -g npm@latest && \ cd /home/site/wwwroot && \ npm install && \ npm run build ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true \ APPSVC_REMOTE_DEBUGGING=TRUE
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Docker NodeJs Attach", "type": "docker", "request": "attach", "processName": "/azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost", "platform": "node", "sourceFileMap": { "/home/site/wwwroot": "${workspaceFolder}" } } ] }
容器启动命令
docker run -p 8080:80 -t report-service-image
问题分析
- 调试端口未映射:appservice镜像默认开启的Node.js调试端口为9229,但启动容器时未将该端口映射到本地,导致调试工具无法连接。
- launch.json配置错误:指定的
processName是.NET WebHost进程,并非Node.js函数的运行进程,调试器无法找到目标进程。 - 调试类型配置冗余:使用
docker类型附加不如直接用node类型配置直观准确,容易出现进程识别问题。
修复步骤
1. 修改容器启动命令,映射调试端口
添加9229端口映射,让本地能访问容器内的调试端口:
docker run -p 8080:80 -p 9229:9229 -t report-service-image
2. 调整launch.json配置
替换为更精准的Node.js调试配置,确保能正确附加到容器内的进程:
{ "version": "0.2.0", "configurations": [ { "name": "Docker NodeJs Attach", "type": "node", "request": "attach", "port": 9229, "address": "localhost", "localRoot": "${workspaceFolder}", "remoteRoot": "/home/site/wwwroot", "skipFiles": [ "<node_internals>/**" ] } ] }
3. 验证容器内调试端口状态
启动容器后,执行以下命令检查9229端口是否被Node进程监听:
docker exec <你的容器ID> netstat -tulpn | grep 9229
如果输出包含node进程监听0.0.0.0:9229,说明调试端口已正常开启。
4. 启动调试
在VS Code中选中修改后的Docker NodeJs Attach配置,点击启动调试按钮,此时断点应能正常触发,远程调试功能生效。
内容的提问来源于stack exchange,提问作者riethmue
相关产品推荐
相关产品推荐

