Search code examples
node.jstypescriptdockervscode-debuggernode-debugger

Node vs-code debugger is showing unbound breakpoint in Docker


After much exploration I made the ts debugger work for specific port and also need to compile the .ts while doing live reload using nodemon. I was getting the same unbound breakpoint when I ran app without docker, but after changing the launch file it worked.

However in the docker the debugger is getting attached but with unbound breakpoint. What changes needs to be done for the launch file for debugger in "name": "my-service-docker",

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "my-service",
            "type": "node",
            "request": "attach",
            "port": 9229,
            "restart": true,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "${workspaceFolder}",
            "sourceMaps": true
        },
        {
            "name": "my-service-docker",
            "type": "node",
            "request": "attach",
            "port": 9230,
            "restart": true,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "${workspaceFolder}",
            "sourceMaps": true
        }
    ]
}

docker-compose.yml

# YML version
version: '3.8'

# Define all the services our book application needs
services:
  my-service :  # node application
    container_name: my-backend-container
    restart: always # automatically restart if the app fails
    build: 
      context: .
      dockerfile: Dockerfile
    image: username/my-service
    env_file:
      - .env
    depends_on:
      - mongo
    ports: 
      - 3000:3000
      - 9230:9229 
    volumes:
      - ./:/usr/src/app
      - exclude:/usr/src/app/node_modules/
  mongo: # database
    expose:
      - "4000"
    image: mongo:5.0.6 # pull the mongo image from docker hub
    logging: 
      driver: none # disable logging
    ports:
      - '27018:27017'
volumes:
  exclude:

nodemon.json

{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [".git", "node_modules/**/node_modules"],
  "execMap": {
    "ts": "node --require ts-node/register -r tsconfig-paths/register"
  },
  "exec": "npx tsc&&node --inspect=0.0.0.0:9229  build/index.js"
}

package.json

...
///
///
 "scripts": {
  "start:dev": "NODE_ENV=development&&nodemon"
}

...

DockerFile

FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
ENV PATH=/usr/src/app/node_modules/.bin:$PATH
# If you are building your code for production
# RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD [ "npm", "run", "start:dev" ]

Solution

  • For the docker, we need to set the right path for remoteRoot: which was /usr/src/app

     {
                "name": "my-service-docker",
                "type": "node",
                "request": "attach",
                "port": 9230,
                "restart": true,
                "sourceMaps": true,
                "address": "localhost",
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/usr/src/app",
                "protocol": "inspector"
            }