How to Dockerize Nodejs Barcode Reader on Linux
If you want to get a Node.js application into a Docker container from scratch, it’s recommended to read the official guide – Dockerizing a Node.js web app. It is useful, but not enough for a complicated case. In this post, I’m going to share what problems I have encountered while Dockerizing the Node.js barcode reader app using Dynamsoft Barcode Reader for Linux.
Nodejs Barcode Addon and App
If you have no idea how to use the Dynamsoft Barcode Reader SDK to create a Node.js barcode addon, please fork the repository on GitHub.
Dockerize the Node.js App
According to the official tutorial, I created the Dockerfile like this:
FROM node:latest
RUN mkdir -p /usr/src/dbr
WORKDIR /usr/src/dbr
# Install app dependencies
# If you want to download relevant Node.js modules when building the Docker image, uncomment following lines.
#COPY package.json /usr/src/dbr
#RUN npm install
COPY libDynamsoftBarcodeReaderx64.so ./
RUN ln -s /usr/src/dbr/libDynamsoftBarcodeReaderx64.so /usr/lib/libDynamsoftBarcodeReaderx64.so
COPY . ./
EXPOSE 2016
CMD [ "npm", "start" ]
Select the matched Node.js version
In the official tutorial, the image used in Dockerfile is argon:
FROM node:argon
Here is mine:
FROM node:latest
What is the difference? Visit the docker-library on GitHub, you will see the detailed descriptions about the Dockerfiles. The argon version is based on v4.2, whereas the latest version is based on v5.5.
I created the addon using v5.x. When I tried to load the addon using the argon image, I got the following exceptions:
return process.dlopen(module, path._makeLong(filename));
^
Error: Module version mismatch. Expected 46, got 47.
at Error (native)
at Object.Module._extensions..node (module.js:460:18)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
Remember to check whether the Node.js version of building your addon and app matches the version installed in Docker image when you get the similar error.
Copy shared libraries to Docker image
Firstly, you have to copy libDynamsoftBarcodeReaderx64.so from **
cp <Dynamsoft>/BarcodeReader4.0/Redist/ libDynamsoftBarcodeReaderx64.so <project>/libDynamsoftBarcodeReaderx64.so
Because the Node.js barcode reader app relies on libDynamsoftBarcodeReaderx64.so, you need to use the Docker instruction “**COPY
COPY libDynamsoftBarcodeReaderx64.so /usr/lib/libDynamsoftBarcodeReaderx64.so
Note: **the
Alternatively, you can copy the so file to the project folder and generate a symlink:
COPY libDynamsoftBarcodeReaderx64.so ./
RUN ln -s /usr/src/dbr/libDynamsoftBarcodeReaderx64.so
Bundle your project
Add all files of your project to the Docker image:
RUN mkdir -p /usr/src/dbr
WORKDIR /usr/src/dbr
COPY . ./
Expose the port
We need to map the port in order to access the Web app.
EXPOSE 2016
Define the command to run the barcode reader
CMD [ "npm", "start" ]
Build Docker Image
sudo docker build -t dynamsoft/dbr .
Run the Image
sudo docker run -p 2016:2016 dynamsoft/dbr
# Or run in background
sudo docker run -p 2016:2016 -d dynamsoft/dbr