Serialport use leads to segmentation fault when using docker

We're running node-red in Docker, because we have an IoT scenario using K3S at hand (currently we're in the final stages of development) and need to be able to use K3S / Helm to update the node-red application running on our Raspi4B (64 bit, because we need a lot of supplemental services running as well, some of them not being available for 32 bit anymore) appliance, from devops / operations pov this is just another service.

However, when starting the docker image (we tried multiple versions) right when opening the serial port we get a segmentation fault. We do it correctly (--privileged, --group-add dialout, etc...) and made sure we also did the npm install serialport manually, with all kinds of versions (someone reported that using @7 before doing it again without version fixes it, we can't compile @7, though).

Is there any workaround for this problem? Any way we can get serial ports (which are crucial to hardware control in our case) working from inside a docker container?

Not using docker is not an acceptable solution for us because solution design and software management requires that everything that is running on the devices must be under K3S control.

I assume you realise the serial node uses C bindings and requires either the correct binaries or compilation on the guest?

Does your image have the necessary tools installed to compile the serial port?

See this issue: serialport@10.3.0 fails in docker container on rpi · Issue #2438 · serialport/node-serialport · GitHub - might be relevant.

Thank you very much!

We just tried another thing:

const { SerialPort } = require("serialport");
var port = new SerialPort({path: '/dev/ttyUSB0', baudRate: 9600});

leads to the same issue.

Will have a look at what you wrote regarding C bindings now.

We tried now.

Did:

npm rebuild --build-from-source

Same difference - even my very short two line script still crashes.

Also tried

npm rebuild --build-from-source --force

to no avail.

Both node-red as well as my short twoliner both still crash with Segmentation Fault.

Where did you run npm rebuild --build-from-source (from what path)?
What was the output of npm rebuild --build-from-source?

1 Like

Copied it for you from console:

bash-5.1$ pwd

/usr/src/node-red

bash-5.1$ npm rebuild --build-from-source

rebuilt dependencies successfully

bash-5.1$ node ./test.js

Segmentation fault (core dumped)

bash-5.1$ cat ./test.js

const { SerialPort } = require("serialport");

var port = new SerialPort({path: '/dev/ttyUSB0', baudRate: 9600});

Starting node-red looks as follows:

bash-5.1$ npm --no-update-notifier --no-fund start --cache /data/.npm -- --userDir /data

node-red-docker@3.0.2 start
node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS "--userDir" "/data"

24 Oct 11:54:26 - [info]

Welcome to Node-RED

24 Oct 11:54:26 - [info] Node-RED version: v3.0.2
24 Oct 11:54:26 - [info] Node.js version: v16.16.0
24 Oct 11:54:26 - [info] Linux 5.15.70-v7l+ arm LE
24 Oct 11:54:28 - [info] Loading palette nodes
24 Oct 11:54:29 - [info] Settings file : /data/settings.js
24 Oct 11:54:29 - [info] Context store : 'default' [module=memory]
24 Oct 11:54:29 - [info] User directory : /data
24 Oct 11:54:29 - [warn] Projects disabled : editorTheme.projects.enabled=false
24 Oct 11:54:29 - [info] Flows file : /data/flows.json
24 Oct 11:54:29 - [warn]


Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.

24 Oct 11:54:29 - [warn] Encrypted credentials not found
24 Oct 11:54:29 - [info] Server now running at http://127.0.0.1:1880/
24 Oct 11:54:29 - [info] Starting flows
Segmentation fault (core dumped)

More info:

bash-5.1$ node --version

v16.16.0

bash-5.1$ npm --version

8.11.0

We're running a Raspi4 with 8 GB of RAM, 16 GB SD card and a USB-to-RS485 converter cable. Version of OS is the official 64 bit image from the RPi foundation.

Could you cd to the node-red-contrib-serialport directory and try again?

If that doesnt work, cd to the node-serialport directory and try once more?

These will be where contrib nodes are installed, typically cd ~/.node-red/node_modules
(I assume your image doesnt globally install node-red nodes?)

1 Like

Managed to make it run with my test script. I will have to find the modules used by node red now, hm.

Any idea where they might be? :slight_smile:

probably under /data/node_modules

But as i said before,

1 Like

It works! Misunderstood the "globally" part, thats why I was blocked! :slight_smile:

Have a look at this now:

24 Oct 12:15:08 - [warn] Encrypted credentials not found

24 Oct 12:15:08 - [info] Server now running at http://127.0.0.1:1880/

24 Oct 12:15:08 - [info] Starting flows

24 Oct 12:15:08 - [info] Started flows

24 Oct 12:15:09 - [info] [serialconfig:0948801d4dfda728] serial port /dev/ttyUSB0 opened at 9600 baud 8N1

Thank you very much for your great help! We will bring this into a dockerfile and we're good to go! :slight_smile:

1 Like