6

I'm running Mosquitto 2.0.7 as a docker.

If I try to use mosquitto_sub from within the docker I can do it. If I try it from a different machine I get connection refused.

The docker is running exposing port 1883 and 9001. The docker is runing on host network. There is no error on the docker log.

Thanks

Andre Martins
  • 71
  • 1
  • 1
  • 2
  • 4
    Please include your `mosquitto.conf`; I'd guess the issue is that you don't have a `listener` defined ([Mosquitto V2](https://mosquitto.org/blog/2020/12/version-2-0-0-released/#breaking-changes) binds to the loopback interfaces, `127.0.0.1` and/or `::1`, when no listeners are configured which means it cannot be accessed externally in the default config). See [this answer](https://stackoverflow.com/questions/65278648/mosquitto-starting-in-local-only-mode/65278769#65278769) for more info. – Brits Feb 10 '21 at 19:26
  • Also include details of how you started the container – hardillb Feb 10 '21 at 21:28
  • Launch configuration of the container is needed to answer. e.g. docker run options or docker-compose.yml – mrq Feb 11 '21 at 23:38

3 Answers3

9

I had same issue. try this changes in ./services/mosquitto/mosquitto.conf: add, replace or verify at your config file this lines:

allow_anonymous true
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

and restart your container mosquitto.

azegurelabs
  • 151
  • 2
  • 3
5

It seems at some point, the location of the config file was changed.

As long as the network_mode is set to "host" and the config file gets copied into the correct folder in the container then the following yml/configs should work without issue as of 2.0.14:

docker-compose.yml:

version: "3"
services:
  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    network_mode: "host"
    volumes:
      - ./conf:/mosquitto/config
      - ./data:/mosquitto/data
      - ./log:/mosquitto/log

conf/mosquitto.conf

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883 0.0.0.0

## Authentication ##
allow_anonymous true
#password_file /mosquitto/conf/mosquitto.conf

Issuing plain-old sudo docker-compose up -d should work, exposing the ports on the host's network.

dperish
  • 1,493
  • 16
  • 27
1

I have my Mosquitto Docker container configured with a port 1883, and that seems to bind it to 0.0.0.0, which allows for access from outside the container. Be sure you also expose the port in your startup command:

docker run -d --name="mosquitto" -p 1883:1883 prologic/mosquitto
JD Allen
  • 799
  • 5
  • 12
  • 2
    `prologic/mosquitto` seems to be based on crux linux; I installed it on my machine and its using Mosquitto 1.3.1 so is not really relevant to the question (which specifies a version). Note that 1.3.1 was released in 2014 so you may want to consider moving to the `eclipse-mosquitto` image. – Brits Feb 10 '21 at 22:39