Trouble connecting to Home assistant (docker)

Hi,

I am at my wits end trying to get Node-RED connected to Home Assistant server. I have HA and NR running in docker containers on a NUC.
These are the docker-compose.yml files for both:

version: "3"
services:
  homeassistant:
    container_name: homeassistant
    image: homeassistant/home-assistant:stable
    volumes:
      - ./config:/config
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock
    devices:
      # Conbee II Zigbee device on USB
      - /dev/ttyACM1:/dev/ttyACM1
    restart: unless-stopped
    network_mode: host
version: "3.7"
services:
  node-red:
    container_name: nodered
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Brussels
    ports:
      - "1880:1880"
    restart: unless-stopped
    volumes:
      - ./data:/data

Node-RED version:

nodered     | 30 Oct 14:03:23 - [info] Node-RED version: v2.1.3
nodered     | 30 Oct 14:03:23 - [info] Node.js  version: v14.18.1
nodered     | 30 Oct 14:03:23 - [info] Linux 5.11.0-38-generic x64 LE

Whith this configuration, NR tries to connect to HA without success:

nodered     | 30 Oct 14:03:24 - [info] Starting flows
nodered     | 30 Oct 14:03:24 - [info] Started flows
nodered     | 30 Oct 14:03:24 - [info] [server:Home Assistant] Connecting to http://192.168.2.60:8123

As advised by some in the HA forums, I changed the NR docker-compose.yml as follows:

version: "3.7"
services:
  node-red:
    container_name: nodered
    image: nodered/node-red:latest
    environment:
      - TZ=Europe/Brussels
#    ports:
#      - "1880:1880"
    restart: unless-stopped
    volumes:
      - ./data:/data
    network_mode: host

Doing so, NR connects to HA:

nodered     | 30 Oct 14:08:57 - [info] Server now running at http://127.0.0.1:1880/
....
nodered     | 30 Oct 14:08:58 - [info] Starting flows
nodered     | 30 Oct 14:08:58 - [info] Started flows
nodered     | 30 Oct 14:08:58 - [info] [server:Home Assistant] Connecting to http://192.168.2.60:8123
nodered     | 30 Oct 14:08:58 - [info] [server:Home Assistant] Connected to http://192.168.2.60:8123

Great, problem solved :grinning: ! Well, no...

In this configuration, I cannot connect anymore to NR via the 1880 port...
But the port seems to be up and listening...

$ sudo lsof -i :1880
COMMAND     PID    USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
node-red 429417 jiheffe   19u  IPv4 1682621      0t0  TCP *:1880 (LISTEN)

What am I missing here ?

Did you try it without the double quotes ? These are numbers, not strings.

Well, actually I found that single quotes, double quotes or even no quotes around the ports do not make any difference...

Whern I use the

    ports:
      - "1880:1880"

statements, I can connect to NR in the web interface, but I can't connect NR to the HA server.

Whern I use the

    network_mode: host

statement, NR connects to HA (according to the logs) but I can't connect to the NR web interface at 1880...

I see for docker-compose the ports are actually in a string for some reason (never noticed that before weird).
Then I don't know, you may have better luck in the HA forums/discord, not too many HA users here afaik.

My NR initiates the outbound connection to HA's bus, not the other way round. You can add the HA nodered integration (Not to be confused with the NodeRed add-on) but only if you want to publish direct from NR to HA.
I have HA on port 8123 (Default) and NR on 32771:1880 I also have 3456:3456 and 8009:8009 on the NR container.
For some reason, 1880 did not work on my Synology NAS hosted Docker. Ohh, also the network is in 'bridge' not 'host' mode.

image

When you took out the port mapping from your compose file, you effectively left docker to pick a random set of inbound ports. Try 21771:1880
There is no secret sauce with 21771, but it's much less likely to be already in use..
then try to connect to NR on that port - so | http://ipaddress:21771

Thank you for all your suggestions :slightly_smiling_face:

Actually, I do not really have a problem with the 1880 port to access the NR web interface when I use

    ports:
      - "1880:1880"

In that situation, I have no problem to get into the NR web interface. BUT, NR does't connect to the HA server.

On the other hand, when I use

    network_mode: host

NR does connect to the HA server, BUT is not accessible at port 1880 via the web interface.

Both options are mutually exclusive, i.e. you cannot have both the ports: and network_mode: directives at the same time.

So for me the issue is not related to HA but rather to Docker and/or NR... and probably to my rather rookie knowledge of docker :wink:

Update:
I can confirm this is not an issue with Home Assistant, but rather with Node-RED and/or Docker.
I configured an MQTT broker in Node-RED, and I am having exactly the same behaviour there.

Case 1 - with ports: statement

    ports:
      - "1880:1880"
  • I can connect to Node-RED on port 1880
  • Node-RED can NOT connect to either HA or MQTT broker
nodered     | 31 Oct 11:01:28 - [info] Server now running at http://127.0.0.1:1880/
nodered     | 31 Oct 11:01:28 - [info] Starting flows
nodered     | 31 Oct 11:01:29 - [info] Started flows
nodered     | 31 Oct 11:01:29 - [info] [server:Home Assistant] Connecting to http://192.168.2.60:8123
nodered     | 31 Oct 11:01:59 - [info] [mqtt-broker:Mosquitto MQTT] Connection failed to broker: http://192.168.2.60

Case 2 - with network_mode: statement

    network_mode: host
  • I can NOT connect to Node-RED on port 1880
  • Node-RED does connect to HA and MQTT broker
nodered     | 31 Oct 11:06:30 - [info] Server now running at http://127.0.0.1:1880/
nodered     | 31 Oct 11:06:30 - [info] Starting flows
nodered     | 31 Oct 11:06:30 - [info] Started flows
nodered     | 31 Oct 11:06:30 - [info] [server:Home Assistant] Connecting to http://192.168.2.60:8123
nodered     | 31 Oct 11:06:30 - [info] [mqtt-broker:Mosquitto MQTT] Connected to broker: http://192.168.2.60
nodered     | 31 Oct 11:06:30 - [info] [server:Home Assistant] Connected to http://192.168.2.60:8123

Continuing investigation :upside_down_face:

That's why the bridge suggestion was made, as ports will only be discarded in host mode.

I tried this, but it makes no difference...
Quite new to Docker, still trying to get the hold on networking options...

Did you actually recreate the containers after the modifications of the docker-compose.yml ? I would suggest to use bridge for HA as well.

Home assistant needs to run in host networking mode for discovery and a lot of the integrations to work properly. Many of the different integrations use a variety of different ports. The docker install documentation for home assistant also says to use host networking mode.

Some get by with bridge mode fine for home assistant if they don't need discovery or aren't using an integration that needs it. While this could fix the problem by putting both containers in the same docker network, it could create other problems.

1 Like