如何将VS代码附加到在docker容器中运行的节点进程 [英] How can I attach VS Code to a node process running in a docker container

查看:1036
本文介绍了如何将VS代码附加到在docker容器中运行的节点进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Visual Studio代码调试器附加到Docker容器中运行的node.js应用程序。

I'm trying to attach the Visual Studio Code debugger to a node.js app that is running inside a Docker container.

我启动应用程序,如: / p>

I start the app like:

node --debug-brk app.js

我在 docker-compose.yml 中公开调试器端口:

I expose the debugger port in docker-compose.yml:

app:
  build: .
  working_dir: /code
  volumes:
    - .:/code
  command: npm run debug
  ports:
    - "3004:3000"
    - "5858:5858"

我的 launch.json 看起来像:

{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Attach",
            "type": "node",
            "address": "localhost",
            "port": 5858
        }
    ]
}

当我启动应用程序并附加调试器时,这将正确连接(我可以看到在调试器UI中闪烁的值已经),但是会停止,告诉我以下内容:

Now, when I start the application and attach the debugger this will correctly connect (I can see the values flashing in the debugger UI already), but then it will stop, telling me the following:

Error opening 'app.js' (File not found: /code/app.js).

这是因为docker不会将该应用程序挂载在根目录中,而是以 / code (参见 docker-compose.yml 中的)和VS代码被突然的偏移困惑。

This is due to the fact that docker will not mount the app in root but in /code (see volumes in docker-compose.yml) and VS code is confused by the sudden offset.

当我在容器外部运行应用程序(即本地,没有偏移量)时,它的工作原理与预期的一样,我可以按预期使用调试器。

When I run the application outside the container (i.e. locally, without offset) it works just as expected and I can use the debugger as expected.

启动配置似乎有一个 cwd 选项,但我不确定是否有任何差异在我的情况下。

There seems to be a cwd option for the launch configuration but I am not sure if that makes any difference in my case.

我可以修复这个路径偏移吗?我在这里缺少其他的东西?

Can I fix this path offset? Am I missing something else here?

推荐答案

此功能现已由VSCode正式支持: https://github.com/Microsoft/vscode-node-debug/issues/8

This feature is now officially supported by VSCode: https://github.com/Microsoft/vscode-node-debug/issues/8

这篇关于如何将VS代码附加到在docker容器中运行的节点进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