【问题标题】:Node.JS: Trying to run docker project with docker-compose up exits due to node-sass and node-gypNode.JS:由于 node-sass 和 node-gyp,尝试使用 docker-compose up 退出运行 docker 项目
【发布时间】:2021-12-30 23:27:01
【问题描述】:

我正在尝试从 MacBook Air M1 运行我的一个 Docker 工作项目,但在执行 docker-compose up 时出现以下错误。显然 它找不到 python 安装,即使我可以从终端同时执行 python --versionpython3 --version。我对 Docker 比较陌生,但我尝试对所有可能的事情进行故障排除以解决此错误

#8 91.79 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
#8 91.79 error /app/node_modules/node-sass: Command failed.
#8 91.79 Exit code: 1
#8 91.79 Command: node scripts/build.js
#8 91.79 Arguments: 
#8 91.79 Directory: /app/node_modules/node-sass
#8 91.79 Output:
#8 91.79 Building: /usr/local/bin/node /app/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
#8 91.79 gyp info it worked if it ends with ok
#8 91.79 gyp verb cli [
#8 91.79 gyp verb cli   '/usr/local/bin/node',
#8 91.79 gyp verb cli   '/app/node_modules/node-gyp/bin/node-gyp.js',
#8 91.79 gyp verb cli   'rebuild',
#8 91.79 gyp verb cli   '--verbose',
#8 91.79 gyp verb cli   '--libsass_ext=',
#8 91.79 gyp verb cli   '--libsass_cflags=',
#8 91.79 gyp verb cli   '--libsass_ldflags=',
#8 91.79 gyp verb cli   '--libsass_library='
#8 91.79 gyp verb cli ]
#8 91.79 gyp info using node-gyp@7.1.2
#8 91.79 gyp info using node@16.13.0 | linux | arm64
#8 91.79 gyp verb command rebuild []
#8 91.79 gyp verb command clean []
#8 91.79 gyp verb clean removing "build" directory
#8 91.79 gyp verb command configure []
#8 91.79 gyp verb find Python Python is not set from command line or npm configuration
#8 91.79 gyp verb find Python Python is not set from environment variable PYTHON
#8 91.79 gyp verb find Python checking if "python3" can be used
#8 91.79 gyp verb find Python - executing "python3" to get executable path
#8 91.79 gyp verb find Python - "python3" is not in PATH or produced an error
#8 91.79 gyp verb find Python checking if "python" can be used
#8 91.79 gyp verb find Python - executing "python" to get executable path
#8 91.79 gyp verb find Python - "python" is not in PATH or produced an error
#8 91.79 gyp verb find Python checking if "python2" can be used
#8 91.79 gyp verb find Python - executing "python2" to get executable path
#8 91.79 gyp verb find Python - "python2" is not in PATH or produced an error
#8 91.79 gyp ERR! find Python 
#8 91.79 gyp ERR! find Python Python is not set from command line or npm configuration
#8 91.79 gyp ERR! find Python Python is not set from environment variable PYTHON
#8 91.79 gyp ERR! find Python checking if "python3" can be used
#8 91.79 gyp ERR! find Python - "python3" is not in PATH or produced an error
#8 91.79 gyp ERR! find Python checking if "python" can be used
#8 91.79 gyp ERR! find Python - "python" is not in PATH or produced an error
#8 91.79 gyp ERR! find Python checking if "python2" can be used
#8 91.79 gyp ERR! find Python - "python2" is not in PATH or produced an error
#8 91.79 gyp ERR! find Python 
#8 91.79 gyp ERR! find Python **********************************************************
#8 91.79 gyp ERR! find Python You need to install the latest version of Python.
#8 91.79 gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#8 91.79 gyp ERR! find Python you can try one of the following options:
#8 91.79 gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#8 91.79 gyp ERR! find Python   (accepted by both node-gyp and npm)
#8 91.79 gyp ERR! find Python - Set the environment variable PYTHON
#8 91.79 gyp ERR! find Python - Set the npm configuration variable python:
#8 91.79 gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
#8 91.79 gyp ERR! find Python For more information consult the documentation at:
#8 91.79 gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#8 91.79 gyp ERR! find Python **********************************************************
#8 91.79 gyp ERR! find Python 
#8 91.79 gyp ERR! configure error 
#8 91.79 gyp ERR! stack Error: Could not find any Python installation to use
#8 91.79 gyp ERR! stack     at PythonFinder.fail (/app/node_modules/node-gyp/lib/find-python.js:302:47)
#8 91.79 gyp ERR! stack     at PythonFinder.runChecks (/app/node_modules/node-gyp/lib/find-python.js:136:21)
#8 91.79 gyp ERR! stack     at PythonFinder.<anonymous> (/app/node_modules/node-gyp/lib/find-python.js:179:16)
#8 91.79 gyp ERR! stack     at PythonFinder.execFileCallback (/app/node_modules/node-gyp/lib/find-python.js:266:16)
#8 91.79 gyp ERR! stack     at exithandler (node:child_process:404:5)
#8 91.79 gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:416:5)
#8 91.79 gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
#8 91.79 gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
#8 91.79 gyp ERR! stack     at onErrorNT (node:internal/child_process:477:16)
#8 91.79 gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
#8 91.79 gyp ERR! System Linux 5.10.47-linuxkit
#8 91.79 gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
#8 91.79 gyp ERR! cwd /app/node_modules/node-sass
#8 91.79 gyp ERR! node -v v16.13.0
#8 91.79 gyp ERR! node-gyp -v v7.1.2
#8 91.79 gyp ERR! not ok 
#8 91.79 Build failed with error code: 1

