IMD SolutionComputing LibraryShop main store

Troubleshooting

Fix Mosquitto connection refused from Telegraf in Docker

Edge computing and IIoT Published 8 min read

Short answer

Telegraf reached Mosquitto and was refused on port 1883, which means the broker had no usable listener; started alone, it logged Address not available. Adding listener 1883 with socket_domain ipv4 to the mosquitto.conf the container actually loads and restarting the broker fixed the dial. Keep servers at tcp://mosquitto:1883; the EOF on the host route is a separate failure, not a name problem.

On this page
  1. Read the two error strings before touching the servers setting
  2. Why tcp://mosquitto:1883 is the right address on a shared network
  3. Working the 172.28.0.3:1883 refusal in this stack
  4. Reproduce the bind failure before editing the config
  5. Add listener 1883 and socket_domain ipv4 to the active mosquitto.conf
  6. Read the EOF on the host route as a different failure
  7. Keep the repaired listener off the open plant network
  8. Questions asked

network Error : dial tcp 172.28.0.3:1883: connect: connection refused answers the first question before any config is opened: Docker resolved the service name mosquitto to 172.28.0.3, so name resolution worked, and nothing accepted the TCP connection on port 1883 at that address. The Mosquitto container in this Compose stack had no usable listener — run on its own, it failed to bind at startup and logged Address not available. The fix that restored the connection is two lines in the mosquitto.conf the container actually loads, listener 1883 and, directly under it, socket_domain ipv4, followed by a broker restart. Keep Telegraf's servers setting at tcp://mosquitto:1883; the name and the port were never wrong. The session's second error, network Error : EOF on the docker.for.mac.localhost route, is a separate failure on the host route, not a DNS problem.

Read the two error strings before touching the servers setting #

The mqtt_consumer input in Telegraf 1.20.4, running on /etc/telegraf/telegraf.conf in this stack, builds one connection path and fails it in order: resolve the host, open TCP, complete the MQTT CONNECT, subscribe, then parse and forward messages. A refusal at the dial stops the attempt at the TCP stage, before credentials, topics, QoS or data_format parsing can matter. Place each reported error on that path:

  1. Container network path — telegraf and mosquitto share the default Compose network.
  2. Name resolution — mosquitto resolved to 172.28.0.3, so this stage passed.
  3. TCP connection — connect: connection refused stops here.
  4. MQTT CONNECT, authentication, subscription — never reached.
  5. Parsing and output — downstream, untouched by this fault.

The three lines that decide the case:

Log line Stage reached Decision
network Error : dial tcp 172.28.0.3:1883: connect: connection refused Name resolved, TCP refused The listener is missing or unbound; repair the broker
Address not available in the Mosquitto startup log Broker socket bind The configured listener never bound; fix mosquitto.conf
network Error : EOF on the host route Attempt ended in a stream closure Different failure; correlate both logs at the attempt time

Refusal normally means no socket was listening; an active rejection somewhere on the path can produce the same text, which is why the broker's own startup log is the tiebreaker. One cheap check comes before any edit: depends_on in docker-compose.yml starts containers in order but does not wait for the broker process to accept connections, so a dial that lands during broker startup is refused. The telegraf service here carries the always restart policy, so such an attempt retries by itself; a refusal that persists once the broker has been up for a few seconds is not a start-order problem.

Why tcp://mosquitto:1883 is the right address on a shared network #

With telegraf and mosquitto declared in the same docker-compose.yml and no separate networks defined, both containers join one default network, and Docker's service-name resolution takes tcp://mosquitto:1883 straight to the broker's container-side port. The ports entry "1883:1883" serves a different purpose: it forwards host-side 1883 into container-side 1883 for clients outside Docker, and publishing a port never creates a listener inside the broker — the two are independent.

Aiming servers at localhost instead would point Telegraf at its own container, the failure covered in the fix for Telegraf MQTT connecting to localhost in Docker. A name that fails to resolve produces a different symptom entirely — that is the MQTT sink that stayed offline until the broker's IP address was used. Neither applies here: the dial error already prints a resolved address, so the servers string is not the fault.

