Troubleshooting
Node-RED MQTT stuck on connecting inside a Docker container
Short answer
A Node-RED MQTT node stuck on connecting means no session was ever established: the transport connection to the server address and port never completed, or the broker declined the client. Test name resolution and routing from inside the container, then set the server to the broker's IP address and the port of its active listener. The controller's own IP works only when a broker actually listens there.
On this page
- Why does an MQTT node stay on connecting forever in Node-RED?
- Why does a successful ping from the host prove nothing about the container?
- Is getaddrinfo EAI_AGAIN the same fault as endless connecting?
- Which server address and port belong in the MQTT broker configuration?
- Example: does a container at 172.17.0.2 reach a broker at 192.168.10.40?
- What do the Node-RED and broker logs show during one deploy?
- How do you verify the repair end to end?
- Questions asked
An MQTT node that sits on connecting forever inside a Docker container is telling you one thing: no session was ever established. Either the transport connection to the configured server address and port never opened, or the broker declined the client. The fastest split is to enter the broker's IP address and the port of its active listener, deploy once, and watch: a node that connects by IP leaves container DNS as the remaining fault, while a node that still will not connect has cleared DNS and points at the broker address, port, listener mode or broker-side configuration. Every test below runs from inside the container, because that is the only vantage point that counts.
Why does an MQTT node stay on connecting forever in Node-RED? #
The node reports connecting because the MQTT client never completed a session with the broker: either the TCP connection to the server address and port never opened, or the broker refused the client, and the node simply keeps retrying in a loop. The MQTT specification, OASIS MQTT 3.1.1, also published as ISO/IEC 20922, requires a transport connection first and then a CONNECT packet that the broker answers with CONNACK. Until a CONNACK returns there is no session and no subscription; the client retries on its own schedule, and that retry loop is the yellow connecting status you watch. It is not proof of a Docker defect, and unless the broker log says otherwise it is not a credentials problem either, because many of these nodes never get far enough for credentials to matter.
Work the path bottom-up, since each stage has its own test and its own failure signature:
| Stage | What must succeed | What the failure looks like |
|---|---|---|
| Container interface | A usable address, gateway and route | Pings to outside addresses fail from inside the container |
| Name resolution | The broker hostname resolves to an address | getaddrinfo EAI_AGAIN in the Node-RED log |
| Transport | The server address and port reach a listening broker | The node sits on connecting with no error |
| MQTT session | The broker accepts the client's settings | The connection arrives and is closed; the reason is in the broker log |
| Flow output | A subscribed message reaches the debug node | Connected, but the debug panel stays empty |
Why does a successful ping from the host prove nothing about the container? #
The container carries its own virtual interface, its own resolver and its own route outward, so a successful ping from the host operating system proves only the host's path — run every test from inside the Node-RED container instead. Attach to the container, opening a shell in it with the container-management tooling you already use on that host to start or inspect it, and work from that prompt. A ping to an outside hostname from inside proves resolution and outbound routing in one shot. A ping to the broker's IP from inside proves IP reachability without touching DNS. Neither ping proves a broker is listening on the configured port or accepting sessions; that is two layers up.
Is getaddrinfo EAI_AGAIN the same fault as endless connecting? #
No: getaddrinfo EAI_AGAIN is a temporary DNS failure — the container asked a resolver for a hostname's address and got no answer — while a node stuck on connecting can occur with or without DNS, because an IP address skips resolution entirely. Inside containers the usual causes are a host network that changed after the container started, a DNS server that is overloaded or unreachable from the container, or resolver settings that do not fit the plant network. Fix it in this order:
- If the host's network changed after the container started, stop and restart the container; that recreates its network and resolver state and clears the error whenever stale state is the cause. Retest name resolution from inside.
- If the name still does not resolve, start the container with an explicit resolver — appending
--dns 8.8.8.8to the container start line is the common quick test, valid only where that resolver is reachable from your segment. - If the node then connects by hostname, keep the resolver fix and record it. If you would rather remove the dependency, put the broker's IP address in the node and repair DNS on its own schedule.
The same restart-then-explicit-resolver sequence is worked through in clearing an EAI_AGAIN DNS resolution failure on an industrial controller.
Which server address and port belong in the MQTT broker configuration? #
Use the IP address of the machine that actually runs the broker and the port of its active listener — the controller's own IP is correct only when a broker really listens there, and the node stays on connecting forever when it does not. Do not enter localhost or 127.0.0.1 for a broker running on the host: inside the container that address is the container itself, the same trap behind Telegraf MQTT connecting to localhost in Docker.
- Identify the machine that actually runs the broker and write down three things from its configuration: the IP address, the port of the active listener, and the listener's mode, plain or TLS. A plain MQTT listener conventionally uses port 1883, but the configured value always wins — a listener on a moved port is exactly the mismatch that keeps a client connecting forever.
- From inside the container, ping the broker's IP. If that fails, repair the container's interface, gateway, route or the filtering between the two before touching any MQTT setting.
- If the design needs a hostname, test resolution from inside the container separately and apply the EAI_AGAIN fixes above when it fails.
- In Node-RED, open the MQTT broker configuration and set the server field to the broker's IP address for the diagnostic connection.
- Match the port and the connection mode to the active listener. When the listener accepts only TLS, a client offering plain MQTT sits on connecting or is rejected — the failure covered in Node-RED MQTT failing when the broker rejects plain MQTT.
- Add credentials only when the broker's listener requires them — no username, password or security mode the listener does not use — then deploy once and read the status.
This is the same move that clears an MQTT sink that stays offline until you use the broker IP address: the client points at the machine that runs the broker, and the name-resolution question drops out of the diagnosis.
Example: does a container at 172.17.0.2 reach a broker at 192.168.10.40? #
No direct route exists: the two addresses sit on different networks, so every packet leaves through the container's gateway 172.17.0.1 — and the test that proves it is a ping to 192.168.10.40 run from inside the container, not from the host. These are hypothetical inputs for a small line network; read the real values from the running container and the broker configuration, then repeat the arithmetic with them. The container holds 172.17.0.2/16 and the broker listens at 192.168.10.40 with a /24.
- The broker's /24 fixes 24 of 32 bits and leaves 32 − 24 = 8 host bits: 2^8 = 256 addresses, of which 256 − 2 = 254 are usable, running 192.168.10.1 through 192.168.10.254. The broker sits inside that range, on network 192.168.10.0.
- The container's /16 leaves 16 host bits: 2^16 = 65,536 addresses, 65,534 usable, on network 172.17.0.0.
- Same-network test: apply the container's own mask to the destination — the destination AND 255.255.0.0 yields 192.168.0.0, which is not the container's network 172.17.0.0. The broker is off-subnet, so the packet goes to the gateway first.
Three outcomes, three decisions:
- A reply to the ping proves interface, gateway, routing and the broker host in one test: move up a layer, test the listener port, then the MQTT settings.
- No reply from the container while the host's own ping to the broker succeeds pins the fault on the container's network path — restart it and check its gateway and route; DNS is innocent and the broker is untouched.
- No reply from either side is a broker-host or physical-path fault that no Node-RED setting will fix.
What do the Node-RED and broker logs show during one deploy? #
The client side and the broker side tell different halves: the Node-RED log names resolution and transport failures, while the broker log shows whether your connection arrived at all and why it was refused — read both during the same deploy attempt. A getaddrinfo EAI_AGAIN entry on the client side means the request never left the resolution stage. A connection timeout or refused entry means resolution worked and the transport failed. On the broker side, no arrival at deploy time means the packets never got there, an addressing, routing or filtering fault, while a connection that arrives and is immediately closed carries the broker's own reason, usually a mode or credential mismatch.
One exception sorts itself out: when a plain MQTT node connects while your Sparkplug node stays on connecting, the network is proven good and the fault sits in the Sparkplug session — the case covered in a Sparkplug node stuck on connecting while MQTT connects.
How do you verify the repair end to end? #
Verify in three checks: the node's status changes from connecting to connected, a known test value published to the exact subscribed topic arrives, and it appears exactly once in the debug panel with the expected topic and payload.
- Deploy once and confirm the status changes from the yellow connecting state to connected, and holds there for a few minutes.
- Publish a known test value — the number 42 works — to the exact topic the node subscribes to, through the same broker.
- Confirm the value appears once in the debug panel with the topic and payload you expect.
One caution belongs here: publish the test value only to a dedicated test topic that no PLC or machine subscribes to, because a payload on a live command topic is a live command. If the node connects by IP but not by hostname, container DNS is the remaining fault — finish the resolver fix from earlier. If it connects but the debug panel stays empty, the session is fine and the topic is wrong: compare the publisher's and subscriber's strings character by character and confirm the publisher targets the same broker, the chase described in fixing MQTT sensor data missing in Node-RED. Decide by what the node does, not by what you changed: connected, with one matching payload in the debug panel, is the only state that closes the call.
Questions asked
Can I enter 127.0.0.1 when the broker runs on the host beside the container?
No. Inside the container, 127.0.0.1 is the container itself, so the node searches for a broker that is not there and stays on connecting. Enter the host's own LAN address instead, the address other machines use to reach it, or run the broker as a container on a shared Docker network and use that container's name, which Docker's built-in resolution handles.
Does a firewall between the container and the broker cause the same endless connecting state?
Yes, and it is easy to mistake for a broker fault. A firewall that silently drops the connection attempt gives the client nothing to report, so it retries and the node stays on connecting; a device that actively rejects usually puts an error in the Node-RED log within seconds. Open the broker's listener port from the container's subnet to the broker before changing the client again.
Is it safe to point the container at 8.8.8.8 on a plant network?
Only when the segment allows resolver traffic out, since DNS uses UDP and TCP port 53, and many OT networks block public resolvers by policy. If the query is dropped, resolution fails exactly as before. On an isolated plant network, hand the container the address of the site's internal resolver with the same --dns option instead; the option changes only which resolver is asked, never how the MQTT session behaves.
Why does the node show connected while the debug panel stays empty?
The session is fine, so look at the topic. MQTT topics are case-sensitive and each slash starts a new level, so a subscription to plant/line1/oven differs from Plant/Line1/Oven and from /plant/line1/oven. Compare the publisher's and subscriber's strings character by character, and confirm the publisher is sending through the same broker the node is subscribed to.
Is restarting the whole controller faster than restarting the container?
No. What clears stale network state is recreating the container's network and resolver, which a container stop and start does in seconds without touching the controller's other tasks. A controller reboot also recreates the container, but only after interrupting everything else running there, and neither restart repairs a wrong server address, port or listener mode.