Node alpine image compilation error with mongodb-client-encryption

Viewed 2562

I am using Docker with node:12.18.3-alpine image. I added mongodb-client-encryption to package.json but when i'm installing packages inside image it gives me error. Everything works on node:12.18.3 image. Do i have to install anything else besides python, g++ and make? I created minimal reproduction:

Package.json:

{
  "name": "MONG-ENCRPT-TEST",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "mongodb-client-encryption": "^1.1.0"
  }
}

This is my Dockerfile:

FROM node:12.18.3-alpine

WORKDIR /user/app

COPY package.json .
COPY yarn.lock .

RUN apk update && apk add python g++ make && rm -rf /var/cache/apk/*
RUN yarn

CMD echo ok

docker-compose.yml:

version: '3.8'
services:
  api-test-mongo:
    restart: unless-stopped
    build:
      context: ./
      dockerfile: ./Dockerfile
    command: echo ok

And this is the error after running docker-compose-up:

[5/5] Building fresh packages...
error /user/app/node_modules/mongodb-client-encryption: Command failed.
Exit code: 1
Command: prebuild-install --tag-prefix node-v || node-gyp rebuild
Arguments:
Directory: /user/app/node_modules/mongodb-client-encryption
Output:
prebuild-install WARN install No prebuilt binaries found (target=12.18.3 runtime=node arch=x64 libc=musl platform=linux)
gyp info it worked if it ends with ok
gyp info using node-gyp@5.1.1
gyp info using node@12.18.3 | linux | x64
gyp info find Python using Python version 2.7.18 found at "/usr/bin/python"
gyp http GET https://unofficial-builds.nodejs.org/download/release/v12.18.3/node-v12.18.3-headers.tar.gz
gyp http 200 https://unofficial-builds.nodejs.org/download/release/v12.18.3/node-v12.18.3-headers.tar.gz
gyp http GET https://unofficial-builds.nodejs.org/download/release/v12.18.3/SHASUMS256.txt
gyp http 200 https://unofficial-builds.nodejs.org/download/release/v12.18.3/SHASUMS256.txt
gyp info spawn /usr/bin/python
gyp info spawn args [
gyp info spawn args   '/user/app/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/user/app/node_modules/mongodb-client-encryption/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/user/app/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/root/.cache/node-gyp/12.18.3/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/root/.cache/node-gyp/12.18.3',
gyp info spawn args   '-Dnode_gyp_dir=/user/app/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/root/.cache/node-gyp/12.18.3/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/user/app/node_modules/mongodb-client-encryption',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/user/app/node_modules/mongodb-client-encryption/build'
  CXX(target) Release/obj.target/mongocrypt/src/mongocrypt.o
In file included from ../src/mongocrypt.cc:1:
../src/mongocrypt.h:8:10: fatal error: mongocrypt/mongocrypt.h: No such file or directory
    8 | #include <mongocrypt/mongocrypt.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [mongocrypt.target.mk:113: Release/obj.target/mongocrypt/src/mongocrypt.o] Error 1
make: Leaving directory '/user/app/node_modules/mongodb-client-encryption/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/user/app/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Linux 4.19.76-linuxkit
gyp ERR! command "/usr/local/bin/node" "/user/app/node_modules/.bin/node-gyp" "rebuild"
gyp ERR! cwd /user/app/node_modules/mongodb-client-encryption
gyp ERR! node -v v12.18.3
gyp ERR! node-gyp -v v5.1.1
gyp ERR! not ok
2 Answers

Docker build was success when "mongodb-client-encryption": "1.2.1" in package.json

Higher version of NODEJS, try to use latest version of mongodb-client-encryption.

What I did to make this work was not tinker with my NODE version nor the mongo-client-encryption package.

When using NODE v14 it gives no error. For me, the update to NODE 16 broke things on Linux and Mac.

What I did to fix it: Remove mongo-client-encryption from my pakcage.json and add it as an external to your webpack config and let webpack handle it.

// webpack.config.js
module.exports = {
  ...
  externals: ["mongodb-client-encryption"]
}

Used with package.json:

"dependencies": {
  ...
  "mongoose": "5.11.15",
  "mongoose-auto-increment": "5.0.1",
},

References:

Related