Node-GYP 构建 Docker 镜像时找不到 Python

mrp*_*ett 1 python node.js npm docker

当我尝试构建我的 docker 镜像时,我不断收到此错误。我通过自制软件安装了Python,并且可以将它用于所有内容。VSC / PyCharm 等

但似乎node-gyp似乎无法找到可执行文件,该可执行文件位于/opt/homebrew/bin/python3

[+] Building 3.3s (9/10)                                                                                  
 => [internal] load build definition from Dockerfile                                                 0.0s
 => => transferring dockerfile: 243B                                                                 0.0s
 => [internal] load .dockerignore                                                                    0.0s
 => => transferring context: 35B                                                                     0.0s
 => [internal] load metadata for docker.io/library/node:16.17.0-slim                                 1.1s
 => [auth] library/node:pull token for registry-1.docker.io                                          0.0s
 => [internal] load build context                                                                    0.0s
 => => transferring context: 231B                                                                    0.0s
 => [1/5] FROM docker.io/library/node:16.17.0-slim@sha256:c921eb1ad35760716fff0d5f0f7d9a95a7b64ee8f  0.0s
 => CACHED [2/5] WORKDIR /app                                                                        0.0s
 => CACHED [3/5] COPY [package.json, package-lock.json*, ./]                                         0.0s
 => ERROR [4/5] RUN npm install                                                                      2.2s
------
 > [4/5] RUN npm install:
#9 2.116 npm notice 
#9 2.116 npm notice New minor version of npm available! 8.15.0 -> 8.19.1
#9 2.116 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.19.1>
#9 2.116 npm notice Run `npm install -g npm@8.19.1` to update!
#9 2.116 npm notice 
#9 2.117 npm ERR! code 1
#9 2.117 npm ERR! path /app/node_modules/bufferutil
#9 2.117 npm ERR! command failed
#9 2.117 npm ERR! command sh /tmp/install-f04042c7.sh
#9 2.118 npm ERR! gyp info it worked if it ends with ok
#9 2.118 npm ERR! gyp info using node-gyp@9.0.0
#9 2.118 npm ERR! gyp info using node@16.17.0 | linux | arm64
#9 2.118 npm ERR! gyp ERR! find Python 
#9 2.118 npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
#9 2.118 npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
#9 2.118 npm ERR! gyp ERR! find Python checking if "python3" can be used
#9 2.118 npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
#9 2.118 npm ERR! gyp ERR! find Python checking if "python" can be used
#9 2.118 npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
#9 2.118 npm ERR! gyp ERR! find Python 
#9 2.118 npm ERR! gyp ERR! find Python **********************************************************
#9 2.118 npm ERR! gyp ERR! find Python You need to install the latest version of Python.
#9 2.118 npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#9 2.118 npm ERR! gyp ERR! find Python you can try one of the following options:
#9 2.118 npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#9 2.118 npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
#9 2.118 npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
#9 2.118 npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
#9 2.118 npm ERR! gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
#9 2.118 npm ERR! gyp ERR! find Python For more information consult the documentation at:
#9 2.118 npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#9 2.118 npm ERR! gyp ERR! find Python **********************************************************
#9 2.118 npm ERR! gyp ERR! find Python 
#9 2.118 npm ERR! gyp ERR! configure error 
#9 2.118 npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
#9 2.118 npm ERR! gyp ERR! stack     at PythonFinder.fail (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:330:47)
#9 2.118 npm ERR! gyp ERR! stack     at PythonFinder.runChecks (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:159:21)
#9 2.118 npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:202:16)
#9 2.118 npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:294:16)
#9 2.118 npm ERR! gyp ERR! stack     at exithandler (node:child_process:408:5)
#9 2.118 npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:420:5)
#9 2.118 npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
#9 2.118 npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
#9 2.118 npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:478:16)
#9 2.118 npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
#9 2.118 npm ERR! gyp ERR! System Linux 5.10.104-linuxkit
#9 2.118 npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
#9 2.118 npm ERR! gyp ERR! cwd /app/node_modules/bufferutil
#9 2.118 npm ERR! gyp ERR! node -v v16.17.0
#9 2.118 npm ERR! gyp ERR! node-gyp -v v9.0.0
#9 2.118 npm ERR! gyp ERR! not ok
#9 2.119 
#9 2.119 npm ERR! A complete log of this run can be found in:
#9 2.119 npm ERR!     /root/.npm/_logs/2022-09-13T11_32_27_464Z-debug-0.log
------
executor failed running [/bin/sh -c npm install]: exit code: 1
Run Code Online (Sandbox Code Playgroud)

这是我的 Dockerfile

FROM node:16.17.0-slim

ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

RUN npm install

COPY . .

EXPOSE 3000
 
CMD [ "node", "./src/index.js" ]
Run Code Online (Sandbox Code Playgroud)
// package.json
{
  "name": "express-books",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "expressjs",
    "supabase",
    "node"
  ],
  "author": "Paul Bennett",
  "license": "ISC",
  "dependencies": {
    "@supabase/supabase-js": "^1.35.6",
    "cors": "^2.8.5",
    "dotenv": "^16.0.2",
    "express": "^4.18.1",
    "joi": "^17.6.0"
  },
  "devDependencies": {
    "eslint": "^8.23.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了回溯错误中 99.9% 的指令...但似乎无法弄清楚...任何帮助将不胜感激。

mrp*_*ett 7

我已经使用新的 Dockerfile 和额外的一行解决了该问题

FROM node:lts-alpine

ENV NODE_ENV=production

WORKDIR /usr/src/app

COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]

RUN apk add --update --no-cache python3 build-base gcc && ln -sf /usr/bin/python3 /usr/bin/python

RUN npm install -g npm@8.19.1
RUN npm install --production && mv node_modules ../

COPY . .

EXPOSE 3000

RUN chown -R node /usr/src/app

USER node

CMD ["node", "./src/index.js"]
Run Code Online (Sandbox Code Playgroud)

有了这条线。

RUN apk add --update --no-cache python3 build-base gcc && ln -sf /usr/bin/python3 /usr/bin/python
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题。