[Solved] Accessing stdin from container

I have a barcode scanner that acts as a keyboard interface, running on a RPi3. I’m running a multi container setup, where my Scanner service is supposed to read the input from the USB device which acts as a keyboard interface. For some reason, the container doesn’t seem to be receiving anything over stdin. My docker-compose.yml is setup as follows:
version: ‘2’
services:
scanner:
build:
context: ./apps/scanner
dockerfile: Dockerfile.template
privileged: true
stdin_open: true
tty: true

Anyone tackled similar issues to this? I’ve followed the discussion in this forum post: Input keyboard stdin but nothing from there solved my problems…

Hi @Craigson,

Are you able to try deploying this sample project to confirm that the device is configured properly? This test should help us understand if there is a problem with the device or rather your application container.

Please let us know if you receive input while testing that application!

That repo fails, both locally and deploying to a device:

[main] W: Failed to fetch http://deb.debian.org/debian/dists/jessie-updates/InRelease Unable to find expected entry ‘main/binary-armhf/Packages’ in Release file (Wrong sources.list entry or malformed file)
[main] E: Some index files failed to download. They have been ignored, or old ones used instead.
[main]
[main] Removing intermediate container 685d04b60fe0
[Info] Uploading images
[main] The command ‘/bin/sh -c apt-get update && apt-get install -yq python build-essential libusb-1.0-0-dev libudev-dev && apt-get clean && rm -rf /var/lib/apt/lists/’ returned a non-zero code: 100
[Success] Successfully uploaded images
[Error] Some services failed to build:
[Error] Service: main
[Error] Error: The command '/bin/sh -c apt-get update && apt-get install -yq python build-essential libusb-1.0-0-dev libudev-dev && apt-get clean && rm -rf /var/lib/apt/lists/
’ returned a non-zero code: 100
[Error] Not deploying release.

If you change the first line of the Dockerfile.template like so (dropping -slim from the base image), you should be able to build & test that project. I will make sure that project is fixed up as well, thanks for pointing it out!

FROM resin/%%BALENA_MACHINE_NAME%%-node:6

Same errors.

I changed to

FROM balenalib/%%BALENA_MACHINE_NAME%%-node:10-run

