Debug the nestjs project in vscode using docker does not work - node.js

I have a NestJs project using docker and I would like to debug it in VSCode.
In package.json, I have:
"start:debug": "nest start --debug 0.0.0.0:9229 --watch"
In docker-compose, I have:
version: '3.8'
services:
core-database:
image: postgis/postgis:latest
volumes:
- /tmp/rg-core/postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=***
- POSTGRES_USER=***
- POSTGRES_PASSWORD=***
ports:
- 5433:5432
core-service:
container_name: core-service
working_dir: /usr/src/app
build:
context: .
dockerfile: ./Dockerfile.dev
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 9001:9001
#debugging port
- 9229:9229
# Database
- DB_HOST=***
- DB_PORT=5432
- DB_NAME=***
- DB_USER=***
- DB_PASSWORD=***
command: 'yarn run start:debug'
In launch.json, I have:
{
// 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": "Attach",
"port": 9229,
"request": "attach",
"localRoot": "${workspaceFolder}",
"type": "node",
"address": "0.0.0.0",
"restart": true,
"remoteRoot": "/usr/src/app",
"sourceMaps": true,
"skipFiles": [
"<node_internals>/**"
]
}
]
}
I'm running docker compose up and giving start debugging in VSCode, but it's not stopping at the breakpoint.
Am I doing something wrong? how should it be done?

Was missing port 9229 export in Dockerfile.
In the package.json: "dev": "nest start --debug 0.0.0.0:9229 --watch"
In the docker-compose:
version: '3.8'
services:
core-database:
image: postgis/postgis:latest
volumes:
- /tmp/rg-core/postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=***
- POSTGRES_USER=***
- POSTGRES_PASSWORD=***
ports:
- 5433:5432
core-service:
container_name: core-service
working_dir: /usr/src/app
build:
context: .
dockerfile: ./Dockerfile.dev
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 9001:9001
#debugging port
- 9229:9229
# Database
- DB_HOST=***
- DB_PORT=5432
- DB_NAME=***
- DB_USER=***
- DB_PASSWORD=***
command: 'yarn run dev'
In the launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug core-service",
"type": "node",
"request": "attach",
"remoteRoot": "/usr/src/app",
"port": 9229,
"restart": true
}
]
}
In the Dockerfile:
...
EXPOSE 9001 9229

I am trying to deal with the same problem for a few days now.
From my research, VS Code Debug Console by default is listening for console.logs. However NestJS Logger is logging to stdout/stderr (process.stdout.write), so launch.json needs the additional line:
{
... ,
"outputCapture": "std"
}
Sadly, it is still not enough to make it work.
To workaround this for now, I'm attaching a running container's terminal to VS Code integrated terminal manually, executing docker container attach --no-stdin <container-name>.
This is still not a real solution so I hope someone brings one. Thank you for raising this issue. Can you share the contents of your Dockerfile for full reference?

Related

VS code debugging of a NestJs dockerized apps inside a monorepo

I have been trying to figure out for a while how I would go about setting up a attachment to node debugging processes that are exposed in my environment from multiple running nestjs apps within a mono-repo setup. (With VS code)
https://github.com/bozvul993/nest-testing-mono-repo-debug
Ideally i want the debugging sessions restarted on code changes [If this is possible], but more importantly working.
I have provided a repository with my sample project.
To run the apps inside /docker folder
docker-compose -f dev.yml up
This brings up the three apps in the monorepo. All apps exposed to the host machine their default node debugging ports...
My vs code launch configuration that i used to attempt this i included:
"type": "node",
"request": "attach",
"name": "Debug App1",
"address": "0.0.0.0",
"port": 9231,
"localRoot": "${workspaceFolder}/mono-repo",
"remoteRoot": "/app/mono-repo",
"trace": true,
"restart": true,
"sourceMaps": true,
"skipFiles": [
"<node_internals>/**"
]
}
With Web-storm this was easier to achieve somehow..
I found this https://code.visualstudio.com/docs/containers/debug-node to be quite useful.
Long story short, my docker-compose file looks like
version: '3.7'
services:
api:
container_name: api
build:
context: .
target: development
volumes:
- '.:/app'
- './node_modules:/app/node_modules'
command: yarn start:debug
ports:
- ${API_PORT}:${API_PORT}
- 9229:9229
networks:
- network
mongo_db:
...
...
...
networks:
network:
driver: bridge
Where the development part of the Docker file picked from the docker-compose.yaml file looks like
FROM node:16-alpine as development
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn
COPY . .
RUN yarn build
FROM node:16-alpine as production
...
And my launch.json looks like
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug: api",
"type": "node",
"request": "attach",
"restart": true,
"port": 9229,
"address": "0.0.0.0",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app",
"protocol": "inspector",
"skipFiles": ["<node_internals>/**"]
}
]
}
And finally, but not least important, the start:debug parts of package.json must be altered a little.
Mine looks like
"start:debug": "nest start --debug 0.0.0.0:9229 --watch",
Works on my machine :) by first starting all the containers using docker compose up -d, and then starting the debugging process from VS code.
VS code 1.57.1, Docker version 20.10.7, MacOS

Why does Visual code unbound remote debug

I try to debug js with docker by vscode IDE
vcode loss debugging - unbound after changed code
My visual code (.vscode) launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug: app-name",
"remoteRoot": "/usr/src/app",
"localRoot": "${workspaceFolder}",
"protocol": "inspector",
"port": 9229,
"restart": true,
"address": "0.0.0.0",
"skipFiles": ["<node_internals>/**"]
}
]
}
}
My Docker file
DockerFile
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
COPY . .
RUN npm run build
package.json command
"start:debug": "nest start --debug 0.0.0.0:9229 --watch",
docker-compose
version: "3.8"
services:
resource:
container_name: gate
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
build:
context: .
target: development
ports:
- 3001:3000
- 9229:9229
command: npm run start:debug
networks:
webnet:
volumes:
pgdata:
unbound brakepoints
I founded the problem,
set debug.javascript.usePreview to false
and my debugger works well.
https://github.com/microsoft/vscode/issues/102493

