Build a Node-RED environment with Docker to move and understand

This article is the 20th day article of Node-RED Advent Calendar 2020.

This time, I would like to supplement and explain the LT that was held at the Node-RED UG Study Group 2020 End Online LT Party that I had previously spoken. I couldn't give a brief overview on the day due to time constraints, but I will introduce how to build a Node-RED environment with Docker with sample sources.

Move for the time being

First of all, I will introduce the method to operate Node-RED at least. In the official documentation, the command looks like this:

docker run -it -p 1880:1880 --name mynodered nodered/node-red

Here's a description of the command content for docker beginners so you can understand what's going on. However, it is a writing style that is likely to be typed every time the same command is executed. You can write a script so that it can be executed every time, but here I will use ** Docker Compose ** to behave like Docker. Docker Compose will bring out the necessary files by defining the service in YAML, but the command to execute the application will be simple. The above execution command is transcribed into YAML as follows. Be sure to name the file docker-compose.yaml.

docker-compose.yaml



services:
    mynodered:
        image: nodered/node-red
        ports:
            - "1880:1880"

The number of lines has increased and the description has increased, but I think it is easier to understand what you are doing than the previous command.

Save the created docker-compose.yaml in an appropriate directory, move to the destination of the YAML file in the terminal, and execute the following command.

cd COMPOSE/YAML/DIR
docker-compose up

Now you can use Node-RED by automatically installing and executing the image. You can stop the container with Ctrl + C.

image.png

Share config file

You can use Node-RED in the same way as before, but if you clean the container, the flow you have created and the nodes you have installed will disappear together, which is painful. Therefore, this time, share the directory where the Node-RED configuration file is saved and the working directory of the host OS on Docker so that the configuration file remains at hand. The Node-RED configuration file is located in the / data directory of the docker container. Based on that, if you modify docker-compose.yaml, it will be as follows.

docker-compose.yaml



services:
  mynodered:
    image: nodered/node-red
    volumes:
      - .:/data
    ports:
      - "1880:1880"

The difference from the previous YAML is that it includes the volumes option. It means sharing the / data directory of the docker container with the same directory as docker-compose.yaml. After making modifications and running docker-compose up, a Node-RED config file will be generated (or rather duplicated?). Now, even if you break the container, the configuration file will remain at hand.

Development environment for self-made nodes

I was able to run Node-RED with Docker, and I still have the configuration file at hand. Now you can build a development environment for your own node. If you use Docker, it will not be affected by environment dependence, so I think it will be an advantage when you create your own node. Rather, I chose Docker because I wanted to build an environment for developing my own nodes.

Preparation of necessary files

Create a development directory for your own node (here node-red-contrib-example-lower-case) on the same directory as docker-compose.yaml. After that, prepare the necessary files in First Node Development in the Node-RED documentation.

--package.json * This will explain how to prepare later


module.exports = function(RED) {
    function LowerCaseNode(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        node.on('input', function(msg) {
            msg.payload = msg.payload.toLowerCase();
            node.send(msg);
        });
    }
    RED.nodes.registerType("lower-case",LowerCaseNode);
}

<script type="text/javascript">
    RED.nodes.registerType('lower-case',{
        category: 'function',
        color: '#a6bbcf',
        defaults: {
            name: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "file.png ",
        label: function() {
            return this.name||"lower-case";
        }
    });
</script>

<script type="text/html" data-template-name="lower-case">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</script>

<script type="text/html" data-help-name="lower-case">
    <p>A simple node that converts the message payloads into all lower-case characters</p>
</script>

Prepare package.json

Create a package.json that goes into the Node-RED container and describes the contents of the npm module. You can't put it in the container without docker-compose up, so execute it in advance.

$ docker-compose exec mynodered bash
$ cd /data/node-red-contrib-example-lower-case
$ npm init

When you run npm init, you will be asked some questions, but the module name is node-red-contrib-example-lower-case, and the rest can be set as you like. When package.json is generated, write the following contents in the same way as the procedure in the document.

{
    "name" : "node-red-contrib-example-lower-case",
    ...
    "node-red" : {
        "nodes": {
            "lower-case": "lower-case.js"
        }
    }
}

Installation

Install the node library created to run the node under development in the directory containing the Node-RED configuration file. Officially, it will be moved to ~/.node-red, but in the case of Docker, the/data directory will be the directory where the Node-RED configuration file is saved, so execute the following command I will do it.

$ docker-compose exec mynodered bash
$ cd /data
$ npm install ./node-red-contrib-example-lower-case

When you finish the installation, restart the container and reload the browser, if lower-case is displayed in the function category in the node list on the left, you can check the operation of your own node. ..

Promotion

The self-made node library node-red-contrib-tello created in the Docker development environment has been released. I tried to make it easy to operate Tello, which is familiar with Tello, with Node-RED, so if you are using Tello, please install it!

Recommended Posts

Build a Node-RED environment with Docker to move and understand
Build a Node.js environment with Docker
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
How to build Rails 6 environment with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Build a Laravel / Docker environment with VSCode devcontainer
Prepare a scraping environment with Docker and Java
[Rails] How to build an environment with Docker
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to build docker environment with Gradle for intelliJ
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
Build WordPress environment with Docker (Local) and AWS (Production)
Try to build a Java development environment using Docker
Build a development environment to create Ruby on Jets + React apps with Docker
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build a Tomcat 8.5 environment with Pleiades 4.8
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to build Rails, Postgres, ElasticSearch development environment with Docker
I tried to create a padrino development environment with Docker
Build docker + laravel environment with laradock
I tried to build the environment of PlantUML Server with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
Use Jenkins to build inside Docker and then create a Docker image.
Steps to build a Ruby on Rails development environment with Vagrant
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
I tried to build a laravel operating environment while remembering Docker
Building Rails 6 and PostgreSQL environment with Docker
Create a MySQL environment with Docker from 0-> 1
Build a WAS execution environment from Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
Build a simple Docker + Django development environment
How to build an environment with Docker, which is the minimum required to start a Rails application
I built a rails environment with docker and mysql, but I got stuck
Build a Windows application test environment with Selenium Grid, Appium, and Windows Application Driver
Make a daily build of the TOPPERS kernel with Gitlab and Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
[Node.js express Docker] How to define Docker environment variables and load them with node.js
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a development environment for Docker + Rails6 + Postgresql
[Memo] Create a CentOS 8 environment easily with Docker
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows