Skip to content

Instantly share code, notes, and snippets.

@DJBenson
Last active April 19, 2026 16:27
Show Gist options
  • Select an option

  • Save DJBenson/5939f59ea24ba9a90ea2e01176fb167d to your computer and use it in GitHub Desktop.

Select an option

Save DJBenson/5939f59ea24ba9a90ea2e01176fb167d to your computer and use it in GitHub Desktop.

Revisions

  1. DJBenson revised this gist Jan 17, 2026. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -126,7 +126,8 @@ nano mosquitto/config/mosquitto.conf

    Add the following line to the configuration file;
    ```
    listener 1883 0.0.0.0
    listener 1883 0.0.0.0 (IPv4)
    listener 1883 :: (IPv6)
    ```

    Restart the Mosquitto container;
  2. DJBenson revised this gist Jan 17, 2026. 1 changed file with 24 additions and 1 deletion.
    25 changes: 24 additions & 1 deletion ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -116,4 +116,27 @@ Add the MQTT integration
    <img width="1447" height="329" alt="image" src="https://gist.github.com/user-attachments/assets/f4cea356-b293-4878-ae76-aee37ac43983" />


    You now have Home Assistant Core linked to Mosquitto MQTT.
    You now have Home Assistant Core linked to Mosquitto MQTT.

    If you want the MQTT server to be available outside of Home Assistant, you need to allow remote connections to Mosquitto;

    ```
    nano mosquitto/config/mosquitto.conf
    ```

    Add the following line to the configuration file;
    ```
    listener 1883 0.0.0.0
    ```

    Restart the Mosquitto container;
    ```
    docker restart ha-test-mosquitto
    ```

    The MQTT server will then be available on the IP address of the host, for example;
    ```
    192.168.1.187:1883
    ```

    NOTE: This isn't necessary unless you absolutely need something else to access the MQTT server outside of Home Assistant (e.g. you have a device which can 'speak' MQTT and needs broker details).
  3. DJBenson revised this gist Jan 17, 2026. 1 changed file with 5 additions and 29 deletions.
    34 changes: 5 additions & 29 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ services:
    image: eclipse-mosquitto:latest
    container_name: ha-test-mosquitto
    restart: unless-stopped
    network_mode: main
    network_mode: host
    volumes:
    - ./mosquitto/config:/mosquitto/config
    - ./mosquitto/data:/mosquitto/data
    @@ -58,21 +58,13 @@ services:
    image: ghcr.io/home-assistant/home-assistant:stable
    container_name: ha-test-homeassistant
    restart: unless-stopped
    network_mode: main
    ports:
    - 8123:8123
    network_mode: host
    volumes:
    - ./homeassistant/config:/config
    - /etc/localtime:/etc/localtime:ro
    - /etc/timezone:/etc/timezone:ro
    ```

    This file makes some assumptions;

    * It uses a network called "main" - replace this with your network or set it to "host"
    * We don't expose a port for Mosquitto - we don't need it - we will refer to the container by its name (ha-test-mosquitto) from Home Assistant


    ### Set permissions

    ```
    @@ -111,29 +103,13 @@ Add the MQTT integration

    #### Add the MQTT broker

    * In the "Broker" field add the name of your Mosquitto container - in our example it's ```ha-test-mosquitto```
    * If you're using the container name or did not change the default MQTT port, leave this as ```1883``` or enter the port you used if you did change the port
    * In the "Broker" field use ```localhost```
    * Leave the port as ```1883```
    * In the username field, enter the user you created earlier, in our example ```ha-mqtt```
    * In the password field, enter your chosen password, in our example we again use ```ha-mqtt```
    * Click on "Submit"

    Note: if you want to use the IP of the host (i.e. to use Mosquitto outside of this setup) then you need to add a port mapping to docker-compose.yaml for the Mosquitto container;

    ```
    mosquitto:
    image: eclipse-mosquitto:latest
    container_name: ha-test-mosquitto
    restart: unless-stopped
    network_mode: main
    ports:
    - 1883:1883
    volumes:
    - ./mosquitto/config:/mosquitto/config
    - ./mosquitto/data:/mosquitto/data
    - ./mosquitto/log:/mosquitto/log
    ```

    <img width="458" height="579" alt="image" src="https://gist.github.com/user-attachments/assets/2c429db8-584a-4971-948a-b5282203ea66" />
    <img width="452" height="576" alt="image" src="https://gist.github.com/user-attachments/assets/8e51c593-a871-4552-9343-4dc441bdeebf" />

    <img width="392" height="188" alt="image" src="https://gist.github.com/user-attachments/assets/bfa4c31c-c52f-4a94-af39-aae83370d61e" />

  4. DJBenson revised this gist Jan 17, 2026. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -72,14 +72,6 @@ This file makes some assumptions;
    * It uses a network called "main" - replace this with your network or set it to "host"
    * We don't expose a port for Mosquitto - we don't need it - we will refer to the container by its name (ha-test-mosquitto) from Home Assistant

    ### Create a Mosquitto user
    ```
    docker run --rm -it \
    -v "$(pwd)/mosquitto/config:/mosquitto/config" \
    eclipse-mosquitto mosquitto_passwd \
    /mosquitto/config/passwd ha-mqtt
    ```
    Replace 'ha-mqtt' with your desired username. You'll be prompted for a password.

    ### Set permissions

    @@ -89,6 +81,15 @@ sudo chmod 700 mosquitto/config
    sudo chmod 600 mosquitto/config/passwd
    ```

    ### Create a Mosquitto user
    ```
    docker run --rm -it \
    -v "$(pwd)/mosquitto/config:/mosquitto/config" \
    eclipse-mosquitto mosquitto_passwd \
    /mosquitto/config/passwd ha-mqtt
    ```
    Replace 'ha-mqtt' with your desired username. You'll be prompted for a password.

    ### Pull and launch the container
    ```docker compose up```

  5. DJBenson revised this gist Jan 17, 2026. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,11 @@ password_file /mosquitto/config/passwd
    ```

    Create an empty password file;
    ```
    touch mosquitto/config/passwd
    ```

    ### Create the docker compose file

    ```nano docker-compose.yaml```
  6. DJBenson revised this gist Jan 17, 2026. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -31,14 +31,6 @@ password_file /mosquitto/config/passwd
    ```

    ### Set permissions

    ```
    sudo chown -R 1883:1883 mosquitto
    sudo chmod 700 mosquitto/config
    sudo chmod 600 mosquitto/config/passwd
    ```

    ### Create the docker compose file

    ```nano docker-compose.yaml```
    @@ -84,6 +76,14 @@ eclipse-mosquitto mosquitto_passwd \
    ```
    Replace 'ha-mqtt' with your desired username. You'll be prompted for a password.

    ### Set permissions

    ```
    sudo chown -R 1883:1883 mosquitto
    sudo chmod 700 mosquitto/config
    sudo chmod 600 mosquitto/config/passwd
    ```

    ### Pull and launch the container
    ```docker compose up```

  7. DJBenson revised this gist Jan 17, 2026. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -105,12 +105,28 @@ Add the MQTT integration

    #### Add the MQTT broker

    * In the "Broker" field add the name of your Mosquitto container (or the IP address of the host) - in our example it's ```ha-test-mosquitto```
    * In the "Broker" field add the name of your Mosquitto container - in our example it's ```ha-test-mosquitto```
    * If you're using the container name or did not change the default MQTT port, leave this as ```1883``` or enter the port you used if you did change the port
    * In the username field, enter the user you created earlier, in our example ```ha-mqtt```
    * In the password field, enter your chosen password, in our example we again use ```ha-mqtt```
    * Click on "Submit"

    Note: if you want to use the IP of the host (i.e. to use Mosquitto outside of this setup) then you need to add a port mapping to docker-compose.yaml for the Mosquitto container;

    ```
    mosquitto:
    image: eclipse-mosquitto:latest
    container_name: ha-test-mosquitto
    restart: unless-stopped
    network_mode: main
    ports:
    - 1883:1883
    volumes:
    - ./mosquitto/config:/mosquitto/config
    - ./mosquitto/data:/mosquitto/data
    - ./mosquitto/log:/mosquitto/log
    ```

    <img width="458" height="579" alt="image" src="https://gist.github.com/user-attachments/assets/2c429db8-584a-4971-948a-b5282203ea66" />

    <img width="392" height="188" alt="image" src="https://gist.github.com/user-attachments/assets/bfa4c31c-c52f-4a94-af39-aae83370d61e" />
  8. DJBenson revised this gist Jan 17, 2026. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -111,4 +111,11 @@ Add the MQTT integration
    * In the password field, enter your chosen password, in our example we again use ```ha-mqtt```
    * Click on "Submit"

    <img width="458" height="579" alt="image" src="https://gist.github.com/user-attachments/assets/2c429db8-584a-4971-948a-b5282203ea66" />

    <img width="392" height="188" alt="image" src="https://gist.github.com/user-attachments/assets/bfa4c31c-c52f-4a94-af39-aae83370d61e" />

    <img width="1447" height="329" alt="image" src="https://gist.github.com/user-attachments/assets/f4cea356-b293-4878-ae76-aee37ac43983" />


    You now have Home Assistant Core linked to Mosquitto MQTT.
  9. DJBenson created this gist Jan 17, 2026.
    114 changes: 114 additions & 0 deletions ha-docker-mqtt.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    # Home Assistant Core in Docker with Mosquitto broker

    *​This requires basic knowledge of linux, bash, nano and Docker itself.*

    ## Basic Setup
    In this example we are using /home/user/ha-docker-mqtt as the root path so all commands should be run from there (your chosen path!).

    ### Create the required directories

    ```
    mkdir -p homeassistant/config
    mkdir -p mosquitto/{config,data,log}
    ```

    ### Create the mosquitto.conf file

    ```
    nano mosquitto/config/mosquitto.conf
    ```

    Paste the following;

    ```
    persistence true
    persistence_location /mosquitto/data
    log_dest stdout
    allow_anonymous false
    password_file /mosquitto/config/passwd
    ```

    ### Set permissions

    ```
    sudo chown -R 1883:1883 mosquitto
    sudo chmod 700 mosquitto/config
    sudo chmod 600 mosquitto/config/passwd
    ```

    ### Create the docker compose file

    ```nano docker-compose.yaml```

    Paste the following content;

    ```
    services:
    mosquitto:
    image: eclipse-mosquitto:latest
    container_name: ha-test-mosquitto
    restart: unless-stopped
    network_mode: main
    volumes:
    - ./mosquitto/config:/mosquitto/config
    - ./mosquitto/data:/mosquitto/data
    - ./mosquitto/log:/mosquitto/log
    homeassistant:
    image: ghcr.io/home-assistant/home-assistant:stable
    container_name: ha-test-homeassistant
    restart: unless-stopped
    network_mode: main
    ports:
    - 8123:8123
    volumes:
    - ./homeassistant/config:/config
    - /etc/localtime:/etc/localtime:ro
    - /etc/timezone:/etc/timezone:ro
    ```

    This file makes some assumptions;

    * It uses a network called "main" - replace this with your network or set it to "host"
    * We don't expose a port for Mosquitto - we don't need it - we will refer to the container by its name (ha-test-mosquitto) from Home Assistant

    ### Create a Mosquitto user
    ```
    docker run --rm -it \
    -v "$(pwd)/mosquitto/config:/mosquitto/config" \
    eclipse-mosquitto mosquitto_passwd \
    /mosquitto/config/passwd ha-mqtt
    ```
    Replace 'ha-mqtt' with your desired username. You'll be prompted for a password.

    ### Pull and launch the container
    ```docker compose up```

    Watch the console - if everything is good you can detach (press d) from the console if you're running a newer version of docker or press CTRL+C and then launch again with ```docker compose up -d``` to launch in daemon mode.

    ### Configure Home Assistant

    Add the MQTT integration

    [![Open your Home Assistant instance and start setting up a new integration.](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start/?domain=mqtt)

    #### Manual steps:

    * Open your browser and go to http://<YourIPAddress:8123
    * Navigate to Setttings -> Integrations, devices, entities and helpers
    * Click on "Add integration"
    * Search for MQTT
    * Select the "MQTT" option at the top

    #### Add the MQTT broker

    * In the "Broker" field add the name of your Mosquitto container (or the IP address of the host) - in our example it's ```ha-test-mosquitto```
    * If you're using the container name or did not change the default MQTT port, leave this as ```1883``` or enter the port you used if you did change the port
    * In the username field, enter the user you created earlier, in our example ```ha-mqtt```
    * In the password field, enter your chosen password, in our example we again use ```ha-mqtt```
    * Click on "Submit"

    You now have Home Assistant Core linked to Mosquitto MQTT.