# No connection to Mosquitto with localhost (Docker)

**URL:** <https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901>\
**Category:** General\
**Tags:** docker\
**Created:** [8 February 2023 17:09 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901 "2023-02-08T17:09:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [8 February 2023 17:09 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/1 "2023-02-08T17:09:48Z")

</div>

First of all, I must apologize, as this topic is most likely not directly related to Node-RED, but to Docker. But maybe you guys can help me anyway.

Node-RED is natively installed on a Raspberry Pi.  
In parallel, an instance of Mosquitto is running in a Docker container.

I was always able to access the Mosquitto MQTT broker with localhost.

But after I had to completely rebuild the RPi, the connection only works via the real IP, i.e. 192.168.0.10 instead of also via localhost.  
Unfortunately, I have absolutely no idea whether I have made a special setting for this, or whether this had worked out of the box.

I would rather not run the container on the network "host" on Docker.  
This would allow me to address Mosquitto via localhost, but I'm sure I hadn't done it that way before the reinstall.

I would be very grateful for any tips and tricks.

Thanks a lot  
Newbow

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 February 2023 17:11 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/2 "2023-02-08T17:11:19Z")

</div>

> [@newbow](#):
>
> I would rather not run the container on the network "host" on Docker.

Why run mosquitto in a container in the first place... ?

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [8 February 2023 17:20 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/3 "2023-02-08T17:20:22Z")

</div>

> [@bakman2](#):
>
> Why run mosquitto in a container in the first place... ?

Well, I thought this is the best way to handle updates.  
I have serveral container and it's way easier to move/ clone container to another machine.

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [23 February 2023 11:42 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/4 "2023-02-23T11:42:57Z")

</div>

I just tried the IP 127.0.0.1 and it works... But why does it not work when using the word "localhost"? It used to work earlier. I don't get it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2023 13:18 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/5 "2023-02-23T13:18:54Z")

</div>

What is in the file /etc/hosts?

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [23 February 2023 13:57 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/6 "2023-02-23T13:57:21Z")

</div>

This is my /etc/hosts:

> `127.0.0.1 localhost`  
> `::1 localhost ip6-localhost ip6-loopback`  
> `ff02::1 ip6-allnodes`  
> `ff02::2 ip6-allrouters`  
> `127.0.1.1 raspberrypi`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2023 14:01 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/7 "2023-02-23T14:01:57Z")

</div>

In that case localhost should map to 127.0.0.1, so I don't know why that would not work. I suspect it is to do with the way the docker container is setup, but I don't know what. Unless it is an ipv6 issue.

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [23 February 2023 14:34 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/8 "2023-02-23T14:34:36Z")

</div>

Thank you Colin, you have put me on the right track.

I changed/ commented my /etc/hosts file as follows:

> 127.0.0.1 localhost  
> `#`::1 localhost ip6-localhost ip6-loopback  
> `#`ff02::1 ip6-allnodes  
> `#`ff02::2 ip6-allrouters  
> 127.0.1.1 raspberrypi

I read somewhere that especially the `localhost` in the line:  
`::1 localhost ip6-localhost ip6-loopback` causes problems.  
Now it's working like a charm again. Maybe the ip6 crap got in my way 😉

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/3583d006a973cf422d83a43a2b9f500e8841723c.png)

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a093b809a5baa43236279746753a57abb7567078.png)

**EDIT:**  
By the way, this also works (removing just the `localhost` in the second line):

> 127.0.0.1 localhost  
> `#`::1 localhost ip6-localhost ip6-loopback  
> `::1 ip6-localhost ip6-loopback`  
> ff02::1 ip6-allnodes  
> ff02::2 ip6-allrouters  
> 127.0.1.1 raspberrypi

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2023 15:30 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/9 "2023-02-23T15:30:20Z")

</div>

Normally what you have would work. Perhaps your docker container is not able to receive IPV6.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 February 2023 16:11 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/10 "2023-02-23T16:11:54Z")

</div>

we have seen/heard of several issue with DNS and IPv6 on docker on Alpine builds (that we use).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 March 2023 16:11 UTC](https://discourse.nodered.org/t/no-connection-to-mosquitto-with-localhost-docker/74901/11 "2023-03-09T16:11:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
