Tdy
Tdy

Reputation: 1013

Debug in VS Code a Node Typescript app running in Docker

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

Here are my files:

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

The command actually executed by npm start is:

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

The debug settings in VSCode:

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

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

Thanks!

Upvotes: 3

Views: 2892

Answers (1)

Tdy
Tdy

Reputation: 1013

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.

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/

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 :)

Upvotes: 3

Related Questions