IMD SolutionComputing LibraryShop main store

Troubleshooting

Fix Telegraf MQTT connecting to localhost in Docker

Edge computing and IIoT Published 6 min read

Short answer

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.

On this page
  1. Why won't Telegraf connect to the MQTT broker at localhost in Docker?
  2. What address should the mqtt_consumer servers line use?
  3. How do you apply the fix and reload Telegraf?
  4. How long should a test point take to reach the dashboard?
  5. Which hop do you check when MQTT data still does not arrive?
  6. Does the published port 1883:1883 matter for Telegraf?
  7. When do the sensor publisher and Telegraf need different broker addresses?
  8. Questions asked

Telegraf cannot reach an MQTT broker at tcp://localhost:1883 from inside a container, because localhost is the Telegraf container's own loopback interface, not the broker. Point the MQTT consumer at the Compose service name, tcp://hivemq:1883, and the connection, the subscription and the capture come back. This article gives the address decision, the one-line change to telegraf.conf, a hop-by-hop check for stacks where the symptom survives the fix, and the timing a test point needs before you call the pipeline healthy.

Why won't Telegraf connect to the MQTT broker at localhost in Docker? #

Because Docker gives every container its own network namespace by default: inside the Telegraf container, localhost and 127.0.0.1 are that container's loopback interface, so the connection attempt to port 1883 targets Telegraf itself and never reaches the broker container running beside it.

The stack has four services in one Compose project — the HiveMQ broker, Telegraf, InfluxDB and Grafana — with an external sensor publishing to the broker. The data path is sensor, broker, Telegraf, InfluxDB, dashboard, and every arrow is a separate connection with its own destination. The fault sits at the second arrow: Telegraf dialed its own loopback, where nothing listens on 1883, so the MQTT session, the subscription to sensors/data and the JSON parsing never started.

Telegraf's log names the address the input dialed. A refused connection or a timeout that mentions localhost confirms the destination; containers on a shared Compose network reach each other freely once addressed correctly, so nothing needs to be unblocked.

What address should the mqtt_consumer servers line use? #

Use the Compose service name: servers = ["tcp://hivemq:1883"]. Compose attaches every service in a project to a shared default network and resolves service names on it, so hivemq resolves to the broker container and the TCP connection reaches the real listener.

Destination in the servers line Where the connection goes Decision
tcp://localhost:1883 The Telegraf container's own loopback Wrong destination; nothing listens there
tcp://hivemq:1883 Compose network, service-name DNS, broker container port Correct for a broker in the same project
The broker container's IP address Compose network, no name involved Works until the container is recreated
The host's address with the published port Through the host's port mapping For clients outside Docker, not for Telegraf

Service names survive container recreation; a container's IP address does not, which is why the name belongs in the configuration. The same rule explains the clue already in the file: the database output read urls = ["http://influxdb:8086"], a service name, while the MQTT input read localhost. On a PC without containers the same mistake wears a different face, and an MQTT sink that stays offline until you use the broker IP address covers that fix.

How do you apply the fix and reload Telegraf? #

Edit the host-side file behind the volume mount, change one line, and restart the telegraf service: the Compose file mounts ./telegraf/telegraf.conf at /etc/telegraf/telegraf.conf, so the edit belongs on the host copy, not inside the container.

  1. Open ./telegraf/telegraf.conf on the Docker host, the file the volume mounts at /etc/telegraf/telegraf.conf.
  2. Replace servers = ["tcp://localhost:1883"] with servers = ["tcp://hivemq:1883"], leaving topics and data_format untouched so the test isolates the destination.
  3. Restart or recreate the telegraf service through Compose so the process rereads its configuration.
  4. Watch the Telegraf log at startup until the MQTT input reports its connection.
  5. Publish one message to sensors/data and follow it downstream.
[[inputs.mqtt_consumer]]
  servers = ["tcp://hivemq:1883"]
  topics = ["sensors/data"]
  data_format = "json"

Leave the rest of the file alone. The Docker socket mount feeds the container-monitoring input, and the links entries concern the database destination; neither changes what localhost means inside Telegraf.

How long should a test point take to reach the dashboard? #

Allow 20 seconds end to end with this stack's settings: a message published now reaches InfluxDB at Telegraf's next flush, flush_interval = "10s", and a dashboard shows it on its next query, so judge the pipeline after that, not before.

Worked example with the values above and one controlled publish at T = 0 s. The subscription is event-driven, so the consumer receives the message as it arrives; the agent's interval = "10s" governs polling inputs such as container metrics, not this listener. The point then waits for the next flush tick, at worst 10 s, so InfluxDB holds it by T + 10 s.

