3

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!

Tdy
  • 863
  • 12
  • 28
  • Are you sure `expose` inside the docker-compose file should be declare like that ? https://docs.docker.com/compose/compose-file/#expose – Jeremy M. Nov 19 '19 at 14:01
  • I changed it according to the doc, but not more success – Tdy Nov 19 '19 at 14:11
  • You'r writing ports: `80:80`, `9229:9229` but this is used for the expose as the left port is the exposed port and the right port is the port you wan't your redirection to point. You should try to use only ports: `9229:YourAppPort` and remove `expose` – Jeremy M. Nov 19 '19 at 14:16
  • I'm getting closer. I managed to attach the debugger to my app by exposing the 9229 right in the node container (and not the reverser proxy) and change my command to start node with inspect enabled. Now I have troubles to get my breakpoints hit, but that's another problem – Tdy Dec 04 '19 at 14:36

1 Answers1

3

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

Tdy
  • 863
  • 12
  • 28