[[inputs.mqtt_consumer]] default config can't handle tcp://localhost

servers = [“tcp://127.0.0.1:1883”] doesn’t work
servers = [“tcp://192.168.100.221:1883”] works

Mosquitto and Telegraf are running in Docker

I don’t think that it is a Telegraf issue but maybe related to my settings. But I cannot figure out what !

`> pi@pi-home-test:~/docker $ sudo lsof -i TCP

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
container 501 root 13u IPv4 11645 0t0 TCP localhost:35953 (LISTEN)
sshd 513 root 3u IPv4 11635 0t0 TCP *:ssh (LISTEN)
sshd 513 root 4u IPv6 11637 0t0 TCP *:ssh (LISTEN)
sshd 1611 root 4u IPv4 15809 0t0 TCP pi-home-test:ssh->michel-HP-EliteBook-840-G3:41478 (ESTABLISHED)
sshd 1767 pi 4u IPv4 15809 0t0 TCP pi-home-test:ssh->michel-HP-EliteBook-840-G3:41478 (ESTABLISHED)
docker-pr 7815 root 3u IPv4 42485 0t0 TCP pi-home-test:1883->172.18.0.3:43300 (ESTABLISHED)
docker-pr 7815 root 4u IPv4 35102 0t0 TCP *:1883 (LISTEN)
docker-pr 7815 root 5u IPv4 42487 0t0 TCP 172.17.0.1:42536->172.17.0.2:1883 (ESTABLISHED)
docker-pr 10146 root 4u IPv4 37778 0t0 TCP *:8086 (LISTEN)
docker-pr 10997 root 4u IPv4 43393 0t0 TCP *:3000 (LISTEN)`

and

`pi@pi-home-test:~/docker $ ping localhost -c1

PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.149 ms

— localhost ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.149/0.149/0.149/0.000 ms
`

This is most likely a docker networking setting/issue.

servers = [“tcp://127.0.0.1:1883”] doesn’t work
servers = [“tcp://192.168.100.221:1883”] works

In a normal docker container, localhost (aka 127.0.0.1) points to the local docker container itself. It does not point to the local machine or any other container.

Are mosquito and telegraf running in separate containers?

I’d also add that you need to make sure mosquito what address it is bound to. Check our mosquito config for bind_address.

I would suggest reading up on how to launch a docker container with a connection to localhost, on a blog post like this or search the docker website for others looking for similar set up.

Thanks a lot !

Silly me !!! Of course, both services were running in different containers :slight_smile:

So, for those need the answer to that issue, there are two ways:

  • share the same container
    In that case , [[inputs.mqtt_consumer]] topic should have:
    servers = [“tcp://mosquitto:1883”]

  • split in different containers:
    telegraf service in docker compose file must the have the extra_hosts parameter:
    extra_hosts:
    - “host.docker.internal:host-gateway”

    and , [[inputs.mqtt_consumer]] topic should have:
    servers = [“tcp://host.docker.internal:1883”]

for the mosquitto config and its bind_address, that was the first thing I touched, but it didn’t solve anything

Big thank again, you saved my day !

1 Like