A dashboard refreshing every 10 s — an illustrative value; use whatever yours is set to — shows it by T + 10 s + 10 s = T + 20 s. Decision: query the database at 10 s, the dashboard at 20 s; past that, the pipeline has a fault to find, not a delay to wait out. Note also that metric_buffer_limit = 10000 only holds points the agent collected, and an input that never connected collected nothing, so the outage period stays empty — capture resumes from the next publish and does not backfill.

Which hop do you check when MQTT data still does not arrive? #

Check the earliest failing hop first — broker connection, then topic match, then payload parsing, then the database write — and treat working container metrics as proof that a different input runs, not that the MQTT consumer is connected.

  • Connection refused or a timeout that still names the wrong address: the servers line does not carry the service name, or the broker is not listening yet. Confirm the line and that HiveMQ finished startup.
  • The name does not resolve: the services are not on the same network. One Compose project without custom networks puts them on the default project network; where networks are defined, verify both services share one.
  • Connected but no messages: the publisher never reached this broker, or the topic does not match. sensors/data is literal and case-sensitive, and one extra level breaks the match; when messages arrive but fields land in the wrong place, that is mapping an MQTT topic into InfluxDB fields with Telegraf.
  • Messages received but no points written: parsing or output. A consumer that connects and still writes nothing is a different fault, covered under Telegraf connecting but writing no data to InfluxDB.

Does the published port 1883:1883 matter for Telegraf? #

Not for Telegraf: the mapping serves clients outside Docker, with the host port on the left of the colon and the broker's container port on the right, and a consumer on the shared Compose network uses the container port directly without ever crossing the host.

The broker service publishes 1883:1883 and 8080:8080; the second serves the broker's own management console, not MQTT. Reading the MQTT mapping correctly is what keeps the two clients straight: an external sensor aims at the host's address and the left value, Telegraf aims at hivemq and the right value. Publishing a host port never places the broker at another container's loopback — the two mechanisms are unrelated.

When do the sensor publisher and Telegraf need different broker addresses? #

Whenever the sensor sits outside Docker and Telegraf inside it: the sensor publishes to the Docker host's reachable address plus the published port 1883, while Telegraf connects to the service name on the internal network — one broker, two destinations, each with its own verification.

Illustrative example: with the host at 192.168.1.50 on the plant network, the sensor publishes to 192.168.1.50:1883, and the port mapping forwards the connection into the broker container; Telegraf ignores that path entirely and dials hivemq:1883. A connected Telegraf still cannot receive data the sensor sent to a different broker or a mistyped port, so prove both paths with one publication. This stack runs the broker with HIVEMQ_DISABLE_AUTH_PLUGIN=true, so the published listener accepts unauthenticated clients — keep it exposed only on the interface and segment the sensors actually use.

Questions asked

Why did container monitoring keep working while MQTT data was missing?

The docker input reads the mounted Docker socket and polls on its own schedule, so it never opens a network connection to anything. The MQTT consumer is the only part of that configuration that dials out to a broker, which is why one input can thrive while the other never connects.

What if the MQTT broker runs on the Docker host instead of as a service?

Then no service name exists to resolve. Give Telegraf the host's LAN address with the port the broker listens on, or move the broker into the Compose project where its service name resolves. Localhost still means the Telegraf container in both cases, so it remains the wrong destination.

Should Grafana also use a service name for its datasource?

Yes, by the same rule: any container-side client of a Compose service reaches it through the service name on the shared network. That is why the example's database output already pointed at the influxdb service on port 8086 while the MQTT input wrongly pointed at localhost, and fixing the one inconsistent destination restored the pipeline.

Does it matter whether the address uses hivemq or mqttbroker?

The service name is the safer choice because Compose resolves it on the project network for as long as the service exists. The stack also sets a container name of mqttbroker, which typically resolves on the same network, but the service name stays valid if the container is renamed, so standardize on it.

Keep reading

  • How-to

    Run Python and Node-RED beside PLC code on an edge PC

    Keep the IEC 61131-3 program in the controller runtime and run Python and Node-RED as separate Docker containers on the same edge PC. Exchange data over one MQTT interface with a fixed publish period, and declare data stale after three missed periods. One container per workload, and nothing from Python or Node-RED inside the control project.

    Edge computing and IIoT

  • Explainer

    Can plant historian software run in Docker containers

    The processes will start, but historian services hold retained state and are not documented to communicate statelessly, so a started container proves nothing about collection, calculations, events, or queries surviving replacement. Keep the working deployment in production and evaluate containers only in an isolated test, with a volume mapped for every path that holds data or configuration, passing a counted query across a restart, a recreate, and a host move.

    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