Search code examples
dockermqttmosquitto

Can't subscribe to Docker Eclipse-Mosquitto Broker


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?


Solution

  • 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.