0
votes

I have the eclipse-mosquitto image up and running, also a publisher in another container that connects and publishes successfully. I can shell into the broker container and use mosquitto_sub to retain the messages, but when I try to subscribe from the local machine the connection is refused.

This is the command I've used to start the container:

docker run --name mqtt --restart=always --net=host -tid  eclipse-mosquitto

The --net=host flag is used so I can just use localhost everywhere. And this is how I tried to subscribe, which works from within the container:

mosquitto_sub -h localhost -t TOPIC

Is there a Docker flag or some other option that I've missed preventing me to subscribe from the local machine? Or would a subscriber in a Docker container work?

1
I just run the same commands as you in my local environment and it worked as expected. Maybe you have some extra configurations in your machine. Can you check that your port 1883 is LISTENING in your local machine with netstat?Sergio Guillen Mantilla
@SergioGuillenMantilla can confirm the commands are working as expected on a machine with Ubuntu. On Windows the port is not listed by netstat unless a local mosquitto broker is started. I've tried adding the port to the firewall, but it wasn't listed afterwards, same as before.styps

1 Answers

2
votes

On Windows only, it is necessary to set the publish flag for the specific port, so the correct command to start the broker is

docker run --name mqtt -p 1883:1883 -tid eclipse-mosquitto

while the publisher is started with

docker run -it --net=host mosquitto-pub

Then a subscriber on the local machine is started without problem by simply

mosquitto_sub -t TOPIC

Note: The --net=host flag for the broker can't be used with the publish flag. Not sure why it's still necessary for the publisher though.