HoTo mqtt with docker

Googled and tried a lot but still confused :crazy_face:

Someone out there running HA and a mqtt broker - i.e. mosquitto - in a docker environment on a Raspi (Raspbian) and is willing so share the mosquitto install/setup?

I don’t currently run the Mosquitto MQTT Broker as a docker container. However, I plan to do so in the future. To prepare for it, I collected configuration examples from people who were using docker-compose to start their mosquitto container.

Here are the four samples I’ve collected. Perhaps they can give you some insight into how you wish to configure your system.

# Sample 1
  mqtt:
    container_name: MQTT
    restart: unless-stopped
    image: eclipse-mosquitto
    volumes:
      - /srv/docker/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - /srv/docker/mosquitto/log:/mosquitto/log
      - /srv/docker/mosquitto/data:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "1883:1883"
      - "9001:9001"

# Sample 2
  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto
    ports:
      - "1883:1883"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /opt/docker/mosquitto:/mosquitto/config:ro
      - /opt/docker/mosquitto:/mosquitto/data
    restart: on-failure

# Sample 3
  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto
    user: "1000:1000"
    ports:
      - 1883:1883
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /opt/mosquitto:/mosquitto/config:ro
      - /opt/mosquitto:/mosquitto/data
    restart: on-failure

# Sample 4
  mqtt:
    image: eclipse-mosquitto:latest
    container_name: "MQTT"
    restart: always
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - /home/$USER/docker_files/mqtt/data:/mosquitto/data
      - /home/$USER/docker_files/mqtt/config:/mosquitto/config
      - /home/$USER/docker_files/mqtt/log:/mosquitto/log
    network_mode: "host"
    environment:
      - TZ=America/New_York

Thanks for that :grinning:

I got the container running with a command similar to Sample 4, but to see the outputs with a discrete docker run.

docker run -itd -p 1883:1883 -p 9001:9001 -v /home/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf -v mosquitto_data:/mosquitto/data -v mosquitto_data:/mosquitto/log --name mosquitto eclipse-mosquitto

But now, how to successively check, if it is working and connect to HA? There are a lot of steps which might fail and I would like to check step by step…
(Had used the HA-internal mqtt-broker before…)

I used

mosquitto_pub  -V mqttv311 -u test -P test -t "some_mqtt/topic/here" -m "Meine erste MQTT-Message"

which answers nothing. When I stop the container I get

Error: Connection refused

Looks like the broker is receiving the message…

You can use any client, I like MQTT-Explorer to verify you can connect and sub/pub on topics.

You don’t “connect to HA”, you have HA connect to it.

Did you set a user/pass in your broker? if you didn’t, why are you passing a user/pass to it?

Great hint! Could verify that my container is running and receives messages.

user/pass not yet set. Copy & paste the old test message which I have used for the internal broker.

Then it won’t connect trying to use a user/pass. try without those options to see if the broker is working.

Hhm, a bit more confused…
Firing the mosquitto_pub command from above (with -u and -P) I see the message in the MQTT-Explorer… Shouldn’t I?

But what to put in the HA configuration.yaml?

It can’t connect if you don’t have the server setup with a user/pass

If you don’t set the server up with a user/pass, you don’t need to add that

1 Like

Sometimes its so easy…

Skipped the security and password to be solved later. Till now, my Raspi isn’t exposed to the internet and the MQTT-messages are just temperature values.

Just copied the old zigbee2mqtt sensor yaml to the new location and I see all my temperature values :+1:

For those who are trying the same. Here is my configuration:
Start the container manually:
docker run -it -d -p 1883:1883 -p 9001:9001 eclipse-mosquitto:latest

Use docker-compose:

docker-compose.yaml

  version: '3'
  services:
    mqtt:
      container_name: mosquitto
      image: eclipse-mosquitto:latest
      ports:
        - "1883:1883"
        - "9001:9001"
      volumes:
        - /home/pi/mosquitto/config:/mqtt/config:ro
        - /home/pi/mosquitto/log:/mqtt/log
        - /home/pi/mosquitto/data/:/mqtt/data

mosquitto.conf

Default file (in my case) located in directory /home/pi/mosquitto/conf

# Place your local configuration in /mqtt/config/conf.d/

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /mqtt/data/

user mosquitto

# Port to use for the default listener.
port 1883


log_dest file /mqtt/log/mosquitto.log
log_dest stdout

include_dir /mqtt/config
2 Likes

Curious if you figured out the setup a password part. Having issues with that in Docker, though honestly it’s not the biggest deal considering all the MQTT traffic is local.