Andre Martins
Andre Martins

Reputation: 91

Unable to connect from outside the Mosquitto docker

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

Upvotes: 8

Views: 20822

Answers (3)

dperish
dperish

Reputation: 1571

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.

Upvotes: 6

azegurelabs
azegurelabs

Reputation: 221

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.

Upvotes: 11

JD Allen
JD Allen

Reputation: 944

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

Upvotes: 1

Related Questions