Debug nodejs in container
There are multiple ways of debugging within nodejs, from most manual one to utilize vscode .devcontainer
feature
Method 1: Manual start debugger
Checks there is a launch.json
file within .vscode
, with following conten
{
"name": "Docker: Attach to Node.js",
"type": "node",
"request": "attach",
"remoteRoot": "/work/src/",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}/nodejs/src",
"protocol": "inspector",
"restart": true,
},
- Make sure
CMD ["nodemon", "--inspect=0.0.0.0:9229", "./src/server.js"]
within dockerfile is commented out.
- Build and run nodejs container
- In a separate terminal,
docker exec -it nodejs sh
to attach shell to container.
- Manually start debugger process in container
nodemon --inspect=0.0.0.0:9229 ./src/server.js
- In vscode debugger, select the debugger profile "Docker: Attach to Node.js", and start debugger by link to the port 9229
- Set breaker in the codes
- In Chrome, open
localhost:5502
to the open port
- Should notice that program will stop at breakpoint
Method 2:
Reading lists