Search code examples
node.jsdockerserial-portnode-gyp

Running nodejs serialport in a docker container


I need to run a nodejs application in a docker container. I'm not an expert in Linux so it's a bit hard to me to understand ho to do that. The whole application stored in github (https://github.com/kashesandr/NRTC). The app uses a serialport module (https://github.com/voodootikigod/node-serialport) that is compiled with node-gyp and in my case a serialport is a virtual one that uses a USB2Serial driver (http://www.prolific.com.tw/US/ShowProduct.aspx?pcid=41) I want to create a separate docker container for the app. Could you please help me?


Solution

  • This question is very vague. There is an official image at docker hub for building node based images. There is plenty of "how to" info in the image's readme. The only tricky part seems to me is how to access the serial port from within the container. I believe it's only possible by running the container in privileged mode, while ensuring that the device node exists inside the container as well. Of course the USB2Serial driver need to be installed on the host operating system.

    I'd suggest spin up the official node image in interactive mode, and try to install / run your app inside it manually, then you could figure out a script based on that later:

    docker run -it --privileged -v /dev:/dev -v path-to-your-app:/usr/src/your-app node:4.4.0 /bin/bash
    
    root@3dd71f11f02f:/# node --version
    v4.4.0
    root@3dd71f11f02f:/# npm --version
    2.14.20
    root@3dd71f11f02f:/# gcc --version
    gcc (Debian 4.9.2-10) 4.9.2
    

    As you see this would give you an interactive (-it) root access inside the container, which has everything you probably need, with an identical /dev structure as on the host os (-v /dev:/dev binds it), so there should be no problem accessing ports. (refine the -v /dev:/dev volume binding to something more specific later for security reasons). If you need everything else which is not installed by default, add it via apt-get (e.g. apt-get update && apt-get install [package]), as the official node image is based on Debian Jessie.

    After you figured out how to run the app (npm install, gyp whatever), writing a Dockerfile should be trivial.

    FROM node:4.4.0
    
    RUN npm install ...\
      && steps\
      && to && be && executed && inside && the && image
    
    CMD /your/app/start/script.sh
    

    ... and do a docker build, then run your image with --privileged, in non interactive (without -it) in production.