Skip to content

Node-RED container with authentication and Home Assistant nodes setup

Introduction

Node-RED is a programming tool. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.

Setup

  1. Create the folders needed by the container

    Run the following commands inside your home folder:

    # Open your terminal application
    cd ~
    mkdir -p nodered/data
    cd nodered
  2. Create the script needed to run the container

    Save the following script as nodered_run.sh:

    nodered_run.sh
    # To create this script use your text editor application, for example Nano
    docker run -d \
    --hostname=nodered \
    --name=nodered \
    --net=host \
    -v $PWD/data:/data \
    -e "NODE_RED_CREDENTIAL_SECRET=<key>" \
    -e TZ="Europe/Amsterdam" \
    --restart=unless-stopped \
    --cap-add=NET_ADMIN \
    nodered/node-red
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace docker with podman if needed
    • Optional Replace --net=host with ports to pass through if you want to expose ports. Here is described how to check if ports are available. Make sure port 1880 is available
    • Optional Replace $PWD/data with the location of your data if needed. This can be a fileserver mount
    • Optional Replace <key> with your own key which you can specify in settings.js. Otherwise a key will be automatically generated
    • Required Replace Europe/Amsterdam with your own timezone
  3. Run the script to create the container

    Run the following command:

    # Open your terminal application
    sudo sh nodered_run.sh

    The image nodered/node-red is automatically pulled and the container is created.

  4. Enable authentication (optional)

    Because I use Home Assistant nodes within Node-RED but also, for example, an SSH node, I have set up authentication to make it a bit more secure.

    Make sure the Node-RED container is running and generate a hash for the password you want to use:

    # Open your terminal application
    sudo docker exec -it nodered npx node-red admin hash-pw

    Enter the password you want to use for Node-RED and copy the hash to memory.

    Now adjust the following in the nodered/data/settings.js file:

    nodered/data/settings.js
    // To edit use your text editor application, for example Nano
    adminAuth: {
    type: "credentials",
    users: [{
    username: "<username>",
    password: "<hash>",
    permissions: "*"
    }]
    },
    // IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace <username> with your username, admin for example
    • Required Replace <hash> with the hash you generated earlier. Copy the hash from memory
    • Optional Replace * if needed to change the permissions

    Restart the Node-RED container. Run the following command:

    # Open your terminal application
    sudo docker restart nodered
  5. Install additional nodes (optional)

    The container uses the /data directory as the user configuration directory. To add additional nodes you can open the shell into the container and run the appropriate npm install commands. Here are a few examples of nodes I always install:

    # Open your terminal application
    # Open a shell in the container
    sudo docker exec -it nodered /bin/bash
    # Once inside the container, npm install the nodes in /data
    cd /
    cd data
    npm install node-red-node-wol
    npm install node-red-contrib-bigssh
    npm install node-red-contrib-home-assistant-websocket
    exit
    Instructions:
    • Optional node-red-node-wol is a node to send Wake-On-LAN (WOL) magic packets
    • Optional node-red-contrib-bigssh (Big SSH) is an input node to execute a command over SSH to a remote host
    • Optional node-red-contrib-home-assistant-websocket are various nodes to assist in setting up automation using Node-RED communicating with Home Assistant

    Restart the Node-RED container. Run the following command:

    # Open your terminal application
    sudo docker restart nodered
  6. Check the results

    If needed you can check if the container is running properly.

    Now you can use Node-RED by opening a web browser and going to: http://localhost:1880. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier.

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact