本文介绍了在 VS Code 中调试在 Docker 中运行的 Node Typescript 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Docker 中运行一个 Node 应用程序,使用 docker-compose.我使用 Traefik 作为代理.我希望能够在 VS Code 中调试它,但我无法连接到我的应用程序:

I'm running a Node application in Docker, with docker-compose. I'm using Traefik as a proxy.I would like to be able to debug it in VS Code but I don't manage to connect to my app:

connect ECONNREFUSED 127.0.0.1:9229

这是我的文件:

docker-compose.yml:

docker-compose.yml:

version: '3'

services:
    traefik:
        image: traefik:1.7
        command: --docker --docker.exposedbydefault=false
        ports:
            - '80:80'
            - 9229:9229
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock

    core:
        image: node:alpine
        labels:
            - traefik.enable=true
            - traefik.port=4001
            - traefik.backend=core
            - traefik.frontend.rule=Host:core.localhost
        volumes:
            - ./leav_core:/app
        working_dir: /app
        command: [sh, -c, 'npm start']
        expose:
            - '9229'

volumes:
    arango_data:
        driver: local

npm start实际执行的命令是:

ts-node --inspect=0.0.0.0:9229 --type-check src/`

VSCode 中的调试设置:

The debug settings in VSCode:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Attach to Node",
            "type": "node",
            "request": "attach",
            "remoteRoot": "/app"
        }
    ]
}

我使用 Traefik http://core.localhost 上定义的 URL 访问我的应用程序,但我不知道如何将调试器附加到它

I access to my application with the URL defined on Traefik http://core.localhost but I don't know how to attach the debugger to it

谢谢!

推荐答案

我的方法并不好,因为 VS Code 中有一个很棒的工具叫做远程开发".这是一个扩展,允许您直接在 VS Code 中附加容器.

My approach was not good as there is a great tool in VS Code called "Remote development". It's an extension that allows you to attach a container directly in VS Code.

首先,我必须更改启动节点应用程序的方式以启用检查.由于 ts-node 不支持 inspect 选项,你必须使用这个:

First, I had to change the way I start my node app to enable inspecting. As ts-node is not supporting the inspect option, you have to use this:

node --inspect=0.0.0.0:9229 -r ts-node/register src/

然后,使用远程开发进入您的容器.进入内部后,您可以像通常在经典"节点环境中所做的那样调试您的应用程序.Personnaly,我在 launch.json 中使用了这些设置:

Then, use Remote Development to get into your container.Once you're inside, you can debug your app as you would normally do in a "classic" node environment. Personnaly, I used these settings in launch.json:

{
    "type": "node",
    "request": "attach",
    "name": "Attach",
    "port": 9229,
    "skipFiles": [
        "<node_internals>/**",
        "node_modules/**"
    ]
}

一切正常,我的断点被正确命中并且可以有效地调试我的应用程序:)

Everything works fine, my breakpoints are hit appropriately and can be debug my app efficiently :)

这篇关于在 VS Code 中调试在 Docker 中运行的 Node Typescript 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:44