在 Visual Studio Code 中为 dockerized 节点进程设置了断点但尚未绑定

Sta*_*ers 2 javascript debugging node.js docker visual-studio-code

我正在尝试将 macOS Catalina 上的 Visual Studio Code 中的调试器用于节点应用程序。我创建了一个非常简单的例子来说明我的情况。

索引.js

require('http').createServer( (request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.end('Hello World\n');
  response.end();
}).listen(3000);
Run Code Online (Sandbox Code Playgroud)

文件

FROM node:12.14.0-alpine 
COPY . /src
CMD ["node","--inspect=0.0.0.0", "src/index.js"]
Run Code Online (Sandbox Code Playgroud)

我构建了 Dockerfile

docker build . -t debugtest
Run Code Online (Sandbox Code Playgroud)

然后运行它

 docker run -p 3000:3000 -p 9229:9229 debugtest
Run Code Online (Sandbox Code Playgroud)

我可以访问http://localhost:3000/

我在 index.js 中设置了一个断点。

在此处输入图片说明 然后在 Visual Studio 代码中设置调试器目标

{
  "version": "0.2.0",
  "configurations": [
  {
    "type": "node",
    "request": "attach",
    "name": "Attach",
    "port": 9229,
    "skipFiles": [
      "<node_internals>/**"
    ]
  }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在,当我以可视化代码启动调试器时,断点的红点消失,并出现“断点设置但尚未绑定”。

在此处输入图片说明

我在各个地方都看到过这个问题,但没有一个解决方案有效。当我在 docker 之外运行节点进程时,node --inspect index.js它可以顺利运行。

我使用的是 1.41.1 版的 Visual Studio 代码和 docker 2.1.0.5。

Sta*_*ers 6

原来我省略remoteRoot的属性launch.json。下面的作品。

启动文件

{
  "version": "0.2.0",
  "configurations": [
  {
    "type": "node",
    "request": "attach",
    "name": "Attach",        
    "port": 9229,
    "remoteRoot": "/src/",
    "skipFiles": [
      "<node_internals>/**"
    ]
  }
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • 这救了我的命!我使用了“默认”`launch.json`,即当您选择“附加到 Docker”时创建的那个。它有一个“remoteRoot”属性,但它指向“/usr/src/app”,这不是我将应用程序代码放入容器中的位置,因此只需将其更改为我的代码在容器中所在的位置即可解决此问题。 (2认同)
  • 也救了我,所以我也不知道“remoteRoot”是我从“Dockerfile”获得的容器内应用程序代码的路径 (2认同)