由于 bcrypt/Make 问题,无法在 Docker 中构建 TypeScript 应用程序

问题描述

我有一个https://github.com/w3tecch/express-typescript-boilerplate 分叉的 GraphqQL 后端应用程序

我在 Docker 中成功构建应用程序的分支已经很长时间了,但不久前它突然开始突然失败。我怀疑这是由于其中一个 Dockerfile 中的依赖项之一发生了变化。

这是我现在在 Docker 中构建应用程序时看到的:

=> ERROR [stage-build 3/5] RUN yarn install   
...
#12 29.39 error /project/node_modules/bcrypt: Command Failed.
#12 29.39 Exit code: 1
...
#12 29.39 gyp info spawn args [ 'BUILDTYPE=Release','-C','build' ]
#12 29.39 make: Entering directory '/project/node_modules/bcrypt/build'
#12 29.39 make: printf: Operation not permitted
#12 29.39 make: *** [bcrypt_lib.target.mk:103: Release/obj.target/bcrypt_lib/src/blowfish.o] Error 127
#12 29.39 make: Leaving directory '/project/node_modules/bcrypt/build'
#12 29.39 gyp ERR! build error 
#12 29.39 gyp ERR! stack Error: `make` Failed with exit code: 2
#12 29.39 gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
#12 29.39 gyp ERR! stack     at emitTwo (events.js:126:13)
#12 29.39 gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
#12 29.39 gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
#12 29.39 gyp ERR! System Linux 4.19.121-linuxkit
#12 29.39 gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/project/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/project/node_modules/bcrypt/lib/binding" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v57"
#12 29.39 gyp ERR! cwd /project/node_modules/bcrypt
#12 29.39 gyp ERR! node -v v8.15.1
#12 29.39 gyp ERR! node-gyp -v v3.8.0
#12 29.39 gyp ERR! not ok 
#12 29.39 node-pre-gyp ERR! build error 
#12 29.39 node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/project/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/project/node_modules/bcrypt/lib/binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v57' (1)
#12 29.39 node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/project/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
#12 29.39 node-pre-gyp ERR! stack     at emitTwo (events.js:126:13)
#12 29.39 node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
#12 29.39 node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:915:16)
#12 29.39 node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
#12 29.39 node-pre-gyp ERR! System Linux 4.19.121-linuxkit
#12 29.39 node-pre-gyp ERR! command "/usr/local/bin/node" "/project/node_modules/bcrypt/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
#12 29.39 node-pre-gyp ERR! cwd /project/node_modules/bcrypt
#12 29.39 node-pre-gyp ERR! node -v v8.15.1
#12 29.39 node-pre-gyp ERR! node-pre-gyp -v v0.11.0
#12 29.39 node-pre-gyp ERR! not ok 
#12 29.39 Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/project/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/project/node_modules/bcrypt/lib/binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v57' (1) executor Failed running [/bin/sh -c yarn install]: exit code: 1
------

Dockerfile 结构如下:

  1. olliecaine/base(一个包含 Node 的 Alpine 容器)
  2. olliecaine/dev(从 olliecaine/base 扩展以添加额外的开发依赖项,例如 Chromium)
  3. 应用程序的 Dockerfile 有 3 个阶段:
    1. Develop - 安装应用程序依赖项并运行 npm run dev
    2. 构建 - lints、测试和构建应用
    3. Host - 使用 olliecaine/base 来托管应用

这些 Dockerfile 可以在我的 devops 存储库中找到,地址为 https://github.com/olivercaine/devops

与该问题相关的所有信息都可以在 https://github.com/olivercaine/express-typescript-boilerplate/blob/bugfix/docker-build-error/README%20-%20BUILD%20ISSUE.md

中找到

如果有人对我如何调试/修复问题有任何建议,我将不胜感激!

解决方法

失败发生在 make 中,它试图编译包的二进制依赖项。 make 在返回 *** [bcrypt_lib.target.mk:103: Release/obj.target/bcrypt_lib/src/blowfish.o] Error 127 之前抛出以下错误 npm,退出代码为 2

此类问题通常与缺少依赖项(编译器工具、库等)有关。 GitHub 上的 bycrypt 已经有类似的问题。

根据 GitHub 线程,该库需要 C/C++ 编译器、make 和 Python 作为构建集依赖项。对于 alpine 这将是

RUN apk add --update alpine-sdk && \
    apk add libffi-dev openssl-dev && \
    apk add python-dev python3-dev