Node-inspector in docker does not load sources - node.js

I've two docker containers with different images. This is the partial output of "docker ps" command:
$user: docker ps
CONTAINER ID IMAGE PORTS
9c8ff81215d4 node:slim 0.0.0.0:5858->5858/tcp, 0.0.0.0:10101->10101/tcp
d85a0de91432 node-debug 0.0.0.0:8080->8080/tcp
The first container is running a server app with debug option:
$user: node --debug server.js
Debugger listening on port 5858
...
and listens on port 5858 with debugger and on port 10101 with server.js app.
The second container is running node-inspector
$user: node-inspector
Node Inspector v0.12.6
Visit http://127.0.0.1:8080/?port=5858
that connects by default on port 5858 to debugger and listens on port 8080 for web-inspector in Chrome.
The issue is when I visit http://127.0.0.1:8080/?port=5858 I see the inspector without loaded sources.
In the Chrome console is see this error:
Request with id = 10 failed. "ErrorNotConnected: Error: connect ECONNREFUSED 127.0.0.1:5858. Is node running with --debug port 5858?"

The problem here is that node inspector is trying to connect to localhost/127.0.0.1, i.e. local to that container, not local to your host. When you run in bridge networking (default), each container is on its own IP.
You could quickly resolve this with either of these options:
Use host networking for both containers
In this case the port forwarding you configured is not necessary any longer
Use host networking just for the node inspector container
In this case you still need port 5858 mapped to host but no longer the port 8080 on node inspector

Related

Can't connect to Node.js inspector on exposed Docker container port

Running the command docker run -p 9222:9229 --name node-inspect-test -d node:alpine node --inspect-brk -e 'console.log("hello world")' should expose the node.js inspector on port 9222 on the Docker host.
Running curl http://localhost:9222/json results in curl: (56) Recv failure: Connection reset by peer.
Requesting the same endpoint from within the container with docker exec -it node-inspect-test wget -qO- http://localhost:9229/json succeeds.
Why does the exposed port 9222 not get forwarded to the internal port 9229 successfully?
I'm running Docker version 17.06.0-ce, build 02c1d87 on Ubuntu 16.04.2.
By default node inspector listens on the loopback interface. The --inspect-brk flag has the option of specifying host and port. In order to have the debugger listen on all interfaces so that it is accessible via the Docker host, use the flag --inspect-brk=0.0.0.0:9229.

Remote debugging NodeJS Container on AWS

Running a NodeJS Docker Container on an EC2 instance, I'm trying to remote debug it, but keep getting "connection refused" from the instance.
What I've tried -
Opening ports in EC2 security groups
Exposing ports in Dockerfile, both the port the app is listening on and the debug port
Forwarding the port within the Docker run command using the -p flag
Making sure the app is accessible directly through the port it's configured to listen to
After trying all of these, the debug port is still inaccessible by the remote debugger or even telnet.
Any ideas what could cause this?

docker connection refused nodejs app

I launch docker container:
docker run --name node-arasaac -p 3000:3000 juanda/arasaac
And my node.js app works ok.
If I want to change host port:
docker run --name node-arasaac -p 8080:3000 juanda/arasaac
Web page is not loaded, logs from browser console:
Failed to load resource: net::ERR_CONNECTION_REFUSED
http://localhost:3000/app.318b21e9156114a4d93f.js Failed to load resource: net::ERR_CONNECTION_REFUSED
Do I need to have the same port both in host and container? It seems it knows how to resolve http://localhost:8080 so it loads my website, but internal links in the webpage go to port 3000 and it's not as good :-(
When you are running your node.js app in a docker container it will only expose the ports externally that you designate with your -p (lowercase) command. The first instance with, "-p 3000:3000", maps the host port 3000 to port 3000 being exposed from within your docker container. This provides a 1 to 1 mapping, so any client that is trying to connect to your node.js service can do so through the HOST port of 3000.
When you do "-p 8080:3000", docker maps the host port of 8080 to the node.js container port of 3000. This means any client making calls to your node.js app through the host (meaning not within the same container as your node.js app or not from a linked or networked docker container) will have to do so through the HOST port of 8080.
So if you have external services that expect to access your node.js at port 3000 they won't be able.

Debug a NodeJS application inside Docker

I'm moving my NodeJS application to docker, and the last problem that I have encountered is debugging the application.
My setup: OSx, boot2docker, docker (based on centos), WebStorm as IDE and debugger.
Here's what I have by now:
Forward 5858 from docker to boot2docker:
docker run -p 5858:5858 ...
Forward 5858 port from boot2docker to host:
VBoxManage controlvm boot2docker-vm natpf1 "boot2docker5858,tcp,127.0.0.1,5858,,5858"
This same setup works to foreword my application ports to host machine.
Port 5858 on the other hand, doesn't seem to react if accessed from outside the docker container.
Inside the docker container it works just fine.
Any idea what can be done to make this work?
Well, I have finally figured it out.
As it seems, node listens only on 127.0.0.1:5858.
To make it listen on all ports, I installed HAProxy on the docker, that forwards the requests from 0.0.0.0:5859 to 127.0.0.1:5858.
Here's the HAProxy configuration if anybody ever needs:
listen l1 0.0.0.0:5859
mode tcp
timeout client 180000
timeout server 180000
timeout connect 4000
server srv1 127.0.0.1:5858
And than add to your Dockerfile:
COPY haproxy.conf haproxy.conf
RUN haproxy -D -f /haproxy.conf

Debug meteorjs application with WebStorm7

I have WebStorm7 installed on a Windows7 machine.
If I run a meteor project in the Windows7 machine with:
>set NODE_OPTIONS=--debug=47977 & meteor
it prints:
=> Meteor server running on: http://localhost:3000/
=> debugger listening on port 47977
and I can debug with WebStorm7 using the_Node.js Remote debug_ configuration, with Host: 127.0.0.1 and Port: 47977.
If I run a meteor project in a Ubuntu machine (within a Oracle VM VirtualBox, with address 192.168.1.9) with:
$ NODE_OPTIONS="--debug=47977" meteor
it prints only:
=> Meteor server running on: http://localhost:3000/
and I cannot debug with WebStorm from the Windows7 machine using the Node.js Remote debug configuration, with Host: 192.168.1.9 and Port: 47977.
From the ubuntu machine a telnet 127.0.0.1 47977 does not work too. It looks like the debugger is not started at all. What am I doing wrong?
the issue might be related to the fact that node.js debugging is only listening on localhost, so you can't connect to the used port from remote host. The workaround is to use a proxy (see http://delog.wordpress.com/2011/04/08/a-simple-tcp-proxy-in-node-js/, for example)
This proxy can be used as follows:
$: node tcpproxy.js 8585 127.0.0.1 5858
8585 here is the 'exposed' port that webstorm will connect to (you can make this what you wish). You are directing traffic that is coming in on 8585 to 5858 (the local debugging port). Ensure 8585 is open on your firewall if you have one. You have to specify this 'exposed' port in your Remote Debug run configuration as a debug port

Resources