I have a simple nodeJs app which is uses express frame work. I can successfully run this app inside a docker container.
I need to debug this app remotely from VS code how to do that.
the following steps I took to debug remotely inside a
simple node app.js
var express = require('express');
var app = express();
var fs = require('fs')
var logger = fs.createWriteStream('log.txt', { flags: 'a' })
app.get('/', function(reg, res){
res.send('Home page ' + reg.params.id);
});
app.get('/:id', function(reg, res){
if(reg.params.id != 'favicon.ico')
logger.write( ' ' + reg.params.id);
res.send('Home page ' + reg.params.id);
});
app.listen(3000, function(){
console.log('A test server is running');
})
VScode launch.json
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"address": "192.168.1.10",
"restart": false,
"sourceMaps": false,
"localRoot": "${workspaceRoot}/",
"remoteRoot": "/app/"
}
Dockerfile
FROM node:7.2.0-alpine
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN npm install
WORKDIR /app/src
EXPOSE 8080
EXPOSE 5858
CMD ["node","--debug-brk=5858","app.js"]
docker build command
docker build -t remotedebug .
docker run command
docker run --rm --name test -p8080:3000 -p5858:5858 remotedebug
after running the docker it console outputs a message
Debugger listening on 127.0.0.1:5858
but I cant access the node app from localhost:8080 and cannot debug from VScode.
2 Answers 2
you have to specify --inspect or --inspect-brk for your dockerfile cmd, remember to specfiy 0.0.0.0 as nodejs bind to 127.0.0.1 that would cause the problem.
example:
expose 9090
node --inspect=0.0.0.0:9090 ...otherargs
and vscode:
{
"type": "node",
"request": "attach",
"name": "attach to docker",
"port": 9290
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/app/server"
},
Comments
Are you using native docker or docker-machine? I suspect you're using docker machine in which case docker is running inside a virtual machine and you need to connect to the IP address of that machine rather than 127.0.0.1. Type:
docker-machine ls
And you'll get an output like this:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.100:2376 v1.9.1
In this case you'd tell VS Code to connect to 192.168.99.100:5858
9 Comments
node app.js' in docker file then I can access my app through ipaddress:8080` and when I execute node app.js --debug-brk=5858 app.js I cant access the appDISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS" NAME="Ubuntu" VERSION="16.04.1 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.1 LTS" VERSION_ID="16.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenialsudo ufw status numbered and post the output please?status: inactiveExplore related questions
See similar questions with these tags.