"Failed to load gRPC binary module" 运行 Sails.js 应用程序在 Docker 容器内时出错

"Failed to load gRPC binary module" error when running a Sails.js application inside a Docker container

我有一个本地 Docker 开发人员环境来构建 Sails.js 应用程序。这是我的 Docker 文件的样子:

FROM node:8.12
LABEL Name=us.gcr.io/my-project/my-app Version=1.0.0

# Container configuration
RUN npm install -g sails@1.0.2 grunt@1.0.3 nodemon@1.18.4
WORKDIR /usr/src/app
COPY ./server/package.json ./package.json
RUN npm install
VOLUME /usr/src/app
EXPOSE 1337

这是我的 docker-compose.yml 文件的样子:

version: '3.4'

services:
  server:
    image: us.gcr.io/my-project/my-app:latest
    build: .
    environment:
      NODE_ENV: development
      IS_DEV_MACHINE: "yes"
    ports:
      - 1338:1337 # HOST_PORT is 1339 to avoid conflicts with other Sails.js apps running on host
    volumes:
      - ./server:/usr/src/app
    entrypoint: nodemon

  mongodb:
    image: mongo:4
    ports:
      - 27018:27017 # HOST_PORT is 27018 to avoid conflicts with other MongoDB databases running on host
    volumes:
      - ../database:/data/db

通常,一切正常,但是,我最近在我的应用程序中导入并初始化了 @google-cloud/logging NPM 包,现在,当我 运行 docker-compose up 时,我收到以下错误:

server_1   | error: Bootstrap encountered an error: (see below)
server_1   | error: Failed to lift app: { Error: Failed to load gRPC binary module because it was not installed for the current system
server_1   | Expected directory: node-v57-linux-x64-musl
server_1   | Found: [node-v57-darwin-x64-unknown]
server_1   | This problem can often be fixed by running "npm rebuild" on the current system
server_1   | Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v57-linux-x64-musl/grpc_node.node'
server_1   |     at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/grpc_extension.js:53:17)
server_1   |     at Module._compile (module.js:653:30)
server_1   |     at Object.Module._extensions..js (module.js:664:10)
server_1   |     at Module.load (module.js:566:32)
server_1   |     at tryModuleLoad (module.js:506:12)
server_1   |     at Function.Module._load (module.js:498:3)
server_1   |     at Module.require (module.js:597:17)
server_1   |     at require (internal/module.js:11:18)
server_1   |     at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client_interceptors.js:145:12)
server_1   |     at Module._compile (module.js:653:30)
server_1   |     at Object.Module._extensions..js (module.js:664:10)
server_1   |     at Module.load (module.js:566:32)
server_1   |     at tryModuleLoad (module.js:506:12)
server_1   |     at Function.Module._load (module.js:498:3)
server_1   |     at Module.require (module.js:597:17)
server_1   |     at require (internal/module.js:11:18) code: 'MODULE_NOT_FOUND' }
mongodb_1  | 2018-10-09T09:27:52.521+0000 I NETWORK  [conn4] end connection 172.19.0.2:48730 (0 connections now open)

在上面的错误中,"Bootstrap" 是我的 Sails.js 项目中初始化 @google-cloud/logging 的 bootstrap.js 文件。这就是我在 bootstrap.js:

中初始化记录器的方式
// Globally required packages
const { Logging } = require('@google-cloud/logging');
const logging = new Logging({ projectId: 'my-project' });
const logger = logging.log('test');

我似乎无法弄清楚为什么会出现此错误。我什至尝试将我的基础 Docker 图像更改为官方 Google App Engine Docker 图像 (gcr.io/google-appengine/nodejs),但这也无济于事。我在任何地方都找不到解决这个问题的方法。感谢任何帮助。

看起来项目很难找到依赖项,例如 gRPC 二进制模块(用 npm install grpc 修复)或者它们当前所在的位置 - more info here

您有没有在本地 Mac (darwin) 环境中执行 npm install 而应该在容器 (linux) 中执行? grpc 二进制文件用于错误的 OS,因此是错误的。这通常发生在本地 Mac 运行 一个 linux Docker 容器中。

Expected directory: node-v57-linux-x64-musl Found: [node-v57-darwin-x64-unknown]

解决方案是 rm -fr node_modules 并在容器中执行 npm install

通过以下命令重建应用程序。

npm rebuild --target=8.1.0 --target_platform=linux --target_arch=x64 --target_libc=musl