解决Docker alpine构建镜像时的node-gyp错误

问题

有时构建 node 镜像,当安装的 npm 包需要使用 node-gyp 编译时,可能遇到如下错误❌:
image

为什么说有时呢,因为在本机并没有遇到此类错误,只是在运行 Coding 构建计划时出现了如此错误❌。

解决

node-gyp 构建的依赖是

  • Python
  • make
  • A proper C/C++ compiler toolchain, like GCC

所以可以在 Dockerfile 中加一行

RUN apk update && apk upgrade && apk add gcc make python3

但有时即使使用了以上命令,构建依然可能报错,例如 apline 镜像缺少一些必须的东西。为了避免这个问题,可以同时安装 musl-dev 包:

RUN apk update && apk upgrade && apk add gcc musl-dev g++ make python3

image

posted @ 2023-03-21 15:36  复读机§  阅读(446)  评论(0编辑  收藏  举报