Working the 172.28.0.3:1883 refusal in this stack #

Inputs, each from the reported stack:

  • Dial target: servers = ["tcp://mosquitto:1883"] in telegraf.conf at /etc/telegraf/telegraf.conf.
  • Resolved address: 172.28.0.3, printed in the 06:21:46Z Telegraf error line.
  • Port mapping on the mosquitto service: "1883:1883" in docker-compose.yml — left side host, right side container.
  • Broker result when started on its own: Address not available, from the reported test.

Rule: split the dial error into destination, port and TCP result. An IP address in the error means resolution passed; connect: connection refused means no socket was accepting at that address and port at that moment.

Arithmetic:

  • Container-side port from the mapping: right side of 1883:1883 = 1883.
  • Port Telegraf dialed: 1883.
  • 1883 = 1883, so the port number is consistent end to end and nothing in the URL needs changing.
  • Broker bind result: Address not available — the listener never came up, so the correct port had no owner.

Result: name correct, port correct, listener absent.

Decision: repair the listener, not the URL — add listener 1883 with socket_domain ipv4 to the active mosquitto.conf and restart the broker. The threshold: any dial error that names an IP address puts the next fix on the broker side, so leave servers at tcp://mosquitto:1883.

Reproduce the bind failure before editing the config #

  1. Record what eclipse-mosquitto:latest actually is: read the version from the running container, because the latest tag moves and the fix should state the version it was applied to.
  2. Run the broker on its own, without Telegraf, so its startup messages stay visible.
  3. Capture the failure: Address not available during broker startup means the broker could not bind an address its configuration or environment required — the listener never opened.
  4. Identify the configuration file the container really loads. The Compose file in this stack mounts only mosquitto-data at /mosquitto/data and mounts no configuration file, so a mosquitto.conf edited on the host may not be the file in use; take the config path from the container's command or startup log before editing anything.
  5. Keep the host platform in the notes, not in the diagnosis: this stack ran on macOS on Apple silicon, and the actionable failure was broker socket setup, which the platform does not change.

Add listener 1883 and socket_domain ipv4 to the active mosquitto.conf #

  1. Open the active mosquitto.conf — the file identified in the previous section — and list the listener lines already present. Adding socket_domain ipv4 while two listeners already share a port has produced Address already in use and a broker that refuses to start, so remove or merge duplicates first.
  2. Add the two lines, listener first: listener 1883, then socket_domain ipv4 on the next line, because listener-scoped settings in mosquitto.conf belong to the listener line above them.
  3. Make the edit persistent: mount the configuration into the container or bake it into the image, then recreate the container, so the file the broker loads at startup is the edited one.
  4. Restart the broker and read the startup log. Success looks like Opening ipv4 listen socket on port 1883. with no bind error. A broker that exits outright at startup is a separate failure pattern; Mosquitto broker exit code 3 fix follows that path step by step.
  5. Test through the exact path Telegraf uses: subscribe with mosquitto_sub -t test, publish with mosquitto_pub -t test -m hi from a client on the same network, and confirm delivery.
  6. Restart Telegraf on servers = ["tcp://mosquitto:1883"] and confirm the inputs.mqtt_consumer dial error is gone from the log.

Once the listener binds, every MQTT client on that network depends on the same broker-side facts — reachability, listener settings and access control — which is what S7-1500 MQTT troubleshooting for a Mosquitto broker covers from a PLC client's side.

Read the EOF on the host route as a different failure #

The session's second attempt replaced the service name with tcp://docker.for.mac.localhost:1883 and returned network Error : EOF. Treat it as a different symptom, not progress toward the same diagnosis: EOF reports that the attempt ended in an unexpected stream closure, and the message alone does not identify which endpoint closed the connection or why — it is not evidence that MQTT CONNECT, credentials or subscription succeeded. That route also adds dependencies the direct path does not have: the host endpoint, Docker's port forwarding on the desktop platform, and the published mapping must all cooperate before a packet reaches the broker's listener.

