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.
Related
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?
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
I am trying to debug a typescript nestjs application in --watch mode (so it restarts when files are changed) in Visual Studio code (using the Docker extension). The code is mounted in docker through the use of a volume.
It almost works perfectly, the docker is correctly launched, the debugger can attach, however I have one problem that I can't seem to work out:
As soon as a file is changed, the watcher picks it up and I see the following in docker logs -f for the container:
[...]
[10:12:59 AM] File change detected. Starting incremental compilation...
[10:12:59 AM] Found 0 errors. Watching for file changes.
Debugger listening on ws://0.0.0.0:9229/af60f5e3-394d-4df3-a565-8d15898348bf
For help, see: https://nodejs.org/en/docs/inspector
user#system:~$
# (at this point the docker logs command stops and the docker is gone)
At that point vscode ends the debugging session, the docker stops (or vice versa?) and I have to manually restart it.
If I launch the exact same docker command (copy/pasted from the vscode terminal window) manually, it does not stop when changing a file. This is the command it generates:
docker run -dt --name "core-dev" -e "DEBUG=*" -e "NODE_ENV=development" --label "com.microsoft.created-by=visual-studio-code" -v "/home/user/projects/core:/usr/src/app" -p "4000:4000" -p "9229:9229" --workdir=/usr/src/app "node:14-buster" yarn run start:dev --debug 0.0.0.0:9229
I did try to look with strace what happens and this is what I see on the node process when I change any file:
strace: Process 28315 attached
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED, si_pid=40, si_uid=0, si_status=SIGTERM, si_utime=79, si_stime=9} ---
+++ killed by SIGKILL +++
The killed by SIGKILL line does not happen when docker is run manually, it only happens when it's started from vscode when debugging.
Hopefully someone has an idea where I'm going wrong.
Here are the relevant configs:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker Node.js Launch",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"platform": "node"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dockerRun": {
"customOptions": "--workdir=/usr/src/app",
"image": "node:14-buster",
"command": "yarn run start:dev --debug 0.0.0.0:9229",
"ports": [{
"hostPort": 4000,
"containerPort": 4000
}],
"volumes": [
{
"localPath": "${workspaceFolder}",
"containerPath": "/usr/src/app"
}
],
"env": {
"DEBUG": "*",
"NODE_ENV": "development"
}
},
"node": {
"enableDebugging": true,
}
}
]
}
Here is a hello world repo: https://github.com/strikernl/nestjs-docker-hello-world
So here's what I found out. When you change code it restarts node's debugger process. VSCode kills Docker container when it loses connection to the debugger.
There is a nice feature which restarts debugger sessions on code changes (see this link) but the problem is - it is for "type": "node" launch configurations. Yours is "type": "docker". From it's options for node only autoAttachChildProcesses seems promising but it doesn't solve the problem (I've checked).
So my suggestion is:
Create a docker-compose.yml file, which will start the container instead of VSCode.
Edit your launch.json so that it attaches to the node in container and restarts debugger session on changes.
Remove/rework tasks.json as it is not needed in it's current state.
docker-compose.yml:
version: "3.0"
services:
node:
image: node:14-buster
working_dir: /usr/src/app
command: yarn run start:dev --debug 0.0.0.0:9229
ports:
- 4000:4000
- 9229:9229
volumes:
- ${PWD}:/usr/src/app
environment:
DEBUG: "*"
NODE_ENV: "development"
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to node",
"type": "node",
"request": "attach",
"restart": true,
"port": 9229
}
]
}
Save the docker-compose.yml in your project root and use docker-compose up to start the container (you may need to install it first https://docs.docker.com/compose/ ). Once it's working start the debugger as usual.
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
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