Deploying and configuring node-red authentication on docker-compose

Deploying and configuring node-red authentication on docker-compose

Node-red deployments on docker-compose with authorization enabled and using docker volume.

Create a docker-compose.yml file:

version: "3.7"

services:
  node-red:
    image: nodered/node-red
    environment:
      - TZ=Europe/Moscow
    ports:
      - "11880:1880" # 11880 - ΠΏΠΎΡ€Ρ‚ для ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ ΠΊ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρƒ, 1880 - ΠΏΠΎΡ€Ρ‚ Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ node-red Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π°.
    volumes:
      - "node-red:/data" # node-red - ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹Π΄Π΅Π»ΠΈΡ‚ docker для хранСния Π΄Π°Π½Π½Ρ‹Ρ…, /data - ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ Π²Π½ΡƒΡ‚Ρ€ΠΈ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π°.
    restart: always
volumes:
  node-red: # созданиС ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π° node-red Π½Π° хостС.


Run the container in debug mode (the first run should be without the `-d` to view errors when they appear):

$ docker-compose up node-red
Creating node-red_node-red_1_3e3e59f5e044 ... done
Attaching to node-red_node-red_1_bca4cb987984
node-red_1_bca4cb987984 |
node-red_1_bca4cb987984 | > [email protected] start /usr/src/node-red
node-red_1_bca4cb987984 | > node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS "--userDir" "/data"
...

We stop the container and run the command to view the volume:

$ docker volume ls
DRIVER              VOLUME NAME
local               node-red_node-red

View detailed information on volume:

$ docker volume inspect node-red_node-red
[
    {
        "CreatedAt": "2020-05-02T18:37:33Z",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "node-red",
            "com.docker.compose.version": "1.23.0",
            "com.docker.compose.volume": "node-red"
        },
        "Mountpoint": "/var/lib/docker/volumes/node-red_node-red/_data", # расолоТСниС нашСго ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°
        "Name": "node-red_node-red",
        "Options": null,
        "Scope": "local"
    }
]

Go to the volume directory. This directory already contains the files that docker created when the container was deployed.

$ sudo ls /var/lib/docker/volumes/node-red_node-red/_data
lib  package.json  settings.js

We are interested in the file settings.js. Open it and look for a piece of code adminAuth. Let's comment it out.

 // Securing Node-RED
    // -----------------
    // To password protect the Node-RED editor and admin API, the following
    // property can be used. See http://nodered.org/docs/security.html for details.
    adminAuth: {
        type: "credentials",
        users: [{
            username: "admin",
            password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
            permissions: "*"
        }]
    },

In key password: you need to insert the hash of the node-red password.

Getting password hash node-red
On any machine with node.js, install the node-red-admin package.

npm i node-red-admin -g

Run the package and set a password:

node-red-admin hash-pw

We start the container and connect to the port 11880.

http://192.168.0.100:11880/

An authorization window should appear.

Deploying and configuring node-red authentication on docker-compose

We enter the login password.

If everything works, restart the container with the key -d.

$ docker-compose up -d node-red

Something like that.

Source: habr.com

Add a comment