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.
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
.
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.
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.
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>
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"
}
}
}
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. ..
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