Can't remote debug node running in docker container - node.js

Github link
I'm trying to debug my backend node server running inside a docker container. For the life of me I can't seem to remote debug the node process when it's running in docker. I used netstat -an to verify that ports 9229 are open in both my host machine and in the container, and I made sure to map the port 9229 on my host machine to 9229 in the container. I'm running node with nodemon --inspect --trace-warnings --unhandled-rejections=strict server.js but have also tried node --inspect server.js without success.
I've tried multiple vscode launch.json configurations, none of which have worked.
The first config is the default one and does not attempt to attach to a running node process.
The second fails with the error ENOENT: no such file or directory, open '~/Desktop/fiction-forge/package.json'. I don't know why the docker extension is looking for package.json in ~/Desktop/fiction-forge.
The third seems to try to connect, giving a loading animation in vscode, but ultimately fails. I've also tried the remote debugger in Chrome to no avail.
I am able to remote debug the node process with the chrome remote debugger when I run it on my host machine on port 9229. It remains a mystery to me why, if the ports are opened and mapped correctly, there is a difference when node is running in the container.
You can create the image and run the container with docker build -t fiction-forge . && docker run -p 3000:3000 -p 5000:5000 -p 9229:9229 fiction-forge. Then start the server with docker exec <container-name> npm run dev --prefix server.

Running node with --inspect=0.0.0.0:9229 and specifying 127.0.0.1:9229 as a connection in the chrome devtools worked.

Related

File system events not triggered in docker container when files in mounted volume are changed on the host

I would like to use nodemon to restart my project when its files are changed. I think nodemon works by listening for inotify events to trigger reloading a node.js project.
The project runs in a docker container, and the project files are in a mounted volume.
When the project files are edited from inside the docker container, for example
docker-compose exec dev vim server.js
nodemon works correctly and restarts the server.
However, when an editor running on the host machine is used, nodemon does not pick up the changes and restart the program.
The contents of the files in the docker container do in fact change, so I suspect editing files this way just doesn't trigger an FS event.
Is it possible to set this up so that editing files on the host machine causes file system events to occur in the Docker container? Why does this not happen already?
Platform Info:
Docker for Windows (Hyper-V)
node docker container
WebStorm -- Host based editor
It looks like file system events just don't work when Docker is running in Hyper-V and the changes happen on the host. But, it's possible to work around that limitation by enabling polling in nodemon:
nodemon -L server.js
In WebStorm the full command that ends up getting used is
docker-compose run dev node node_packages/nodemon/bin/nodemon.js -L server.js
More info:
https://github.com/remy/nodemon#application-isnt-restarting

NodeJS in Docker doesn't see connection

