OzrenTkalcecKrznaric
OzrenTkalcecKrznaric

Reputation: 5646

docker-compose for attaching node.js debugger from VS Code to a node process in WSL docker

I'm really doing the best as I can but I don't know what am I doing wrong.

I have this existing project that can be started and debugged both on Win10 and Linux containers. But I want to be able to attach the debugger in WSL container on Win10 and I'm really loosing my patience now.

So, I have VS Code on Win10 and WSL2 installed. I have Remote-WSL extension (also Remote-Containers, but can't get that to work either, so let's skip that for the time being). And I would prefer to keep Dockerfile as-is, if possible.

When I try to attach to node debugger by running "Docker: Attach to Node" configuration, I get the following error:

Error processing attach: Error: Could not connect to debug target at http://localhost:9229: Promise was canceled
    at e (/home/ozren_admin/.vscode-server/bin/3dd905126b34dcd4de81fa624eb3a8cbe7485f13/extensions/ms-vscode.js-debug/src/extension.js:1:92915)

Here are my files...

/docker-compose.yml

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    env_file: ./myapp.env
    depends_on:
      - db
    volumes:
      - "./app:/src/app"
    ports:
      - "5000:4000"
      - "9229:9229"
    expose:
      - 9229
    links:
      - db:myapp_mobile_apis_db
  db:
    image: mysql:5.7
    container_name: myapp_mobile_apis_db
    restart: always
    environment:
      # see: https://hub.docker.com/_/mysql
      MYSQL_DATABASE: 'myapp'
      MYSQL_USER: 'myapp_user'
      MYSQL_PASSWORD: 'myapp_user_pwd'
      MYSQL_ROOT_PASSWORD: '****************'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - my-db:/mnt/c/Projects/MyApp/Local/data/

volumes:
  my-db:

/Dockerfile

FROM node:alpine

RUN mkdir /src

WORKDIR /src

COPY app/ /src/app/

COPY gulpfile.js /src/

COPY .sequelizerc /src/

ADD app/package.json /src/package.json

RUN npm install --global gulp-cli

RUN npm install

COPY myapp.env /src/.env

EXPOSE 80 

CMD gulp prod

/.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Docker: Attach to Node",
            "port": 9229,
            "address": "localhost",
            "localRoot": "${workspaceRoot}/app",
            "remoteRoot": "/src/app",
            "protocol": "inspector"
        }
    ]
}

When I add the following to docker.compose.yml

command: sh -c "npm run start:debug:docker"

I get this on docker-compose up

web_1  | npm ERR! missing script: start:debug:docker

I tried googling for additional info and the idea is probably to run node in the debugging mode, but I obviously can't get it to work. Any help appreciated!

Upvotes: 2

Views: 4016

Answers (1)

OzrenTkalcecKrznaric
OzrenTkalcecKrznaric

Reputation: 5646

There was this:

/gulpfile.js

'use strict';
let gulp = require('gulp');
let nodemon = require('gulp-nodemon');

let started = !1;
let paths = {
    js: [
        '**/*.js',
        '!node_modules'
    ]
};

gulp.task('prod', (e) => {
    nodemon({
        script: './app/app.js',
        ext: 'js',
        watch: paths.js,
        ignore: ['./app/node_modules/**']
    }).on('start', () => {
        started || (started = !0, e());
    });
});

And when I added this line to nodemon options to enable debug over inspect:

nodeArgs: ['--inspect=0.0.0.0:9229'],

...all of a sudden the debugger started working.

DISCLAIMER: I started my journey to Node.js ecosystem from .Net world, where stuff like remote debugging and other basics usually aren't hidden behind... packages, but eh. Live and learn.

Upvotes: 3

Related Questions