我尝试了错误提示的所有事情 - 设置 python 的路径变量,并尝试将 NPM 的配置设置为 python 的路径但没有运气......

我也尝试将 node 降级到 v16,因为 node-sass 6.0.1 与 node v17 according to the docs 不兼容

我通过自制软件安装了 python 3.9.8,运行 which python3 输出 /opt/homebrew/bin/python3

node -vv16.13.0

npm -vv8.1.0

echo $PATH:/Library/Frameworks/Python.framework/Versions/3.10/bin /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

npm config list:


prefix = "/opt/homebrew" 

; "global" config from /opt/homebrew/etc/npmrc

; python = "/opt/homebrew/opt/python@3.9/libexec/bin" ; overridden by user

; "user" config from /Users/**username**/.npmrc

python = "/opt/homebrew/opt/python@3.9/libexec/bin" 

【问题讨论】:

  • 您已经在您的计算机中安装了 python,但它不存在于运行 npm 的 docker 容器中……您需要在同一容器内的 npm 安装过程之前将 python 安装在容器中。 ..
  • 或者我不确定是否有任何方法可以使用 python 的容器并将其在 docker compose 中链接到另一个容器.. 但总的来说.. python 安装在你的 Mac 上但没有在 docker 环境中,所以你需要解决这个问题。

标签: python node.js npm docker-compose node-sass


【解决方案1】:

问题:

python 本地安装在你的 macbook 上,但不在 docker 容器内(这是一个独立的虚拟操作系统)。

解决方案

您需要在运行 nodejs 脚本或 npm 命令的同一容器中安装 python。

在 dockerFile 中拉取你的 nodejs 图像后尝试添加这个

RUN apt-get update || : && apt-get install python -y

这适用于基于 debian 或基于 debian 的 linux 发行版 ...(如果您有基于 debian 的官方 Node 映像,那么在拉取映像运行后上一行应该可以正常工作,您需要在同一个容器中安装 python )。

【讨论】:

  • 就我而言,在 dockerFile 中的 yarn 之前添加 RUN apk add --update python3 make g++\ &amp;&amp; rm -rf /var/cache/apk/* 解决了这个问题。仍然好奇相同的容器如何在没有这个的其他机器上正常工作......
  • @Stepan 您使用的是 alphine 版本......如果没有安装 python,任何地方都会出现同样的问题......因为 node-sass 需要 node-gyp 进行本机编译和 node-gyp需要 python 来解释.. :) toturial 的人可能错过了它,或者他可能没有使用 node-sass 或任何本机模块。
猜你喜欢
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2023-03-09
  • 2020-09-11
相关资源
最近更新 更多