使用 M1 在 dockerized linux 上安装节点 canvas

Install node canvas on dockerized linux with M1

我在我的 MacBook Air M1 上 Dockerfile 运行(所以在 docker 我有 linux 和 M1)

FROM node:16.7.0
WORKDIR /work
CMD while true; do sleep 10000; done
EXPOSE 3000

我构建,运行并通过

登录到容器
docker build --no-cache -t node-cmd-custom .
docker run -d --name node-cmd-custom -p 3000:3000 -v /work/code/metaplex:/work node-cmd-custom
docker exec -it node-cmd-custom bash -c "cd /work/code/metaplex; bash"

在 docker 容器内我安装 Metaplex(使用官方 doc)如下

git clone --branch v1.0.0 https://github.com/metaplex-foundation/metaplex.git
cd metaplex/js && yarn install && yarn bootstrap

但是我得到以下错误

[5/5] Building fresh packages...
[6/21] ⠁ canvas
[10/21] ⠁ keccak
[11/21] ⠁ core-js-pure
[9/21] ⠁ secp256k1
error /work/code/metaplex/metaplex/js/node_modules/canvas: Command failed.
Exit code: 1
Command: node-pre-gyp install --fallback-to-build
Arguments: 
Directory: /work/code/metaplex/metaplex/js/node_modules/canvas
Output:
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using node-pre-gyp@1.0.5
node-pre-gyp info using node@16.7.0 | linux | arm64
node-pre-gyp info check checked for "/work/code/metaplex/metaplex/js/node_modules/canvas/build/Release/canvas.node" (not found)
node-pre-gyp http GET https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz
node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz 
node-pre-gyp WARN Pre-built binaries not installable for canvas@2.8.0 and node@16.7.0 (node-v93 ABI, glibc) (falling back to source compile with node-gyp) 
node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz 
gyp info it worked if it ends with ok
gyp info using node-gyp@5.1.1
gyp info using node@16.7.0 | linux | arm64
gyp info ok 
gyp info it worked if it ends with ok
gyp info using node-gyp@5.1.1
gyp info using node@16.7.0 | linux | arm64
gyp info find Python using Python version 2.7.16 found at "/usr/bin/python"
gyp http GET https://nodejs.org/download/release/v16.7.0/node-v16.7.0-headers.tar.gz
gyp http 200 https://nodejs.org/download/release/v16.7.0/node-v16.7.0-headers.tar.gz
gyp http GET https://nodejs.org/download/release/v16.7.0/SHASUMS256.txt
gyp http 200 https://nodejs.org/download/release/v16.7.0/SHASUMS256.txt
(node:238) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only.
(Use `node --trace-deprecation ...` to show where the warning was created)
gyp info spawn /usr/bin/python
gyp info spawn args [
gyp info spawn args   '/work/code/metaplex/metaplex/js/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/work/code/metaplex/metaplex/js/node_modules/canvas/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/work/code/metaplex/metaplex/js/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/root/.cache/node-gyp/16.7.0/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/root/.cache/node-gyp/16.7.0',
gyp info spawn args   '-Dnode_gyp_dir=/work/code/metaplex/metaplex/js/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/root/.cache/node-gyp/16.7.0/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/work/code/metaplex/metaplex/js/node_modules/canvas',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
Package pangocairo was not found in the pkg-config search path.
Perhaps you should add the directory containing `pangocairo.pc'
to the PKG_CONFIG_PATH environment variable
No package 'pangocairo' found
gyp: Call to 'pkg-config pangocairo --libs' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/work/code/metaplex/metaplex/js/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (node:events:394:28)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
gyp ERR! System Linux 5.10.47-linuxkit
gyp ERR! command "/usr/local/bin/node" "/work/code/metaplex/metaplex/js/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/work/code/metaplex/metaplex/js/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/work/code/metaplex/metaplex/js/node_modules/canvas/build/Release" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v93"
gyp ERR! cwd /work/code/metaplex/metaplex/js/node_modules/canvas
gyp ERR! node -v v16.7.0
gyp ERR! node-gyp -v v5.1.1
gyp ERR! not ok 
node-pre-gyp ERR! build error 
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /work/code/metaplex/metaplex/js/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/work/code/metaplex/metaplex/js/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/work/code/metaplex/metaplex/js/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/work/code/metaplex/metaplex/js/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:394:28)
node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1064:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
node-pre-gyp ERR! System Linux 5.10.47-linuxkit
node-pre-gyp ERR! command "/usr/local/bin/node" "/work/code/metaplex/metaplex/js/node_modules/canvas/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /work/code/metaplex/metaplex/js/node_modules/canvas
node-pre-gyp ERR! node -v v16.7.0
node-pre-gyp ERR! node-pre-gyp -v v1.0.5

当我尝试只安装时 canvas npm install canvas 我有类似的错误

npm ERR! code 1
npm ERR! path /work/code/metaplex/metaplex/node_modules/canvas
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
npm ERR! 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=/work/code/metaplex/metaplex/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/work/code/metaplex/metaplex/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1)
npm ERR! node-pre-gyp info it worked if it ends with ok
npm ERR! node-pre-gyp info using node-pre-gyp@1.0.8
npm ERR! node-pre-gyp info using node@16.7.0 | linux | arm64
npm ERR! node-pre-gyp info check checked for "/work/code/metaplex/metaplex/node_modules/canvas/build/Release/canvas.node" (not found)
npm ERR! node-pre-gyp http GET https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz
npm ERR! node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz 
npm ERR! node-pre-gyp WARN Pre-built binaries not installable for canvas@2.8.0 and node@16.7.0 (node-v93 ABI, glibc) (falling back to source compile with node-gyp) 
npm ERR! node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.8.0/canvas-v2.8.0-node-v93-linux-glibc-arm64.tar.gz 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@16.7.0 | linux | arm64
npm ERR! gyp info ok 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@16.7.0 | linux | arm64
npm ERR! gyp info find Python using Python version 3.7.3 found at "/usr/bin/python3"
npm ERR! (node:1436) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only.
npm ERR! (Use `node --trace-deprecation ...` to show where the warning was created)
npm ERR! gyp info spawn /usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/work/code/metaplex/metaplex/node_modules/canvas/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/root/.cache/node-gyp/16.7.0/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/root/.cache/node-gyp/16.7.0',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/root/.cache/node-gyp/16.7.0/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/work/code/metaplex/metaplex/node_modules/canvas',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! Package pangocairo was not found in the pkg-config search path.
npm ERR! Perhaps you should add the directory containing `pangocairo.pc'
npm ERR! to the PKG_CONFIG_PATH environment variable
npm ERR! No package 'pangocairo' found
npm ERR! gyp: Call to 'pkg-config pangocairo --libs' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
npm ERR! gyp ERR! configure error 
npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1
npm ERR! gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:394:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
npm ERR! gyp ERR! System Linux 5.10.47-linuxkit
npm ERR! 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=/work/code/metaplex/metaplex/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/work/code/metaplex/metaplex/node_modules/canvas/build/Release" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v93"
npm ERR! gyp ERR! cwd /work/code/metaplex/metaplex/node_modules/canvas
npm ERR! gyp ERR! node -v v16.7.0
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok 
npm ERR! node-pre-gyp ERR! build error 
npm ERR! 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=/work/code/metaplex/metaplex/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/work/code/metaplex/metaplex/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/work/code/metaplex/metaplex/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:394:28)
npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1064:16)
npm ERR! node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
npm ERR! node-pre-gyp ERR! System Linux 5.10.47-linuxkit
npm ERR! node-pre-gyp ERR! command "/usr/local/bin/node" "/work/code/metaplex/metaplex/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd /work/code/metaplex/metaplex/node_modules/canvas
npm ERR! node-pre-gyp ERR! node -v v16.7.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.8
npm ERR! node-pre-gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-12-28T11_59_12_514Z-debug.log

在文档中有 OSx 和 M1 处理器的解决方案(使用 brew - 但实际上没有信息如何在 Linux 和 M1 中解决这个问题(在里面docker - 而我们没有的地方 brew)。如何修复它?

将您的 dockerfile 更改为(它基于此 info)并再次 install/run metaplex/canvas

FROM node:16.7.0
WORKDIR /work

RUN apt-get update
RUN apt-get install -y build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev

CMD while true; do sleep 10000; done
EXPOSE 3000

(现在您只能使用 npm install canvas --build-from-source 构建 canvas)

如果你想在 node:16.7.0-alpine(高山 linux)上做同样的事情,请看