Correlate the attempt with the broker log: a connection record at the same timestamp moves the investigation to access policy or protocol settings; no record means the attempt never reached the broker as an MQTT connection. Decision: keep container-to-container traffic on the service name, and use a host route only when the broker genuinely runs on the host. In this session the EOF was never the real fault — the broker had no listener on any path, which the refusal had already shown.

Keep the repaired listener off the open plant network #

listener 1883 with no authentication after it accepts connections and subscriptions from any client that reaches it. The Telegraf config in this stack subscribes to #, so a writable, readable 1883 on a plant network lets any peer inject values into the pipeline or read everything the broker delivers — an OT security problem, not a tuning detail. Configure authenticated access and topic permissions before the broker serves more than its acquisition network; a working pattern is the authenticated Mosquitto lab broker with Node-RED.

Expect a defined order of failures afterwards: once a listener requires TLS, plain clients are rejected outright — the separate case of Node-RED MQTT failing when the broker rejects plain MQTT. TLS lives in the listener as well, each listener carrying its own certificate settings, as TLS 1.3 listener configuration for the HiveMQ broker shows.

Decision for this stack: bind the listener on IPv4, keep servers on the service name, verify with a publish and subscribe through the Telegraf path, and only then widen access — authenticated, and scoped to the acquisition network.

Questions asked

Does depends_on guarantee Mosquitto is ready before Telegraf dials?

No. Compose starts dependent containers in order but does not wait for the broker process to accept connections, so a Telegraf dial during broker startup is refused. In this stack the Telegraf service used the always restart policy, so such an attempt is retried and succeeds once the listener binds. If refusal persists after the broker has been up, readiness is not the cause; check the listener configuration.

What does Address already in use on port 1883 mean?

Another socket already owns the port. Two causes appear with Mosquitto: a configuration that defines two listeners on the same port, which can surface when socket_domain ipv4 is added alongside existing listener lines, and another process holding 1883 on the host before the container starts. Remove the duplicate listener or stop the other process, then start the broker and read the startup log.

Does socket_domain ipv4 disable IPv6 for the whole broker?

No. The setting selects an IPv4 socket for the listener line it follows and leaves other listeners untouched. It earned its place here because the default bind failed with Address not available; pinning that listener to IPv4 fixed the socket setup. If a dual-stack listener already binds cleanly on your host, you do not need the setting at all.

Should the eclipse-mosquitto:latest tag be pinned?

Yes, for reproducibility. Record the actual version behind the latest tag when you apply the listener fix, pin that version in docker-compose.yml, and upgrade deliberately, checking the startup log for the listener line after each upgrade. An unpinned image can change defaults under a running acquisition stack without any edit on your side.

Does the same fix apply when Telegraf runs on the host and Mosquitto in Docker?

Then the address changes: a host-side Telegraf reaches the broker through the published port mapping, so servers names the host address and the host-side port rather than the service name. The listener repair is identical, because the mapping forwards into the container-side port, which only accepts traffic once Mosquitto has bound it.

Keep reading

  • Troubleshooting

    Fix Telegraf MQTT connecting to localhost in Docker

    Point the Telegraf MQTT consumer at the Compose service name tcp://hivemq:1883, not tcp://localhost:1883. Each container has its own network namespace, so localhost inside Telegraf is Telegraf itself and the connection to port 1883 never reaches the broker container. Service-name resolution on the Compose network is what restored sensor capture in a four-container stack.

    Edge computing and IIoT

  • Troubleshooting

    Fix a Mosquitto MQTT TLS listener that returns unknown error

    Client null sending CONNECT followed by Unknown error is a TLS handshake failure, not an MQTT fault. Prove the transport with openssl s_client, confirm the Mosquitto listener loaded its certificate, then give the client your self-signed CA in PEM and make the certificate SAN match the host you dial. tls_version defaults to both TLS 1.2 and 1.3 and is not the cause.

    Edge computing and IIoT

  • Troubleshooting

    Fix a PLC MQTT client that never reaches your broker

    Most PLC MQTT failures sit in one of three stages: the broker listener, the network path to the port the client is actually configured for, or the client's own publish logic. Test in that order; a connect timeout points to the first two, a silent publish to the third.

    Edge computing and IIoT