我在Node.js上看到了几个关于macOS,Docker和bcrypt
的问题。 This似乎是我的问题最接近的答案,但它没有奏效。
我的Dockerfile
:
FROM node:6.4.0
COPY . /app
RUN ls -la /app
WORKDIR /app
RUN cd /app; npm install
CMD ["node", "index.js"]
我的.dockerignore
文件:
node_modules
我的package.json
依赖项:
"dependencies": {
"bcrypt": "1.0.0",
}
启动容器时,我收到此错误:
/app/node_modules/bcrypt/build/Release/bcrypt_lib.node: invalid ELF header
从ls -la /app
中的Dockerfile
命令,我可以看到node_modules
文件夹绝对不是从主机(macOS)复制的:
drwxr-xr-x 6 root root 4096 Dec 7 21:29 .
drwxr-xr-x 47 root root 4096 Dec 7 21:29 ..
-rw-r--r-- 1 root root 763 Dec 7 20:55 .dockerignore
-rw-r--r-- 1 root root 122 Dec 7 21:18 Dockerfile
rw-r--r-- 1 root root 13525 Dec 7 19:43 index.js
-rw-r--r-- 1 root root 947 Dec 7 21:28 package.json
bcrypt
npm install
日志:
> [email protected] install /app/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using [email protected]
node-pre-gyp info using [email protected] | linux | x64
node-pre-gyp info check checked for "/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node" (not found)
node-pre-gyp http GET https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.0/bcrypt_lib-v1.0.0-node-v48-linux-x64.tar.gz
node-pre-gyp http 404 https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.0/bcrypt_lib-v1.0.0-node-v48-linux-x64.tar.gz
node-pre-gyp ERR! Tried to download: https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.0/bcrypt_lib-v1.0.0-node-v48-linux-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for [email protected] and [email protected] (node-v48 ABI) (falling back to source compile with node-gyp)
node-pre-gyp http Pre-built binary not available for your system, looked for https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.0/bcrypt_lib-v1.0.0-node-v48-linux-x64.tar.gz
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info spawn /usr/bin/python2
gyp info spawn args [ '/usr/local/lib/node_modules/npm/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 '/app/node_modules/bcrypt/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/root/.node-gyp/6.4.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/.node-gyp/6.4.0',
gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=node.lib',
gyp info spawn args '-Dmodule_root_dir=/app/node_modules/bcrypt',
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 ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/app/node_modules/bcrypt/build'
CXX(target) Release/obj.target/bcrypt_lib/src/blowfish.o
CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt.o
CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt_node.o
SOLINK_MODULE(target) Release/obj.target/bcrypt_lib.node
COPY Release/bcrypt_lib.node
COPY /app/node_modules/bcrypt/lib/binding/bcrypt_lib.node
TOUCH Release/obj.target/action_after_build.stamp
make: Leaving directory '/app/node_modules/bcrypt/build'
gyp info ok
node-pre-gyp info ok
更新:我试图在容器内运行bcrypt
似乎工作正常:
docker run identity file /app/node_modules/bcrypt/build/Release/bcrypt_lib.node
/app/node_modules/bcrypt/build/Release/bcrypt_lib.node: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=2a9d4a04f6f9c548f8035b9b737c1efac5d774d2, not stripped
comment from Stefan Scherer表明我的问题中的代码是有效的,应该可以工作并让我搜索问题的其他原因。
最后我发现它是由docker-compose.yml
引起的,它启动了容器作为多种服务的一部分。
问题是由容器的volume
条目引起的:
volumes :
- ../identity/:/app
因此,来自主机的node_modules
文件夹再次被映射到容器中,并且bcrypt ELF头部确实再次无效。
解决方案是从映射中排除node_modules
文件夹(如here所述):
volumes :
/app/node_modules/