SOLVED: Docker/MQTT Mosquitto setup - what am I missing?

I have been banging my head off the wall trying to get Mosquitto/MQTT set up on my HA. Using HA 0.84.6 in Docker and using docker-compose on a NUC if that matters at all.

Here’s my docker-compose portion of the file for MQTT:

mqtt:
      container_name: MQTT
      restart: unless-stopped
      image: eclipse-mosquitto:latest
      volumes:
        - /srv/docker/mosquitto/config:/mosquitto/config
        - /srv/docker/mosquitto/log:/mosquitto/log
        - /srv/docker/mosquitto/data:/mosquitto/data
       # - /etc/localtime:/etc/localtime:ro
      depends_on: 
        - homeassistant
      ports:
        - "1883:1883"
        - "9001:9001"   
      network_mode: host  
      environment:
        - TZ=America/New_York

In my configuration.yaml, I have the following:

mqtt:
  broker: 127.0.0.1
  port: 1883

I’m positive that it will be obvious to most people, but I can’t seem to figure out what I’m doing wrong. I have attempted to connect to the server using MQTT.fx and it also doesn’t show up in the list of available services in the HA side bar. I have changed the broker to the IP address of my HA machine as well.

I have tried to follow the instructions here, but I’m not getting any farther. Can anyone help show me what I’m doing wrong?

What does the logfile say? Do you set a username/password anywhere? (for the broker)?
I am using hass.io with the MQTT add-on, not docker-compose
(Also, this topic is hidden because you didn’t set a category for it)

I use the docker setup similar to you.
The IP address should be the IP of the docker host, I don’t think localhost will work, as that is inside the docker container.(afaik) or if your mqtt docker has it’s own IP then use that.

My config is here, but not very useful as everything is in the secrets file…

https://github.com/Data-Monkey/Home-Assistant-Config/blob/master/config/packages/mqtt.yaml

Hi, I don’t think you should use ports - "1883:1883" - "9001:9001" in combination with network_mode: host. Use either one or the other. I use the first one.

Do you have a config file called /srv/docker/mosquitto/config/mosquito.conf ?
eclipse-mosquitto expects a config file. You can use an empty one.
To get the default mosquito.conf, you can first run eclipse-mosquitto without volume mounts and then executed this to export the file:

$ docker exec <container id>  cat /mosquitto/config/mosquito.conf >  /srv/docker/mosquitto/config/mosquito.conf

I hope this is useful.

1 Like

I agree. And check mosquitto logs first if it’s running OK.

1 Like

I think the problem is 127.0.0.1

That will be the internal address for the homeassistant docker container I think.

Use the device ip address.

I agree that needs to change too.
So you have a combination of things to resolve.

Success! There appeared to be an issue with the mosquitto.conf. I kept getting a permission denied error trying to do @tedsluis suggestion, but I just went to the github page and copied/pasted the default .conf file into my setup and then restarted the docker container.

I removed the network_mode: host as suggested and instead included the ports. Finally, I modified the configuration.yaml file to point to the device’s IP address rather than localhost or 127.0.0.1.

Thanks so much to everyone for the quick and great help!

2 Likes

Do you mind please sharing your mosquitto.conf? thanks

Sure, I don’t think I ever changed anything here.
I run on unRAID using the container and template by spants
spants/mqtt:latest

root@Tower:/mnt/cache/appdata/MQTT# cat mosquitto.conf
# Place your local configuration in /config/conf.d/

retry_interval 20
sys_interval 10
store_clean_interval 10
user mosquitto
max_inflight_messages 40
max_queued_messages 200
queue_qos0_messages false
message_size_limit 0
allow_zero_length_clientid true
persistent_client_expiration 3m
allow_duplicate_messages false
autosave_interval 60
autosave_on_changes false

persistence false
persistence_file mosquitto.db
persistence_location /config/data/

# Port to use for the default listener.
listener 1883

listener 9001
protocol websockets

log_dest stderr
log_type all
connection_messages true
log_timestamp true

include_dir /config/conf.d

# If the password.mqtt file is empty, this has no effect.
allow_anonymous true

# DO NOT CHANGE THIS LINE BELOW!!!
password_file /config/passwords.mqtt
1 Like

Appreciate it, thanks a lot!