# Debugging NodeJs in Node-RED Docker container

**URL:** <https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370>\
**Category:** Developing Nodes\
**Created:** [6 February 2020 20:43 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370 "2020-02-06T20:43:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 February 2020 20:43 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/1 "2020-02-06T20:43:15Z")

</div>

Hi folks,

My test system died yesterday, after a few desperate attempts to downgrade NodeJs. At the end I decided to start using **Docker containers**. And shortly afterwards I was up-and-running again. My compliments to the Node-RED chef and sous-chef for the nice Docker setup and tutorial!!!!!

Now I would like to start debugging my Node-RED server-side code again. But I'm not sure how to achieve with NodeJs running in a Docker container.

In a non-Docker setup that was very easy:

- I needed to start NodeJs in debug mode:  
_node --inspect=0.0.0.0:5858 /usr/lib/node\_modules/node-red/red.js_
- And then I could connect with the Chrome debugger to port 5858

So I can forward port 5858 of my host system to port 5858 of the Docker container, but I'm not sure how to start NodeJs with the `--inspect=0.0.0.0:5858` parameter.

Hopefully this doesn't mean I have to start creating (somehow) Docker images by myself?

Thanks a lot !!!  
Bart

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 February 2020 21:31 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/2 "2020-02-06T21:31:37Z")

</div>

I 'think' I need to pass the NodeJs command line parameter as **NODE\_OPTIONS** environment variable:

```auto
sudo docker run -it -p 1880:1880 -p 5858:5858 -v /home/pi/.node-red:/data -e NODE_OPTIONS="--inspect=0.0.0.0:5858" --name mynodered nodered/node-red:1.0.3-12-minimal-arm32v6

```

Then it indeed starts very promising, but it fails immediately afterwards:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a5ff5b00dc02500e62d12e4b35faa8efbc82baca.png)

When I use another port number (e.g. the default inspector port 9229) I get the same error ...  
It looks like i tries to use the port twice now.