I have a NodeJS/Vue app that I can run fine until I try to put it in a Docker container. I am using project structure like:
When I do npm run dev I get the output:
listmymeds#1.0.0 dev /Users/.../projects/myproject
webpack-dev-server --inline --progress --config build/webpack.dev.conf.js
and then it builds many modules before giving me the message:
DONE Compiled successfully in 8119ms
I Your application is running here: http://localhost:8080
then I am able to connect via browser at localhost:8080
Here is my Dockerfile:
FROM node:9.11.2-alpine
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD npm run dev
EXPOSE 8080
I then create a docker image with docker build -t myproject . and see the image listed via docker images
I then run docker run -p 8080:8080 myproject and get a message that my application is running here: localhost:8080
However, when I either use a browser or Postman to GET localhost:8080 there is no response.
Also, when I run the container from the command line, it appears to lock up so I have to close the terminal. Not sure if that is related or not though...
UPDATE:
I trying following the Docker logs such as
docker logs --follow
and there is nothing other than the last line that my application is running on localhost:8080
This would seem to indicate that my http requests are never making into my container right?
I also tried the suggestion to
CMD node_modules/.bin/webpack-dev-server --host 0.0.0.0
but that failed to even start.
It occurred to me that perhaps there is a Docker network issue, perhaps resulting in an earlier attempt at kong api learning. So I run docker network ls and see
NETWORK ID NAME DRIVER SCOPE
1f11e97987db bridge bridge local
73e3a7ce36eb host host local
423ab7feaa3c none null local
I have been unable to stop, disconnect or remove any of these networks. I think the 'bridge' might be one Kong created, but it won't let me whack it. There are no other containers running, and I have deleted all images other than the one I am using here.
Answer
It turns out that I had this in my config/index.js:
module.exports = {
dev: {
// Various Dev Server settings
host: 'localhost',
port: 8080,
Per Joachim Schirrmacher excellent help, I changed host from localhost to 0.0.0.0 and that allowed the container to receive the requests from the host.
With a plain vanilla express.js setup this works as expected. So, it must have something to do with your Vue application.
Try the following steps to find the source of the problem:
Check if the container is started or if it exits immediately (docker ps)
If the container runs, check if the port mapping is set up correctly. It needs to be 0.0.0.0:8080->8080/tcp
Check the logs of the container (docker logs <container_name>)
Connect to the container (docker exec -it <container_name> sh) and check if node_modules exists and contains all
EDIT
Seeing your last change of your question, I recommend starting the container with the -dit options: docker run -dit -p 8080:8080 myproject to make it go to the background, so that you don't need to hard-stop it by closing the terminal.
Make sure that only one container of your image runs by inspecting docker ps.
EDIT2
After discussing the problem in chat, we found that in the Vue.js configuration there was a restriction to 'localhost'. After changing it to '0.0.0.0', connections from the container's host system are accepted as well.
With Docker version 18.03 and above it is also possible to set the host to 'host.docker.internal' to prevent connections other than from the host system.

Cannot access server in docker container

I started a docker image using
docker run --interactive --tty node_web_1
And the running server inside the container successfully prints
Running on http://localhost:8080
in the console. I have exposed port 8080 in the Dockerfile. So in my understanding, when I call http://localhost:8080 in the browser in windows, I should access the server in the container, right? I get no reply though....
How do I go about finding the error? I executed the command
docker-machine ls
as suggested here How to access Docker container's web server from host but apparently I have no docker-machines running? What are those machines? Do I need them? I have only docker for windows installed, no additionall packages or shells.
try to publish your port
docker run -p 8080:8080 -it node_web_1

How do you RUN npm module node-inspector in OpenShift environment?

I have made these failing attempts are on the OpenShift gear via SSH.
Attempt-1:
node-debug app.js
Node Inspector v0.12.7
Cannot start the server at 127.0.0.1:8080. Error: listen EACCES.
Attempt-2:
node-inspector
Node Inspector v0.12.7
Cannot start the server at 0.0.0.0:8080. Error: listen EACCES.
Attempt-3:
node --debug app.js
debugger listening on port 5858
Failed to open socket on port 5858, waiting 1000 ms before retrying
I don't know what the correct method is to get an installed node-inspector running in an OpenShift Node.JS Gear.
Not sure if this is way too late or if you have found a solution.
I've managed to get this working in OpenShift 'origin' on my local machine
I made use of the node4-rhel7 image so the following is specific (i.e entrypoint and adding to /usr/bin in the image), so change it accordingly
Here are the steps :-
create or update a Dockerfile that gave me the correct permissions (rw) for my node app
install node-inspector (npm install node-inspector --save)
add this script (call it start.sh - in a folder root/usr/bin) also remember to set execute permissions
if [ $# gt 0 ] && [ "$1" == "debug"]
then
# assume default debug port of 5858
node --debug name-of-startup.js
node_modules/node-inspector/bin/inspector.js -p 9000 --save-live-edit=true
else
node name-of-startup.js
fi
add a script called container-entrypoint also in a folder root/usr/bin
#!/bin/bash
exec "$#"
add this to your Dockerfile
ADD root /
ENTRYPOINT ["container-entrypoint"]
CMD ["start.sh"]
docker build -t name-of-your-image:version .
oc edit dc/name-of-your-node-pod
add port: 9000 and protocol: TCP in "spec.containers.ports"
add "command": [ "start.sh","debug"] in "spec.containers"
update the image to the newly built one on your local docker (name-of-your-image:version)
save (this will re-deploy your app) and execute 'oc get ep' (to get the endpoint of you app)
open chrome with with url http://your-app-endpoint:9000/?port=5858
you can now debug/set breakpoints etc and change code via the chrome interface - primitive but works like a charm :)

Connection refused when accessing web app through Docker

I'm pretty new to Docker, so I'm trying to take a node web app that I've written and Docker-ize it. The app is open source, so you can find it and the Dockerfile here: Paw-Wars
So you don't have to click through, the Dockerfile is here:
FROM mhart/alpine-node
WORKDIR /src
ADD . .
RUN npm install
EXPOSE 5050
COPY config.json /src/config.json
CMD npm run docker
So I open up the Docker Quickstart Terminal (I'm on Mac OS X), go to my path, and build it:
docker build -t paw-wars .
After it builds, I run it:
docker run paw-wars
And it spins up just fine and says it's listening on port 5050. I get the ip from docker-machine ip default, and try to connect to it on port 5050, but I get connection refused. Most searches I've done trying to solve this tell me that I need to make sure to use the correct IP, but I'm almost positive I'm doing that. Not really sure what I'm doing wrong. It's not in the repo, but I've also tried binding to 0.0.0.0 in my app (index.js), but that didn't work either.
Thanks!
The issue is that you MUST specify a port in your docker run command. I thought that using EXPOSE in your dockerfile is sufficient, but it's not. That's just to get the port exposed internally.
docker run -p 5050:5050 paw-wars worked great.

Resources