Debug webpack-dev-server application inside Docker container

I'm using webpack-dev-server to run an Nestjs application inside a Docker container. All is up and running, but I can't debug the application from my VS Code instance. I'm trying to expose the 9229 port using this configuration on the webpack.config.js:
devServer: {
host: '0.0.0.0',
port: 9229,
},
When I run netstat -l inside the container I can see that node is not listening the 9229 port:
I'm exposing the port 9229 in the Dockerfile and docker-compose files. The Dockerfile:
FROM node:12.16.1-alpine
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
COPY . .
EXPOSE 3000
EXPOSE 9229
CMD [ "yarn", "run", "start:debug"]
And the docker-compose.yml file:
version: '3.7'
services:
open-tuna-api:
image: opentunaapi
ports:
- 3000:3000
- 9229:9229
volumes:
- ./dist:/usr/src/app/dist
- ./:/usr/src/app
networks:
- open-tuna-network
expose:
- 9229
networks:
open-tuna-network:
And this is the script I'm using to run the application:
"start:debug": "webpack --config webpack.config.js && node --inspect=0.0.0.0:9229 node_modules/webpack-dev-server/bin/webpack-dev-server.js",
My launch configuration is as follow:
{
"name": "Attach",
"preLaunchTask": "compose-up",
"stopOnEntry": true,
"type": "node",
"request": "attach",
"port": 9229,
"cwd": "${workspaceFolder}", // the root where everything is based on
"localRoot": "${workspaceFolder}", // root of all server files
"remoteRoot": "/usr/src/app", // workspace path which was set in the dockerfile
"outFiles": ["${workspaceFolder}/dist/**/*.js"], // all compiled JavaScript files
"protocol": "inspector",
"restart": true,
"sourceMaps": true,
"trace": "verbose",
"address": "0.0.0.0",
"skipFiles": [
"<node_internals>/**"
],
}
And when I run this configuration with the container up and running I'm receiving a message saying that VS Code cannot connect to the process.
So, my question is: is there a way to debug JavaScript / TypeScript app running on webpack-dev-server inside a Docker container? What is wrong in my environment?
Thanks for the help.
EDIT
Apparently my issue has no relation with Docker, since I can reproduce it outside of the container.
Have a look at your config and make sure you include the program field. And point it to the right file under node_modules.
"program": "${workspaceRoot}/node_modules/webpack-dev-server/bin/webpack-dev-server.js"
That should get you going.
If you want more insight into this, there's a longer conversation that you might find useful - check out this comment on the main webpack-dev-server GitHub repo.

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?
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

Cannot find module, docker - babel config in node

I have a cannot find module error using docker. I'm not sure what is going on. I've tried deleting 'volumes' in the docker-compose file. I have also tried rming the image and running docker-compose up again. I'm really at a loss as to what is happening here. Any help would be appreciated.
docker-compose
version: '2'
services:
nginx:
build: "./nginx"
links: ["node1", "node2"]
ports: ["80:80"]
node1:
build:
context: "./node"
args:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
environment:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
NODE_PATH: "lib"
NODE_ENV: "production"
POSTGRES_USER: "admin"
POSTGRES_PASSWORD: "password"
links: ["postgres", "mongo"]
ports: ["5000:5000"]
node2:
build:
context: "./node"
args:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
environment:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
NODE_PATH: "lib"
NODE_ENV: "production"
POSTGRES_USER: "admin"
POSTGRES_PASSWORD: "password"
links: ["postgres", "mongo"]
ports: [5000]
postgres:
image: "postgres"
environment:
POSTGRES_USER: "admin"
POSTGRES_PASSWORD: "password"
ports: ["5432:5432"]
mongo:
image: mongo
ports: ['27017:27017']
Dockerfile
FROM node
# Set up environment
RUN npm config set proxy $http_proxy
RUN npm config set https-proxy $https_proxy
# Install app
ENV INSTALL_PATH="/opt/node"
RUN ["mkdir", "-p", "$INSTALL_PATH"]
ADD package.json $INSTALL_PATH/package.json
ADD index.js $INSTALL_PATH/index.js
# Define working directory
WORKDIR $INSTALL_PATH
# Install dependencies
RUN npm install -g nodemon
RUN npm install
# Expose port
EXPOSE 5000
# Run app
ENTRYPOINT npm start
index.js
require('babel-core/register')()
require('babel-polyfill')
require('./bin/server.js')
package.json
{
"name": "no-commerce",
"version": "0.0.1",
"description": "API for No-Commerce",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "./node_modules/.bin/nodemon index.js",
"test": "NODE_ENV=test ./node_modules/.bin/mocha --compilers js:babel-register --require babel-polyfill",
"lint": "eslint src/**/*.js",
"docs": "./node_modules/.bin/apidoc -i src/ -o docs"
},
Error: Cannot find module './bin/server.js'
File Structure:
- Root
-docker-compose
-node
-package.json
-bin
-server.js
-index.js
-Dockerfile
-nginx
For starters, you are only adding these files to the container in your docker file:
ADD package.json $INSTALL_PATH/package.json
ADD index.js $INSTALL_PATH/index.js
You need to add server.js to $INSTALL_PATH/bin

Resources