Home > Software engineering >  Connection Errors in Node Red in a 'Docker for Windows' environment
Connection Errors in Node Red in a 'Docker for Windows' environment

Time:10-28

I've been trying to migrate / replicate my various setup docker containers ('Node Red', MongoDB and MySQL) from my NAS with a Linux distribution (where they are are nicely running) to my Windows 10 machine (with way stronger hardware) where I've setup WSL and 'Docker for Windows'.

The setup of WSL and 'Docker for Windows' was straightforward and the "Getting-Started" docker example (via command docker run --name repo alpine/git clone https://github.com/docker/getting-started.git) was running nicely.

I've then pulled a MySQL and MongoDB docker image into the windows docker environment, made sure they have their specific data paths and backupped & restored both databases from the original environment. I have both containers started and running successfully (in my 'Windows for Docker' environment) by dropping the following commands in a PowerShell (with admin rights):

MySQL:

docker run -p 3306:3306 -p 33060:33060 -v C:\Docker_Data\MySQL:/var/lib/mysql --network="bridge" --add-host localhost:127.0.0.1 --name MySql -e MYSQL_ROOT_PASSWORD=mySecretPW -d mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

MongoDB:

docker run -p 27017:27017 -v C:\Docker_Data\MongoDB:/data/db --network="bridge" --add-host localhost:127.0.0.1 --name mongoDB -e MONGO_INITDB_ROOT_USERNAME=MongoAdmin -e MONGO_INITDB_ROOT_PASSWORD=mySecretPW -d mongo:latest --wiredTigerCacheSizeGB 1.0

Node-Red itself could be pulled and then started nicely as well via:

docker run -d -p 1880:1880 -v C:\Docker_Data/Node-Red:/data --network="bridge" --add-host localhost:127.0.0.1 --name NR-Latest-14 --restart unless-stopped --link mongoDB --link MySql nodered/node-red:latest-14

Opening the Node-Red backend on localhost:1880 works well and I could manage to install required plugins nicely. Importing my existing flows.json then brought in all various flows where mySQL and mongoDB nodes were brought in.

This is what my 'Docker for Windows Desktop' is showing: enter image description here

;TLDR Node Red flows fail both with mongoDB and mySQL connection errors as follows (but I am able to connect to both via database clients successfully):

MongoDB-Nodes:

MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: getaddrinfo ENOTFOUND mongodb at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:71:26) { name: 'MongoNetworkError' }]

MySQL-Nodes:

"Database not connected"

with the mySQL configuration node output:

"Error: connect ECONNREFUSED 127.0.0.1:3306"

enter image description here

The configuration nodes look as follows:

MongoDB:

enter image description here

MySQL:

enter image description here

(User 'node-red' has restricted priviledges; tested with a MySQL client and properly connects to the DB)

I've also added port rules for both inbound and outbound in my Windows Firewall for

  • MongoDB: 27017 (tcp)
  • MySQL: 3306, 33060 (tcp)

Inside the running Node-Red container the /etc/hosts file looks as follows:

127.0.0.1       localhost 
::1     localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix 
ff02::1 ip6-allnodes 
ff02::2 ip6-allrouters 
127.0.0.1       localhost  
172.17.0.3      mongoDB be6c7e97f98d
172.17.0.2      MySql bfa9a8206a20
172.17.0.4      cd7134e9ff59

                                                                

/etc/hosts [Readonly] 1/10 10%

The hosts file on my NAS where Node-Red is successfully running together with MongoDB and MySQL has the following content:

127.0.0.1       localhost ::1     localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
172.17.0.2      mySQL MySql_8.0.22 MySql
172.17.0.3      mongoDB mongoDB
172.17.0.4      Node-Red-Custom-N14

/etc/hosts [Readonly] 1/10 10%

I have the feeling it must be something super simple I've been overlooking all the time. Maybe you've got an idea what I could try out then please let me know.

CodePudding user response:

A few points that will help here:

  1. 127.0.0.1 always points to the network stack that the running code is attached to.
  2. Under normal circumstances (any time except when the network mode is not host) every running Docker container has it's own network stack. (Containers actually work using something called Network Namespaces)

This means that unless you have a MySQL instance running in the same container as Node-RED configuring the MySQL nodes configured to connect to either localhost or 127.0.0.1 will fail to connect.

You probably have 2 options for how to fix this.

  1. Since you are mapping the exposed ports from your containers to your host machine you can replace all the IP addresses with 172.17.0.1 which is the default IP address assigned to the docker0 bridge.

  2. Use Docker compose to manager all the containers as this will create DNS entries (in the internal Docker DNS service) that maps each containers IP address to it's name

  • Related