[EDIT] Could it be related to [this](https://github.com/zeit/next.js/issues/9027) somehow?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 February 2020 22:31 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/3 "2020-02-06T22:31:13Z")

</div>

I'm certain you have searched Bart but in case you missed this one, thought I'd post it (sorry I've only small experience of docker and even less when it comes to remote debugging)

> **[Live Debugging with Docker - Docker Blog](https://www.docker.com/blog/live-debugging-docker/)**
>
> Learn from Docker experts to simplify and advance your app development and management with Docker. Stay up to date on Docker events and new version announcements!

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 February 2020 22:48 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/4 "2020-02-06T22:48:22Z")

</div>

Hey Steve,

Thanks for joining!  
I'm not experienced with this stuff, but in the [package.json](https://github.com/node-red/node-red-docker/blob/master/package.json#L13) file of the **node-red-docker** repository, I see this:

```auto
"scripts": {
    "start": "node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS"
},

```

So I 'assume' this will be [called](https://github.com/node-red/node-red-docker/blob/2dad4559bec8aeb6e42f76da8d519ba15610fc06/README.md#startup) by NPM:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22b07acfdf7f4f32341a9fbbcefb5126768aae08.png)  
And from the link in my previous post you can see this:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34bc65624e3867a0dfa9cd9fedbd0e0cfa598b60.png)

So I think that first the port is being used by NPM, and afterwards by NodeJS (which will fail because the port is already in use...)? But I don't know how that guy his solution could be implemented in the Node-RED docker container 🥺

Unfortunately this stuff is not my cup of tea ...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [7 February 2020 08:15 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/5 "2020-02-07T08:15:49Z")

</div>

In the link I added above, somebody has provided a solution:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/777434d61f3db8ef51a73e389f0bf9c953d40ff5.png)

Could that perhaps be used in the Node-RED [Dockerfile.alpine](https://github.com/node-red/node-red-docker/blob/master/.docker/Dockerfile.alpine#L93) file, by adding the $NODE\_OPTIONS in the entrypoint:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/faff54ee36f960432b09b1eb6957d85e781100f4.png)

And then remove the $NODE\_OPTIONS from the nodeJs start command in the [package.json](https://github.com/node-red/node-red-docker/blob/master/package.json#L13) file:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/322c64a48488937ba9e29e210072ee61463f31c0.png)

Or is that complete bull what I'm saying here?  
Can I perhaps change my local Docker image to test this theory??

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 February 2020 06:45 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/6 "2020-02-09T06:45:33Z")

</div>

To be able to fix this, I have been reading a bit more about this topic.  
And it seems (e.g. [here](https://www.luigicolella.it/blog/how-to-create-nodejs-app-with-docker-3)) that I have to solve it in another way:

1. I started creating my own Node-RED Docker image, as explained very well in this [tutorial](https://github.com/node-red/node-red-docker/tree/master/docker-custom). Again my compliments to the Node-RED chef and his sous-chef!

2. Then _ **I only have added a "debug" script line** _ to the [package.json](https://github.com/node-red/node-red-docker/blob/master/docker-custom/package.json#L12) file:

3. Then I have build my Docker image (named _"testing:node-red-build"_).

4. Once the build was completed, I have started a container (from my custom Docker image) via this command:

5. Then I have specified (once) the IP address and port 9229 in my Chrome debugger, and it immediately allows me to start debugging the server-side code (which is running inside the Docker container!):

So it works fine, and I don't think there is impact on non-debug environments.

So now I only need to have the approval of @knolleary and @dceejay, whether this solution is good enough to create a **pull request** (to add the _"debug_" script line to the package.json file)? It would be great if I could do all my developments and debugging on Docker containers, since that allows me to setup very easily new clean test environments (e.g. with various NodeJs versions)...

**Question:** Is it also ok if I add a third script to the package.json file:

```auto
"debug_brk": "node --inspect=0.0.0.0:9229 --inspect-brk $NODE_OPTIONS node_modules/node-red/red.js $FLOWS"

```

The extra _" -- inspect - brk"_ flag will cause Node-RED to break already on the first line of its application code. This allows developers to debug the startup code, which might be useful sometimes ...

Thanks for reading!!!  
Bart

Here the _ **details of the command** _ to start the Node-RED Docker container in debug mode, for people that want to have more background information:

- _"Docker run"_ starts a container.

- _"-it"_ (Optional) tells Docker to attach your terminal to the container, so you can see that NodeJs indeed has been started in debug mode:

- _"-p 1880:1880"_: Tell Docker that port 1880 of the host system (e.g. Raspberry) should be forwarded to port 1880 of this Docker container. That way you access your Node-RED web-server (which is running inside the Docker container), like you used to do without Docker...

- _"-p 9229:9229"_: Tell Docker that port 9229 of the host system (e.g. Raspberry) should be forwarded to port 9229 of this Docker container. That way your debugger (e.g. Chrome, Visual Code, ...) can communicate with NodeJs to do remote debugging.

- _"--entrypoint npm testing:node-red-build run debug"_: The Node-RED [DockerFile](https://github.com/node-red/node-red-docker/blob/master/docker-custom/Dockerfile.custom#L91) contains the following entrypoint:

- _"--"_ This npm argument tells npm that the next argument won't be an npm argument but that it needs to be passed as an argument to the _"debug_" script.

- _"--userDir /data"_: npm will pass this argument to the _"debug_" script, which means it becomes an argument for Node-RED. This way Node-RED knows that his data folder (with flow, credentials, ... files) will be the _/data_ folder (in the Docker container filesystem). In the Node-RED [documentation](https://github.com//node-red/node-red-docker#using-a-host-directory-for-persistence-bind-mount), you can see that you can use this folder as a mount point for e.g. the /home/pi/.node-red folder on your host system (i.e. a Raspberry for example).

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 February 2020 08:33 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/7 "2020-02-09T08:33:34Z")

</div>

Excellent work Bart.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 10:05 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/8 "2020-02-09T10:05:34Z")

</div>

Hi Bart,  
now that looks like a better solution. Happy if you want to fire up a PR pointed at the dev branch of the node-red-docker project. Even better if it included a section for the docs !

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 February 2020 10:14 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/9 "2020-02-09T10:14:58Z")

</div>

> [@dceejay](#):
>
> PR pointed at the dev branch

Hey Dave,  
Very glad to hear that a PR is allowed. Thanks!  
And also good to know (in front) that I need to target the '_dev_' branch!!!!

> [@dceejay](#):
>
> Even better if it included a section for the docs !

Do you mean the [readme](https://github.com/node-red/node-red-docker#linking-containers) file of the node-red-docker project itself:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c6a9c5f8590a6d7c496d68a82683e564450a1cd3.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 10:58 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/10 "2020-02-09T10:58:05Z")

</div>

Yes - somewhere in the README that makes sense.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 February 2020 13:58 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/11 "2020-02-09T13:58:52Z")

</div>

Hey Dave,  
the [pull-request](https://github.com/node-red/node-red-docker/pull/165) has been created, inclusive some documentation 🍾. This means you can publish it in Dockerhub around 2024 🤐

> [@BartButenaers](#):
>
> **Question:** Is it also ok if I add a third script to the package.json file:

To avoid mistakes, I have build a new local docker image which now also contains the second script ("debug\_brk").

When I start the docker container using that second script:

```auto
docker run -it -p 1880:1880 -p 9229:9229 --entrypoint npm testing:node-red-build run debug_brk -- --userDir /data

```

Then you will see in the log that NodeJs nicely starts listening to the debug port, and then it breaks automatically:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e217afd5559eae927fa18c8e1e5a3454f7274f43.png)

NodeJs will wait at the first statement of Node-RED until you connect with a debugger to port 9229:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bbe3ac96e7cac532a61a2fab6d8d3af1cf6782ee.png)

From here on you can start debugging the entire Node-RED startup code, or the startup code of each contribution node ...

Have fun with it !!!  
Bart

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 14:00 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/12 "2020-02-09T14:00:18Z")

</div>

> [@BartButenaers](#):
>
> publish it in Dockerhub around 2024

you're VERY optimistic aren't you 🙂  
maybe by 20:24 tonight

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 February 2020 15:19 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/13 "2020-02-09T15:19:18Z")

</div>

Haha!  
Please don't hurry. I have now my own private debuggable container which fits my needs until you have time to publish it...

Just out of curiosity: do you need to build the image manually on various types of hardware? Because initially I had accidentally started an AMD build on my Raspberry, which failed. Afterwards I had started an ARM build, which runned fine.

Before this weekend, I wasn't even aware that there were multiple builds for a single image. Had always thought that Docker images where hardware independent 😌

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 16:20 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/14 "2020-02-09T16:20:57Z")

</div>

Thanks to the great work by @RaymondMouthaan we have travis build it for multiple version across multiple platforms.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 February 2020 16:53 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/15 "2020-02-09T16:53:44Z")

</div>

For everybody following this discussion. I have added a [wiki](https://github.com/node-red/node-red-docker/wiki/Debug-container-via-Chrome-Developer-Tools) page to the node-red-docker repository, which explains how to debug Node-RED (server-side code) via Chrome Developer tools. Note that this also works when Node-RED is running without Docker ...  
As usual, all '_constructive_' feedback is more than welcome!

---

<div class="post-metadata">

**Author:** ![RaymondMouthaan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raymondmouthaan/32/12692_2.png) [@RaymondMouthaan](https://discourse.nodered.org/u/RaymondMouthaan)\
**Post date:** [9 February 2020 22:28 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/16 "2020-02-09T22:28:58Z")

</div>

Great work for figuring out remote debugging! Thanks!

Later this week I will have a look to get remote debugging feature as part of the official Node-RED images. My idea is to have an environment variable "remote-debug", which can be set to true or false and is false by default. In case it's true, the "script" you provided will be loaded and node-red runs in debug, so a connection from Chrome can be established. This way it's easy for users to toggle between normal and remote debug mode, without building custom images or load specific scripts.

Greets,  
Raymond

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 22:42 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/17 "2020-02-09T22:42:46Z")

</div>

meanwhile I have taken the PR as-is and pushed it to [https://hub.docker.com/r/nodered/node-red-dev](https://hub.docker.com/r/nodered/node-red-dev) to make it easier to test

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 February 2020 23:28 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/18 "2020-02-09T23:28:00Z")

</div>

> [@BartButenaers](#):
>
> This means you can publish it in Dockerhub around 2024

> [@dceejay](#):
>
> maybe by 20:24 tonight

> [@dceejay](#):
>
> pushed it to [Docker](https://hub.docker.com/r/nodered/node-red-dev)

...and `dev` will be merged to `stable` in 2024...

You should be a politician @dceejay😆

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 February 2020 23:52 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/19 "2020-02-09T23:52:10Z")

</div>

> [@Paul-Reed](#):
>
> and `dev` will be merged to `stable` in 2024...

Is that a request...? We can make that the plan of record if you like...

---

<div class="post-metadata">

**Author:** ![Yuji09](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yuji09/32/29490_2.png) [@Yuji09](https://discourse.nodered.org/u/Yuji09)\
**Post date:** [20 March 2020 13:58 UTC](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370/20 "2020-03-20T13:58:38Z")

</div>

Hi, this is great work!

I wonder how I can start the debug from my docker-compose.yml, instead of docker run. I am running different containers in my RPI and node-red is one of them. I am no expert in this.

Cheers

[Next page](https://discourse.nodered.org/t/debugging-nodejs-in-node-red-docker-container/21370.md?page=2)
