【问题标题】:Bcrypt installation fails in DockerDocker 中的 Bcrypt 安装失败
【发布时间】:2017-02-02 19:20:57
【问题描述】:

我已经使用 MongoDB 创建了一个在 Docker 中运行的节点应用程序。在我包含node.bcrypt.js 之前它运行良好。这会导致 Node 在 node-gypbcrypt 崩溃。

该应用在本地和 Heroku 上运行良好。

我尝试安装一些我在网上找到的建议软件包,根据错误消息已知它们是必需的。这就是为什么我添加了一些额外的依赖项,请参阅下面 dockerfile 中的node-gyp-related 行。

现在它已经到了我找不到更多建议的地方,但它仍然不起作用。我觉得它在本地和 Heorku 上都有效,但在 Docker 上无效,这很奇怪,因此我缺少它。

提前致谢。

错误:

> crowdshelf-server@1.0.0 start /server
> node index.js

  COPY Release/bcrypt_lib.node
make: Leaving directory `/server/node_modules/bcrypt/build'
module.js:338
    throw err;
          ^
Error: Cannot find module './lib/topologies/server'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/server/node_modules/mongodb/node_modules/mongodb-core/index.js:3:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)

npm ERR! Linux 3.13.0-58-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "start"
npm ERR! node v0.12.7
npm ERR! npm  v2.11.3
npm ERR! code ELIFECYCLE
npm ERR! crowdshelf-server@1.0.0 start: `node index.js`
npm ERR! Exit status 1

这是在我向 Dockerfile 添加了一些安装之后,请参阅node-gyp 之后的行。 Dockerfile:

# Base Docker-image on Ubuntu
FROM    ubuntu:latest

#install mongodb
#install git, curl, python and mongo
# node-gyp 
RUN apt-get install -y build-essential make automake gcc g++ cpp libkrb5-dev libc6-dev man-db autoconf pkg-config 

# Create the MongoDB data directory
RUN mkdir -p /data/db

# mongodb setup
# Install NodeJS
RUN curl --silent --location https://deb.nodesource.com/setup_0.12 | sudo bash -
RUN apt-get update && apt-get install -y nodejs
RUN npm install -g node-gyp

# Git-clone project
# expose ports
# Install dependencies and start server and database
CMD cd /server && sh start.sh

starth.sh-脚本只是安装依赖项并启动 MongoDB 和服务器。

编辑: repo 告诉我查看node-gyp dependencies,但我觉得上面显示的 Dockerfile 已经涵盖了这一点。

【问题讨论】:

    标签: node.js docker bcrypt


    【解决方案1】:

    它正在崩溃,因为您缺少一些用于 Bcrypt 编译目的的基本工具。每次执行 npm install 时都需要编译 Bcrypt,以便为正在运行的操作系统创建一个版本,因为它是用 C 编写的。

    想要使用原版 Bcrypt 的可以运行以下 docker 命令:

    docker run -t -i --rm -v $(pwd):/app -w /app node:slim sh -c 'apt-get update && apt-get install -y build-essential && apt-get install -y python && npm install'
    

    npm install 之前,我们需要:

    • apt-get update:确保包管理系统知道要找到我们想要的东西。
    • apt-get install -y build-essential:将安装编译 C 代码等所需的所有工具
    • apt-get install -y python:我们添加 python,因为它是必需的,并且它不包含在 build-essential 包中。

    一旦我们有了所有这些东西,我们就可以成功运行npm install :)

    希望这会有所帮助。

    【讨论】:

    • 应该是选择的答案,因此开发人员正在使用经过良好测试且正在积极开发的 bcrypt 包
    • 要向 dockerfile 添加什么?
    • 我不明白这个答案的任何内容以及为什么它被赞成。我们不能只在 DockerFile 中添加一些行以便能够在容器中使用 bcrypt 吗?
    • 仅仅因为您是 Docker 新手并不意味着答案没有意义。它之所以受到支持,是因为它对了解 Docker 的人来说是有意义的。当然,您可以将我在工作示例下的清晰列表中列出的所有命令添加到 Dockerfile 中。
    【解决方案2】:

    我通过简单地更改bcrypt-library 解决了这个问题。 This one 是基于类似的问题创建的,并提供相同的 API。与我的问题中提到的库的唯一区别是:

    bcrypt.hash(password, function(err, hash) {
        if(!err) callback(hash);
    });
    

    虽然在这个答案中链接的那个:

    bcrypt.hash(password, null, null, function(err, hash) { // Addd nulls
        if(!err) callback(hash);
    });
    

    【讨论】:

    • 这个包最近更新了,并且基于这个答案中提供的链接包 - github.com/dcodeIO/bcrypt.js。请记住,与这些选项中的任何一个相比,bcrypt 的更新最为一致,因此如果@davidgatti 的答案对您有用,请在这些包上使用它。
    猜你喜欢
    • 2016-03-03
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多