Gabriel L.
Gabriel L.

Reputation: 56

Debugging NodeJs Program in Docker Container with VSCode in one step

I'm trying to setup my VSCode environment so I can debug my dockerized node.js program in one single step by hitting F5.

Currently my setup is the following:

.vscode/launch.json:

{
  "version": "0.1.0",
  "configurations": [
    {
      "name": "Attach",
      "type": "node",
      "protocol":"inspector",
      "request": "attach",
      "port": 5858,
      "restart": false,
      "sourceMaps": false,
      "localRoot": "${workspaceRoot}/",
      "remoteRoot": "/usr/local/src/my-app"
    }
  ]
}

docker-compose.debug.yml:

version: "3"

services:
  app:
    build: .
    ports:
      - "3000:3000"
      - "5858:5858"
    entrypoint: node --inspect-brk=0.0.0.0:5858 app/entry.js
    networks:
      - appnet

networks:
  appnet:

Now this works w/o any problem when I execute docker-compose -f ./docker-compose.debug.yml up --build in an external terminal, and then run the "Attach" configuration in VSCode.

However I can't find a way to run docker-compose, before attaching to the remote (docker) process from within VSCode. The goal is to be able to just hit F5 and have VSCode launch docker-compose, and automatically attach itself to it.

I've tried calling the docker-compose by using the "Launch via NPM" VSCode configuration and adding

"docker-debug" : "docker-compose -f ./docker-compose.debug.yml up --build"

to my package.json scripts section.

But that only partially works as the debugger seems to ignore the remoteRoot attribute of the config and hence, is completely useless for debugging my program (e.g.: it doesn't accept breakpoints, and the only files it knows how to debug are nodes.js internals...)

Any idea of how I could solve this?

Upvotes: 0

Views: 1696

Answers (1)

Hossein Alipour
Hossein Alipour

Reputation: 705

this is works for me, In your launch.json:

{
  "name": "Debug Jest",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"],
  "address": "127.0.0.1",
  "port": 9230,
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/usr/src/app/server"   # path to your nodejs workspace in docker
},

package.json you run your service:

 "scripts": {
    "debug": "docker-compose -p dev -f docker-compose-dev.yml up jestdebug"
  },

and in docker-compose-dev.yml:

version: '3.4'
services:
  jestdebug:
    image: node:10.15.3-alpine
    working_dir: /usr/src/app/server
    command: node --inspect-brk=0.0.0.0:9230 node_modules/.bin/jest --runInBand ${jestdebug_args}
    volumes:
      - nodemodules:/usr/src/app/server/node_modules
      - ../server:/usr/src/app/server
    ports:
      - '9230:9230' # for debuging
    networks:
      - backend
    depends_on:
      - nodejs
    tty: true
# ...other services

Upvotes: 1

Related Questions