如何解决在将bcrypt添加到package.json时,使用docker节点alpine镜像出现“找不到任何Python安装”的问题?

36

在我的package.json中添加bcrypt之前,一切都正常。现在,我收到了以下错误消息。

这是我package.json的摘录:

  "dependencies": {
    "bcrypt": "3.0.6",
    "express": "^4.17.1",
    "mongodb": "^3.3.1",
    "nodemailer": "^6.3.0",
    "pm2": "^3.5.1",
    "redis": "^2.8.0",
    "xlsx": "^0.15.0"
  },

这是我的Dockerfile。我正在使用官方的Node Alpine镜像。我想知道Alpine是否已经安装了Python。


FROM node:13.5.0-alpine

WORKDIR /usr/app

COPY ./src .

RUN npm install

当我运行docker-compose时,出现了这个错误信息:

node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v3.0.6/bcrypt_lib-v3.0.6-node-v79-linux-x64-musl.tar.gz
node-pre-gyp WARN Pre-built binaries not found for bcrypt@3.0.6 and node@13.5.0 (node-v79 ABI, musl) (falling back to source compile with node-gyp)
gyp ERR! find Python
gyp ERR! find Python Python is not set from command line or npm configuration
gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! find Python checking if "python" can be used
gyp ERR! find Python - "python" is not in PATH or produced an error
gyp ERR! find Python checking if "python2" can be used
gyp ERR! find Python - "python2" is not in PATH or produced an error
gyp ERR! find Python checking if "python3" can be used
gyp ERR! find Python - "python3" is not in PATH or produced an error
gyp ERR! find Python
gyp ERR! find Python **********************************************************
gyp ERR! find Python You need to install the latest version of Python.
gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
gyp ERR! find Python you can try one of the following options:
gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
gyp ERR! find Python   (accepted by both node-gyp and npm)
gyp ERR! find Python - Set the environment variable PYTHON
gyp ERR! find Python - Set the npm configuration variable python:
gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
gyp ERR! find Python For more information consult the documentation at:
gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
gyp ERR! find Python **********************************************************
gyp ERR! find Python

gyp ERR! configure error
gyp ERR! stack Error: Could not find any Python installation to use
gyp ERR! stack     at PythonFinder.fail (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:307:47)
gyp ERR! stack     at PythonFinder.runChecks (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:136:21)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:179:16)
gyp ERR! stack     at PythonFinder.execFileCallback (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:271:16)
gyp ERR! stack     at exithandler (child_process.js:309:5)
gyp ERR! stack     at ChildProcess.errorhandler (child_process.js:321:5)
gyp ERR! stack     at ChildProcess.emit (events.js:305:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
gyp ERR! stack     at onErrorNT (internal/child_process.js:469:16)
gyp ERR! stack     at processTicksAndRejections (internal/process/task_queues.js:84:21)
gyp ERR! System Linux 4.15.0-47-generic
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/usr/app/node_modules/bcrypt/lib/binding" "--napi_version=5" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v79"
gyp ERR! cwd /usr/app/node_modules/bcrypt
gyp ERR! node -v v13.5.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
node-pre-gyp ERR! build error
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 configure --fallback-to-build --module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/app/node_modules/bcrypt/lib/binding --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v79' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/usr/app/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:305:20)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:1028:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
node-pre-gyp ERR! System Linux 4.15.0-47-generic
node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/app/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/app/node_modules/bcrypt
node-pre-gyp ERR! node -v v13.5.0
node-pre-gyp ERR! node-pre-gyp -v v0.12.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/app/node_modules/bcrypt/lib/binding --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v79' (1)
npm WARN testapp@0.0.1 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.11: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bcrypt@3.0.6 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bcrypt@3.0.6 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-24T14_22_05_060Z-debug.log
ERROR: Service 'web' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 1

我该如何解决这个问题?我需要在Alpine上安装Python还是只需要添加环境变量?如果需要,请告诉我如何操作。我已经搜索了很多解决方案,但都没有成功...


3
人们喜欢使用阿尔卑斯基础镜像,因为它们非常小,由于它们非常小,所以不包含那些你没有明确安装的语言解释器等内容。 - David Maze
8个回答

72
在 npm install 之前,在您的 Docker 文件中添加以下内容:
RUN apk add --update python make g++\
   && rm -rf /var/cache/apk/*

如果您正在使用Python 3,请记得使用python3而不是python。


8
实际上这就是正确的答案。npm会构建本地包,而node-gyp依赖于Python。此外,如果您不想在最终镜像中包含那些构建工具,您可以使用多阶段构建。https://docs.docker.com/develop/develop-images/multistage-build/ - Kennyhyun
我认为这个应该是被接受的答案,因为如果它在alpine镜像上运行,就不需要不必要地使用笨重的node镜像。但是目前在使用node:14-alpine版本时,它对我来说失败了。 - Vaulstein
Python2已经停止维护,现在唯一的选择是使用Python3。 - Vaulstein

42

以管理员权限打开PowerShell,并运行以下命令,然后安装bcrypt

Windows: npm install --global --production windows-build-tools


1
这仅适用于Windows。 - Jeremy

30

Linux Alpine是非常精简的镜像。如果性能不是限制,您可能希望考虑使用官方node镜像,它具有您所需的所有依赖项,只需担心开发:https://github.com/nodejs/docker-node因此,您可以拥有这样的Dockerfile

FROM node:10
WORKDIR /usr/app
COPY ./src .
RUN npm install
EXPOSE 3000 # Use here the port you want to expose

1
不是最优解,但可能是最容易应用的答案。 - Timothy

17

为了完整起见:我解决了它。

解决方案是用 "bcryptjs" 代替依赖项 "bcrypt"。

使用bcryptjs没有错误。无论是标准的node docker镜像还是node alpine docker镜像都没有问题。


15
在Dockerfile中运行Node服务器的Compose文件

FROM node:16.8.0-alpine
ENV SERVER_HOME=/usr/src/server/
WORKDIR $SERVER_HOME
COPY ./package*.json $SERVER_HOME
RUN npm install -g npm@7.24.0
RUN apk add --update python3 make g++ && rm -rf /var/cache/apk/*
RUN npm install
COPY . $SERVER_HOME
#EXPOSE 5000
CMD npm run build
CMD npm run start

在alpine镜像中安装node模块时遇到了问题,因为在alpine中找不到python,所以我使用python3运行它。 RUN apk add --update python3 make g++ && rm -rf /var/cache/apk/* 如果一切正常,那么一切都能够很好地工作。如果仍然出现问题,请把npm install替换为npm install --force。


13

阿尔卑斯山的正确解决方案是:

RUN apk add --no-cache python3 make g++

6

0

对我来说,问题出在pg-native依赖项上,node-gyp使用不正确。

从此链接中找到了解决方案 - node-gyp failure pg-native

以下是帮助的评论节选:

A:不要注意到错用了两个不同的数据库库:即pg和pg.js。

B:小心使用像sequelize这样的东西,您可能已经在初始化与DB的连接时标记了“本地”选项。这也经常会导致此错误。

C:最后,我经常完全删除pg-native库。它会引起奇怪的错误,这些错误与node-gyp不一致。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接