Mqtt-docker for Home Assistant

Robert AndresenHome Assistant, Internet of things, Tutorials Leave a Comment

In this post, I created a simple PHP-to-MQTT integration for Telldus Tellstick. I used CloudMQTT as a broker, but after upgrading HA to 0.107 it stopped working for some reason.

When I started to troubleshoot this, I started wondering why I have used a third-party broker. Maybe it was before I started with docker…

So i just fired up an own MQTT-broker in Docker. I added this to my docker-compose.yaml (see my full docker-compose.yaml file at the end):

  mqtt:
    container_name: mosquitto
    image: eclipse-mosquitto:latest
    ports:
      - "1883:1883"
      - "9002:9001"
    volumes:
      - /home/robert/mosquitto/config:/mqtt/config:ro
      - /home/robert/mosquitto/log:/mqtt/log
      - /home/robert/mosquitto/data/:/mqtt/data

I had to map port 9001 to 9002, as Portainer uses 9001. Not sure if I actually need the 9001 port. Looks like it’s for websockets?

To only start the MQTT-container in my composer file, I typed:

 docker-compose up mqtt

I had to change the configuration.yaml in HA, with the new MQTT settings. As this is now running locally, i probably wouldn’t need to have the config in my secret file.

mqtt:
  broker: !secret DOCKER_MQTT_HOST
  port: !secret DOCKER_MQTT_PORT

Tip

I used MQTT-Explorer from Windows store to see that it was working.

My full docker-compose.yaml

version: '3.2'
services:
  homeassistant:
    container_name: home-assistant
    image: homeassistant/home-assistant
    depends_on:
      - db
    volumes:
      - /home/robert/homeassistant:/config
      - /etc/localtime:/etc/localtime:ro
    devices:
      - /dev/ttyACM0:/dev/ttyACM0
    restart: always
    network_mode: host

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      - MYSQL_DATABASE=<<user>>
      - MYSQL_USER=<<password>>
      - MYSQL_PASSWORD=<<password>>
      - MYSQL_ROOT_PASSWORD=<<password>>
    volumes:
      - /var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    links:
      - db:mysql
    ports:
      - 8181:80
    environment:
      PMA_HOST: db
      PMA_USER: <<user>>
      PMA_PASSWORD: <<password>>

  nodered:
    image: nodered/node-red-docker:v8
    depends_on:
      - homeassistant
    ports:
      - 1880:1880
    volumes:
      - /home/robert/node-red:/data
    network_mode: host

  influxdb:
    container_name: influxdb
    restart: unless-stopped
    image: influxdb
    volumes:
      - /var/influxdb:/data
    environment:
      - "PRE_CREATE_DB=home_assistant"
    ports:
      - "8086:8086"

  grafana:
    container_name: grafana
    restart: unless-stopped
    image: grafana/grafana
    depends_on:
      - "influxdb"
    volumes:
      - /srv/docker/grafana:/var/lib/grafana
    ports:
      - "3000:3000"
      
  mqtt:
    container_name: mosquitto
    image: eclipse-mosquitto:latest
    ports:
      - "1883:1883"
      - "9002:9001"
    volumes:
      - /home/robert/mosquitto/config:/mqtt/config:ro
      - /home/robert/mosquitto/log:/mqtt/log
      - /home/robert/mosquitto/data/:/mqtt/data