Password protect Node-RED running in Docker container

First of all we need to enable admin authentication on NodeRED. To do this, open the file settings.json from the container folder and comment out these lines:

adminAuth: {
        type: "credentials",
        users: [{
            username: "admin",
            password: "my-password",
            permissions: "*"
        }]
    },

Now here is where things get a bit more complicated. The password within this block of code needs to be encrypted (so that you can't just open the file and read the password). To do this, we need to first get access to the container terminal via SSH

docker exec -it your-container-name bash

Once running the terminal within the container, we need to execute another command that will generate the encrypted password:

node -e "console.log(require('bcryptjs').hashSync(process.argv[1], 8));" your-password

the terminal will then convert your-password to an encrypted one (let's pretend it's $2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM. that we need to paste in the settings.json file.

In other words, out settings file will look something like this:

adminAuth: {
        type: "credentials",
        users: [{
            username: "admin",
            password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM.",
            permissions: "*"
        }]
    },

Once the settings are set, restart the NodeRED container.