1

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?

2
  • You may want to take a look at custom tasks: code.visualstudio.com/docs/editor/tasks#_custom-tasks and add a custom task that runs docker-compose as a "preLaunchTask" in your attach config. This, however, will pose other problems. You can either run docker-compose in the background. In this case vscode will try to attach to a process that's not running yet. Or, you can run it in foreground and then it won't get to the debugger unless you exit the process. I don't have a solution for that (yet). Commented Jul 26, 2017 at 15:31
  • Check the answer I got today stackoverflow.com/questions/46500639/… Commented Sep 30, 2017 at 14:23

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.