1

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.

asked Jan 11, 2018 at 5:08

2 Answers 2

1

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"
},
answered May 21, 2019 at 18:02
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Jan 11, 2018 at 5:33

9 Comments

I have separate linux computer with docker installed, I am accessing it from my local network. There no virtual machine. when I execute 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 app
Which version of which distro are you running? It's likely that the local firewall is preventing access to port 5858 and 8080.
DISTRIB_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=xenial
Can you run sudo ufw status numbered and post the output please?
status: inactive
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.