# Several node-red containers are reported as unhealthy by docker

**URL:** <https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152>\
**Category:** General\
**Tags:** docker\
**Created:** [13 August 2024 15:56 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152 "2024-08-13T15:56:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 15:56 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/1 "2024-08-13T15:56:47Z")

</div>

Lately I noticed that several of my node-red containers are reported as unhealthy by docker.

This is the inspect output for such a container

```auto
{
    "Id": "b282259f2feb6bbbe6e46ca6a56efa25f24c25d152cd2601d236740d7b26fbf0",
    "Created": "2024-08-12T14:30:07.063943378Z",
    "Path": "./entrypoint.sh",
    "Args": [],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 3078,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2024-08-13T11:53:54.875344403Z",
        "FinishedAt": "2024-08-13T11:53:00.02949769Z",
        "Health": {
            "Status": "unhealthy",
            "FailingStreak": 478,
            "Log": [
                {
                    "Start": "2024-08-13T17:53:04.336514692+02:00",
                    "End": "2024-08-13T17:53:04.606846869+02:00",
                    "ExitCode": 1,
                    "Output": ""
                },
                {
                    "Start": "2024-08-13T17:53:34.607796346+02:00",
                    "End": "2024-08-13T17:53:34.917576805+02:00",
                    "ExitCode": 1,
                    "Output": ""
                },
...
        "Healthcheck": {
            "Test": [
                "CMD-SHELL",
                "node /healthcheck.js"
            ]
        },
...

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 15:59 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/2 "2024-08-13T15:59:47Z")

</div>

FYI the contents of the healtcheck.js

```auto
b282259f2feb:/$ cat healthcheck.js 
var http = require('http');
var https = require('https');
var settings = require('/data/settings.js');
var request;

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

var options = {
    host : "localhost",
    port : settings.uiPort || 1880,
    timeout : 4000
};

if (settings.hasOwnProperty("https")) {
    request = https.request(options, (res) => {
        //console.log(`STATUS: ${res.statusCode}`);
        if ((res.statusCode >= 200) && (res.statusCode < 500)) { process.exit(0); }
        else { process.exit(1); }
    });
}
else {
    request = http.request(options, (res) => {
        //console.log(`STATUS: ${res.statusCode}`);
        if ((res.statusCode >= 200) && (res.statusCode < 500)) { process.exit(0); }
        else { process.exit(1); }
    });
}

request.on('error', function(err) {
    //console.log('ERROR',err);
    process.exit(1);
});

request.end(); b282259f2feb:/$ 

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 16:05 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/3 "2024-08-13T16:05:28Z")

</div>

I have uncommented the log statement at the end in the healtcheck.js and then manually executed the healthcheck in the container. This is what I get:

```auto
b282259f2feb:/tmp$ node check.js 
ERROR Error: connect ECONNREFUSED ::1:1880
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 1880
}
b282259f2feb:/tmp$ 

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 16:09 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/4 "2024-08-13T16:09:37Z")

</div>

If I replace `localhost` by `127.0.0.1` the healthcheck is working.  
So the problem is that it is using ipv6 address `::1` for localhost while it is only listening for ipv4 addresses:

```auto
b282259f2feb:/tmp$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State       
tcp 0 0 localhost:40789 0.0.0.0:* LISTEN      
tcp 0 0 0.0.0.0:1880 0.0.0.0:* LISTEN  
...

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 16:24 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/5 "2024-08-13T16:24:04Z")

</div>

I don't have this for all my node-red containers when comparing the only difference is that localhost is not mapped to ipv6 address "::1" when it is working.

So this is from a container having the problem:

```auto
82259f2feb:~$ node --version
v18.18.2
b282259f2feb:~$ uname -a
Linux b282259f2feb 6.5.0-44-generic #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 7 15:10:09 UTC 2024 x86_64 Linux
b282259f2feb:~$ 

```

This is from a container not having the problem:

```auto
bash-5.1$ node --version
v16.16.0
bash-5.1$ uname -a
Linux bd6ae5207f57 6.5.0-44-generic #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 7 15:10:09 UTC 2024 x86_64 Linux
bash-5.1$ 

```

So the problem seems to be caused by more recent node version.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 16:37 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/6 "2024-08-13T16:37:55Z")

</div>

Problem got introduced by node version 17

> <https://github.com/nodejs/node/issues/40702>
>
> \### Version
> 
> v17.0.1
> 
> \### Platform
> 
> Darwin Kernel Version 21.1.0: Wed Oct …13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE\_X86\_64 x86\_64
> 
> \### Subsystem
> 
> \_No response\_
> 
> \### What steps will reproduce the bug?
> 
> I'm trying to connect to a micro-service with this piece of code:
> 
> \`\`\`Javascript
> const connect = (host, port) =\> {
> socket = new require('net').Socket();
> socket.connect(port, host)
> .on('connect', () =\> console.log('konected!'))
> .on('error', e =\> console.log(e));
> };
> 
> connect('localhost', 5098);
> \`\`\`
> 
> this prints out:
> \`\`\`bash
> Error: connect ECONNREFUSED ::1:5098
> at TCPConnectWrap.afterConnect \[as oncomplete\] (node:net:1161:16)
> at TCPConnectWrap.callbackTrampoline (node:internal/async\_hooks:130:17) {
> errno: -61,
> code: 'ECONNREFUSED',
> syscall: 'connect',
> address: '::1',
> port: 5098
> }
> \`\`\`
> 
> 
> \### How often does it reproduce? Is there a required condition?
> 
> Since installed node 17
> 
> \### What is the expected behavior?
> 
> No errors.
> 
> \### What do you see instead?
> 
> \`\`\`bash
> Error: connect ECONNREFUSED ::1:5098
> at TCPConnectWrap.afterConnect \[as oncomplete\] (node:net:1161:16) {
> errno: -61,
> code: 'ECONNREFUSED',
> syscall: 'connect',
> address: '::1',
> port: 5098
> }
> \`\`\`
> 
> \### Additional information
> 
> As far a i tested (v14.18.1 and v16.12.0), this only happens in this version of node.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [13 August 2024 19:37 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/7 "2024-08-13T19:37:46Z")

</div>

The problem got fixed by upgrading node-red to docker image `nodered/node-red:4.0.2-22`  
as this is using node version v22.6.0 which doesn't have this problem.

```auto
Welcome to Node-RED
===================

13 Aug 21:37:01 - [info] Node-RED version: v4.0.2
13 Aug 21:37:01 - [info] Node.js version: v22.6.0
13 Aug 21:37:01 - [info] Linux 6.5.0-44-generic x64 LE

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 August 2024 19:38 UTC](https://discourse.nodered.org/t/several-node-red-containers-are-reported-as-unhealthy-by-docker/90152/8 "2024-08-27T19:38:38Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