and managed to get past the initial errors. But there are issues in the HID package (same ones I see when running on my MacBook.

[main]     HID.target.mk:110: recipe for target 'Release/obj.target/HID/src/HID.o' failed
[main]     make: *** [Release/obj.target/HID/src/HID.o] Error 1
[main]
[main]     make: Leaving directory '/usr/src/app/node_modules/node-hid-stream/node_modules/node-hid/build'
[main]     gyp
[main]      ERR! build error
[main]
[main]     gyp ERR!
[main]     stack Error: `make` failed with exit code: 2
[main]
[main]     gyp
[main]
[main]     ERR!
[main]
[main]     stack
[main]          at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
[main]     gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
[main]
[main]     gyp
[main]     ERR!
[main]
[main]     stack
[main]          at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
[main]
[main]     gyp ERR! System
[main]      Linux 4.15.0-45-generic
[main]
[main]     gyp ERR! command
[main]      "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
[main]     gyp ERR! cwd
[main]      /usr/src/app/node_modules/node-hid-stream/node_modules/node-hid
[main]     gyp
[main]     ERR!
[main]      node -v v10.16.0
[main]     gyp
[main]     ERR!
[main]      node-gyp -v v3.8.0
[main]     gyp
[main]     ERR!
[main]      not ok
[main]
[main]     npm WARN app No description
[main]
[main]     npm WARN app No repository field.
[main]
[main]     npm
[main]
[main]     WARN app No license field.
[main]
[main]
[main]
[main]     npm ERR! code
[main]      ELIFECYCLE
[main]
[main]     npm ERR! errno
[main]      1
[main]
[main]     npm ERR! node-hid@0.5.7 install: `prebuild-install || node-gyp rebuild`
[main]
[main]     npm
[main]     ERR!
[main]      Exit status 1
[main]
[main]     npm ERR!
[main]
[main]     npm
[main]
[main]     ERR!
[main]      Failed at the node-hid@0.5.7 install script.
[main]     npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[main]
[main]
[main]     npm
[main]     ERR! A complete log of this run can be found in:
[main]
[main]     npm
[main]     ERR!
[main]          /root/.npm/_logs/2019-06-13T21_45_13_923Z-debug.log
[main]
[main]     Removing intermediate container 8b1a78ddd738
[Info]     Uploading images
[main]     The command '/bin/sh -c JOBS=MAX npm install --production --unsafe-perm && npm cache clean && rm -rf /tmp/*' returned a non-zero code: 1

Hi again,

I just successfully deployed that project to a device I have here locally, could you share exactly how you are running the build? Is it via a git remote or balenaCLI? If via our CLI, please provide the version if you could!

Thank you!

Deployed to a RPi3? Strange…

Currently deploying via git, git push balena master

CLI version 11.0.7

I have reproduced your error locally. If you use the node v6 base image rather than node v10, you should be able to compile the test project successfully.

I just tested that end to end with a Raspberry Pi 3 application and only the changes I mentioned above.

@xginn8 Unfortunately that’s not the case for me. Here’s my dockerfile:

FROM resin/%%BALENA_MACHINE_NAME%%-node:6

RUN apt-get update && apt-get install -yq \
    python build-essential libusb-1.0-0-dev \
    libudev-dev && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY package.json package.json

RUN JOBS=MAX npm install -g node-gyp
RUN JOBS=MAX npm install node-hid --build-from-source
RUN JOBS=MAX npm install --production --unsafe-perm && npm cache clean && rm -rf /tmp/*

COPY . ./

CMD ["npm", "start"]

And here’s the output from the build server:

[Info]     Starting build for keyboard-test, user dev25
[Info]     Dashboard link: https://dashboard.balena-cloud.com/apps/1477627/devices
[Info]     Building on arm01
[Info]     Pulling previous images for caching purposes...
[Success]  Successfully pulled cache images
[main]     Step 1/9 : FROM resin/raspberrypi3-node:6
[main]      ---> 4bd6c3fffce1
[main]     Step 2/9 : RUN apt-get update && apt-get install -yq     python build-essential libusb-1.0-0-dev     libudev-dev &&     apt-get clean && rm -rf /var/lib/apt/lists/*
[main]      ---> Running in 8eb7bbf706b7
[main]     Get:1 http://archive.raspbian.org jessie InRelease [15.0 kB]
[main]     Get:2 http://archive.raspberrypi.org jessie InRelease [22.9 kB]
[main]     Ign http://deb.debian.org jessie InRelease
[main]     Get:3 http://security.debian.org jessie/updates InRelease [44.9 kB]
[main]     Get:4 http://deb.debian.org jessie-updates InRelease [7340 B]
[main]     Get:5 http://deb.debian.org jessie Release.gpg [2420 B]
[main]     Get:6 http://deb.debian.org jessie Release [148 kB]
[main]     Get:7 http://archive.raspbian.org jessie/main armhf Packages [13.3 MB]
[main]     Get:8 http://archive.raspbian.org jessie/contrib armhf Packages [49.5 kB]
[main]     Get:9 http://archive.raspberrypi.org jessie/main armhf Packages [170 kB]
[main]     Get:10 http://archive.raspbian.org jessie/non-free armhf Packages [106 kB]
[main]     Get:11 http://archive.raspbian.org jessie/rpi armhf Packages [1297 B]
[main]     Get:12 http://archive.raspbian.org jessie/firmware armhf Packages [1208 B]
[main]     Get:13 http://security.debian.org jessie/updates/main armhf Packages [821 kB]
[main]     Get:14 http://deb.debian.org jessie/main armhf Packages [8898 kB]
[main]     Fetched 23.6 MB in 13s (1704 kB/s)
[main]     W: Failed to fetch http://deb.debian.org/debian/dists/jessie-updates/InRelease  Unable to find expected entry 'main/binary-armhf/Packages' in Release file (Wrong sources.list entry or malformed file)
[main]     E: Some index files failed to download. They have been ignored, or old ones used instead.
[main]
[main]     Removing intermediate container 8eb7bbf706b7
[Info]     Uploading images
[main]     The command '/bin/sh -c apt-get update && apt-get install -yq     python build-essential libusb-1.0-0-dev     libudev-dev &&     apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Success]  Successfully uploaded images
[Error]    Some services failed to build:
[Error]      Service: main
[Error]        Error: The command '/bin/sh -c apt-get update && apt-get install -yq     python build-essential libusb-1.0-0-dev     libudev-dev &&     apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Error]    Not deploying release.

remote: error: hook declined to update refs/heads/master
To git.balena-cloud.com:dev25/keyboard-test.git
 ! [remote rejected] master -> master (hook declined)

@Craigson, I see that @xginn8 updated the balena-io-playground/node-keyboard-input project yesterday following this conversation, and I note the following difference between the Dockerfiles your were experimenting with, and the one currently in the node-keyboard-input project:

. .
Your Dockerfile: FROM resin/%%BALENA_MACHINE_NAME%%-node:6
FROM balenalib/%%BALENA_MACHINE_NAME%%-node:10-run
node-keyboard-input: FROM balenalib/%%BALENA_MACHINE_NAME%%-node:6-run

I was successful(*) in running the following commands:

$ git clone git@github.com:balena-io-playground/node-keyboard-input.git
...
$ git remote add balena <user>@git.balena-cloud.com:<user>/<appName>.git
...
$ git push balena master
...
[Success]  Release successfully created!
...

I was also successful(*) in building the project on my laptop, using Docker and ARM emulation, with the balena CLI command:

$ balena build . -a <rpi-app-name> -e --logs

(*) Well, kind of successful. After deploying the project to an RPi 3, the keyboard wasn’t really working… I’m trying to figure out why. But at least it got past the image build phase without errors.

Thanks @pdcastro, I’ve also successfully managed to build and deploy to the Pi, but I have a feeling I’m running into the same issue as you, with the HID lib:

14.06.19 16:26:56 (-0400)  main  > @ start /usr/src/app
14.06.19 16:26:56 (-0400)  main  > node server.js
14.06.19 16:26:56 (-0400)  main  
14.06.19 16:26:58 (-0400)  main  /usr/src/app/node_modules/node-hid-stream/node_modules/node-hid/nodehid.js:24
14.06.19 16:26:58 (-0400)  main      this._raw = new (Function.prototype.bind.apply(binding.HID,
14.06.19 16:26:58 (-0400)  main                  ^
14.06.19 16:26:58 (-0400)  main  
14.06.19 16:26:58 (-0400)  main  Error: cannot open device with vendor id 0xf39 and product id 0x877
14.06.19 16:26:58 (-0400)  main      at Error (native)
14.06.19 16:26:58 (-0400)  main      at new HID (/usr/src/app/node_modules/node-hid-stream/node_modules/node-hid/nodehid.js:24:17)
14.06.19 16:26:58 (-0400)  main      at newHidDevice (/usr/src/app/node_modules/node-hid-stream/lib/hidstream.js:55:10)
14.06.19 16:26:58 (-0400)  main      at Hidstream (/usr/src/app/node_modules/node-hid-stream/lib/hidstream.js:21:19)
14.06.19 16:26:58 (-0400)  main      at KeyboardBase (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-base.js:9:19)
14.06.19 16:26:58 (-0400)  main      at Keyboard (/usr/src/app/node_modules/node-hid-stream/lib/keyboard.js:9:5)
14.06.19 16:26:58 (-0400)  main      at KeyboardBase (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-base.js:9:19)
14.06.19 16:26:58 (-0400)  main      at KeyboardCharacters (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-characters.js:8:5)
14.06.19 16:26:58 (-0400)  main      at Object.<anonymous> (/usr/src/app/server.js:5:18)
14.06.19 16:26:58 (-0400)  main      at Module._compile (module.js:577:32)
14.06.19 16:26:58 (-0400)  main  
14.06.19 16:26:58 (-0400)  main  npm ERR! Linux 4.14.98
14.06.19 16:26:58 (-0400)  main  npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
14.06.19 16:26:58 (-0400)  main  npm ERR! node v6.16.0
14.06.19 16:26:58 (-0400)  main  npm ERR! npm  v3.10.10
14.06.19 16:26:58 (-0400)  main  npm ERR! code ELIFECYCLE
14.06.19 16:26:58 (-0400)  main  npm ERR! @ start: `node server.js`
14.06.19 16:26:58 (-0400)  main  npm ERR! Exit status 1
14.06.19 16:26:58 (-0400)  main  npm ERR! 
14.06.19 16:26:58 (-0400)  main  npm ERR! Failed at the @ start script 'node server.js'.
14.06.19 16:26:58 (-0400)  main  npm ERR! Make sure you have the latest version of node.js and npm installed.
14.06.19 16:26:58 (-0400)  main  npm ERR! If you do, this is most likely a problem with the  package,
14.06.19 16:26:58 (-0400)  main  npm ERR! not with npm itself.
14.06.19 16:26:58 (-0400)  main  npm ERR! Tell the author that this fails on your system:
14.06.19 16:26:58 (-0400)  main  npm ERR!     node server.js
14.06.19 16:26:58 (-0400)  main  npm ERR! You can get information on how to open an issue for this project with:
14.06.19 16:26:58 (-0400)  main  npm ERR!     npm bugs 
14.06.19 16:26:58 (-0400)  main  npm ERR! Or if that isn't available, you can get their info via:
14.06.19 16:26:58 (-0400)  main  npm ERR!     npm owner ls 
14.06.19 16:26:58 (-0400)  main  npm ERR! There is likely additional logging output above.
14.06.19 16:26:58 (-0400)  main  
14.06.19 16:26:58 (-0400)  main  npm ERR! Please include the following file with any support request:
14.06.19 16:26:58 (-0400)  main  npm ERR!     /usr/src/app/npm-debug.log
14.06.19 16:27:00 (-0400) Service exited 'main sha256:c38071422bc4aff923896c0193d63410dbf71e18305bb7b588c221f98e26ba4c'
14.06.19 16:27:02 (-0400) Restarting service 'main sha256:c38071422bc4aff923896c0193d63410dbf71e18305bb7b588c221f98e26ba4c'
14.06.19 16:26:59 (-0400)  main  2019-06-14T20:27:04.853700000Z [..................] - : 
14.06.19 16:27:04 (-0400)  main  > @ start /usr/src/app
14.06.19 16:27:04 (-0400)  main  > node server.js
14.06.19 16:27:04 (-0400)  main  
14.06.19 16:27:06 (-0400)  main  /usr/src/app/node_modules/node-hid-stream/node_modules/node-hid/nodehid.js:24
14.06.19 16:27:06 (-0400)  main      this._raw = new (Function.prototype.bind.apply(binding.HID,
14.06.19 16:27:06 (-0400)  main                  ^
14.06.19 16:27:06 (-0400)  main  
14.06.19 16:27:06 (-0400)  main  Error: cannot open device with vendor id 0xf39 and product id 0x877
14.06.19 16:27:06 (-0400)  main      at Error (native)
14.06.19 16:27:06 (-0400)  main      at new HID (/usr/src/app/node_modules/node-hid-stream/node_modules/node-hid/nodehid.js:24:17)
14.06.19 16:27:06 (-0400)  main      at newHidDevice (/usr/src/app/node_modules/node-hid-stream/lib/hidstream.js:55:10)
14.06.19 16:27:06 (-0400)  main      at Hidstream (/usr/src/app/node_modules/node-hid-stream/lib/hidstream.js:21:19)
14.06.19 16:27:06 (-0400)  main      at KeyboardBase (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-base.js:9:19)
14.06.19 16:27:06 (-0400)  main      at Keyboard (/usr/src/app/node_modules/node-hid-stream/lib/keyboard.js:9:5)
14.06.19 16:27:06 (-0400)  main      at KeyboardBase (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-base.js:9:19)
14.06.19 16:27:06 (-0400)  main      at KeyboardCharacters (/usr/src/app/node_modules/node-hid-stream/lib/keyboard-characters.js:8:5)
14.06.19 16:27:06 (-0400)  main      at Object.<anonymous> (/usr/src/app/server.js:5:18)
14.06.19 16:27:06 (-0400)  main      at Module._compile (module.js:577:32)
14.06.19 16:27:06 (-0400)  main  

I’ve changed to the correct product and Vendor ID, but still no dice.

@Craigson, indeed when running the project on an RPi 3, I was getting the same error you posted above, “cannot open device with vendor id 0xf39 and product id 0x877”. Then I saw in the project’s README that I should edit the server.js file to use my keyboard’s ID, and I got it to work with the following steps:

  • On a device’s Host OS terminal (which can be opened via the web dashboard), I used the lsusb command to get my USB keyboard’s vendorId (0x04f2) and productId (0x0402):
$ lsusb
...
Bus 001 Device 007: ID 04f2:0402 Chicony Electronics Co., Ltd Genius LuxeMate i200 Keyboard
  • Edited the following line in server.js:
var characters = new KeyboardCharacters({ vendorId: 0x04f2, productId: 0x0402 });
  • Saved, committed and pushed:
$ git commit -am 'Update keyboard hardware ID'
$ git push balena master
  • On the device’s web dashboard, enabled “PUBLIC DEVICE URL”. Clicked on the link, which opened this page on a browser:
https://<long-device-UUID>.balena-devices.com/
  • Typed on they keyboard, watched characters being displayed on the web page and got incomprehensibly happy about it. :slight_smile:

So, it sounds like your issue was either in getting the correct hardware ID, or perhaps forgetting to commit the changes? Let us know how you get on with it.

Thanks @pdcastro, in a very strange turn of events the productId was different between my local environment and on the device, which requires further investigation. However I managed to get the sample to run, thank you for your assistance.