I'm running node-red in a Docker container in ubuntu and I want to read a csv file from the host.
I have used the instructions regarding permissions and persistence in the git hub repository node-red docker/wiki/permissions-and-persistence. Here the code lines with the results:
- I have checked the user with uid=1000 and created the directory for the bind mounting under this user. Afterwards I have checked who is the owner and is the same:
ich@hier:~$ id $USER
uid=1000(ich) gid=1000(ich) Gruppen=1000(ich)
ich@hier:~$ mkdir -p /home/ich/node-red
ich@hier:~$ ls -nal /home/ich/node-red
insgesamt 12
drwxrwxr-x 3 1000 1000 4096 Apr 13 21:09 .
drwxrwxrwx 23 1000 1000 4096 Apr 13 20:48 ..
drwxr-xr-x 2 1000 1000 4096 Apr 13 21:09 data
- Then I have created the docker container including the bind mounting to the directory
ich@hier:~$ sudo docker container run -d -it -p 1880:1880 --name node-red -v /home/ich/node-red/data:/data --network report_net nodered/node-red
b3461d6a4e12f8d7458cf500ea23d1c74043f2e33fbd3c9e87cab17f2afeec12
My flow in node-red is quite simple, just a timestamp that activates a read file node. The read file node should read the csv file and display it on the debug. Here the code
[
{
"id": "bdf3e780d80630ad",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ce335719d89260c3",
"type": "inject",
"z": "bdf3e780d80630ad",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 180,
"y": 100,
"wires": [
[
"52d41c28b5386790"
]
]
},
{
"id": "52d41c28b5386790",
"type": "file in",
"z": "bdf3e780d80630ad",
"name": "ReadAlarms",
"filename": "/home/ich/node-red/data/Alarms",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"allProps": false,
"x": 390,
"y": 100,
"wires": [
[
"cc3b34b5725078d1"
]
]
},
{
"id": "cc3b34b5725078d1",
"type": "debug",
"z": "bdf3e780d80630ad",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 610,
"y": 100,
"wires": []
}
]
The result is "Error: ENOENT: no such file or directory, open '/home/ich/node-red/data/Alarms'". But the file is there and if I use node-red locally it works and can read the file.
I have also tried with another flow to write a file in the data folder,
[
{
"id": "2b699dfbc6d1d4a8",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "9c9c3702e8276f8b",
"type": "inject",
"z": "2b699dfbc6d1d4a8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 120,
"wires": [
[
"62d2c90df60f9387"
]
]
},
{
"id": "62d2c90df60f9387",
"type": "function",
"z": "2b699dfbc6d1d4a8",
"name": "RandomArray",
"func": "const num=[];\nvar i;\n\nfor (i=0; i<3; i ){\n \nnum[i] = Math.floor(Math.random() * 11);\n\n}\n\nrandArray = {payload: num}\n\nreturn randArray;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 360,
"y": 120,
"wires": [
[
"116160c9d246d3b3"
]
]
},
{
"id": "116160c9d246d3b3",
"type": "file",
"z": "2b699dfbc6d1d4a8",
"name": "",
"filename": "/home/ich/node-red/testDocker.txt",
"appendNewline": true,
"createDir": true,
"overwriteFile": "false",
"encoding": "none",
"x": 640,
"y": 120,
"wires": [
[
"0a65dd6c28013d82"
]
]
},
{
"id": "0a65dd6c28013d82",
"type": "debug",
"z": "2b699dfbc6d1d4a8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 910,
"y": 120,
"wires": []
}
]
with the result "failed to create file: Error: EACCES: permission denied, mkdir '/home/ich/node-red/data' and when I run it locally it works.
I have checked and node-red is saving the flows and the installed modules on the directory and when I remove the container and I create a new one with the same bind mounting the flows are there. So node-red has the permission to write and read in the folder (at least the flows and the modules).
I'm running node-red under Docker in a machine with Ubuntu 20.
Does anyone knows what I'm missing here? Are other permissions required to read and write files outside the container with the "write file" and "read file" nodes? Any help will be welcome ;-)
I hope this is not too much text to read and you have all the information. If there is something else you need, do not hesitate to write.
Thank you!
CodePudding user response:
You can't access the file at /home/ich/node-red/data/Alarms
because that path does not exist inside the Docker container.
When you added the volume to the Docker container with the -v /home/ich/node-red/data:/data
command line argument you effectively mapped /home/ich/node-red/data
to the path /data
inside the container. So to access a file called Alarms
the path you need to use in Node-RED is /data/Alarms
In short, all your paths in Node-RED need to be relative to /data