Bcrypt:使用Docker和Sails.JS无效的ELF头

时间:2015-08-19 20:38:53

标签: node.js docker sails.js bcrypt

我的节点Dockfile

# Set the base image to ubuntu
FROM ubuntu

# Define working directory
ADD . /src
WORKDIR /src

# Install Node.js & other dependencies
RUN apt-get update && \
        apt-get -y install curl && \
        apt-get -y install sudo && \
        curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash - && \
        apt-get -y install python build-essential nodejs

RUN npm install -g node-gyp && \
        node-gyp clean && \
        npm cache clean

RUN node -v

# Install nodemon
RUN npm install -g nodemon


ADD package.json /src/package.json
RUN cd /src && npm install


# Expose port
EXPOSE  8080

# Run app using nodemon
CMD npm install; nodemon /src/app.js

这是我的docker-compose.yml

nginx:
    build: ./nginx
    links:
        - node1:node1
        - node2:node2
        - node3:node3
    ports:
        - "80:80"
redis:
    image: redis
    ports:
        - "6379"
node1:
    build: ./node
    links:
        - redis
    ports:
        - "8080"
node2:
    build: ./node
    links:
        - redis
    ports:
        - "8080"
node3:
    build: ./node
    links:
        - redis
    ports:
        - "8080"

docker-compose build成功运行,Redis / Nginx在docker-compose up上正确设置,但节点实例会抛出此错误:

node2_1 | /src/node_modules/bcrypt/node_modules/bindings/bindings.js:83
node2_1 |         throw e
node2_1 |               ^
node2_1 | Error: /src/node_modules/bcrypt/build/Release/bcrypt_lib.node: invalid ELF header
node2_1 |     at Error (native)
node2_1 |     at Module.load (module.js:355:32)
node2_1 |     at Function.Module._load (module.js:310:12)
node2_1 |     at Module.require (module.js:365:17)
node2_1 |     at require (module.js:384:17)
node2_1 |     at bindings (/src/node_modules/bcrypt/node_modules/bindings/bindings.js:76:44)
node2_1 |     at Object.<anonymous> (/src/node_modules/bcrypt/bcrypt.js:3:35)
node2_1 |     at Module._compile (module.js:460:26)
node2_1 |     at Object.Module._extensions..js (module.js:478:10)
node2_1 |     at Module.load (module.js:355:32)
node2_1 |     at Function.Module._load (module.js:310:12)
node2_1 |     at Module.require (module.js:365:17)
node2_1 |     at require (module.js:384:17)
node2_1 |     at Object.<anonymous> (/src/api/models/User.js:10:14)
node2_1 |     at Module._compile (module.js:460:26)
node2_1 |     at Object.Module._extensions..js (module.js:478:10)
node2_1 | 19 Aug 20:21:11 - [nodemon] app crashed - waiting for file changes before starting...

我已经搜遍了所有人,似乎无法找到有效的解决方案。我尝试使用常规设置网址:https://deb.nodesource.com/setup,专门安装python2.7,并使用ubuntu:15.04图片。

我已经读过,当人们在OS X(我的操作系统)上构建linux env后尝试使用bcrypt时会发生这种情况,但我不认为这是问题,因为我和#39;使用docker和boot2docker建立一切。

7 个答案:

答案 0 :(得分:17)

确保您没有复制node_modules文件夹。我在使用官方nodejs“onbuild”图像时会出现此错误,该图像会复制所有内容......

现在我用:

.dockerignore

node_modules

dockerfile

FROM node:6.4.0

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app

CMD [ "npm", "start" ]

EXPOSE 6969

编辑:Github上的官方NodeJS Docker初学者图像项目已接受我的READ READ请求,该请求指示显式忽略node_modules。

答案 1 :(得分:6)

即使使用Express而不是Sails,我也遇到了同样的事情。我在这里尝试了所有的建议都没有成功。诀窍是通过bcrypt-nodejs更改npm模块bcrypt:

npm uninstal bcrypt
npm install bcrypt-nodejs --save

然后将您的需求更改为

var bcrypt   = require('bcrypt-nodejs');

现在它完美无缺。

答案 2 :(得分:3)

在我的软件包配置中,我有"bcrypt":"^0.8.0",当我拿出^并将其更改为"bcrypt":"0.8.0"时,我能够将所有内容都运行。

问题是它试图运行bcrypt 0.8.5并且由于某种原因导致了问题。

答案 3 :(得分:3)

只是添加一个新的可能原因。我尝试为nodejs应用程序构建我的docker镜像,但我发现了错误invalid ELF header。就我而言 我已经通过添加.dockerignore文件中的node_modules/*解决了这个问题。

答案 4 :(得分:1)

根本原因是=> Docker上下文。 Docker上下文将所有内容从您的根文件夹复制到目标容器工作目录。

因此,您必须添加.dockerignore文件并添加这些条目(以及您要忽略的任何其他文件)

  • node_modules
  • npm-debug.log

现在,当您构建容器时,它将根据容器的操作系统构建所有内容。

答案 5 :(得分:0)

我发现,排除整个本地<head> <title>Waste manager</title> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/css/ol.css" type="text/css"> <style> .map { height: 400px; width: 100%; } </style> <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/build/ol.js"></script> </head> <body> <div class="cyan"> </div> </body> <template name='ApplicationLayout'> {{> yield "navbar"}} {{> yield "main"}} {{> yield "addForm"}} {{> yield "footer"}} </template> <!-- <template name="note"> <li class="collection-item"> {{text}} <a href="#" class="delete-note secondary-content"><button class="brand-logo">delete</button></a> </li> </template>--> <template name="navbar"> <div class="container"> <nav class="cyan"> <div class="container"> <div class="nav-wrapper"> <a href="/" class="brand-logo hide-on-med-and-down">Waste Manager</a> <ul id="nav-mobile" class="right"> <li class="nav-item"> <a href="/maps" class="btn-floating btn-large waves-effect waves-light green"><i class="material-icons left">map</i></a> </li> <li class="nav-item"> <a href="/cards" class="btn-floating btn-large waves-effect waves-light green"><i class="material-icons left">info</i></a> </li> <li class="nav-item"> {{> loginButtons}} </li> </ul> </div> </div> </nav> </div> </template> <template name="addForm"> <div class="container"> <form class="add-form"> <input type="text" name="text" placeholder="Input text"> </form> </div> </template> <template name="maps"> <div class="container"> <h2>My Map</h2> <div id="map" class="map"></div> <script type="text/javascript"> var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([18.41, 43.85]), zoom: 12 }) }); var vectorSource = new ol.source.Vector; var marker = new ol.Feature({ geometry: new ol.geom.Point( ol.proj.fromLonLat([18.4165, 43.8594]) ), }); vectorSource.addFeature (marker); var marker2 = new ol.Feature({ geometry: new ol.geom.Point( ol.proj.fromLonLat([18.4135, 43.8558]) ), }); vectorSource.addFeature (marker2); var markerVectorLayer = new ol.layer.Vector({ source: vectorSource, }); map.addLayer(markerVectorLayer); </script> </div> </template> <template name="cards"> <div class="container"> <p>cards work!</p> <div class="container"> <div class="col s12 m7"> <div class="card small"> <div class="card-image"> <img src="images/sample-1.jpg"> <span class="card-title">Card Title</span> </div> <div class="card-content"> <p>I am a very simple card. I am good at containing small bits of information. I am convenient because I require little markup to use effectively.</p> </div> <div class="card-action"> <a href="#">This is a link</a> </div> </div> </div> <div class="col s12 m7"> <div class="card small"> <div class="card-image"> <img src="images/sample-1.jpg"> <span class="card-title">Card Title</span> </div> <div class="card-content"> <p>I am a very simple card. I am good at containing small bits of information. I am convenient because I require little markup to use effectively.</p> </div> <div class="card-action"> <a href="#">This is a link</a> </div> </div> </div> <div class="col s12 m7"> <div class="card small"> <div class="card-image"> <img src="images/sample-1.jpg"> <span class="card-title">Card Title</span> </div> <div class="card-content"> <p>I am a very simple card. I am good at containing small bits of information. I am convenient because I require little markup to use effectively.</p> </div> <div class="card-action"> <a href="#">This is a link</a> </div> </div> </div> </div> </div> </template> <template name="display"> <div class="container"> {{#if currentUser}} <h1>Smart Bins</h1> <a href="/addForm" class="btn-floating btn-large waves-effect waves-light cyan"><i class="material-icons left">add</i></a> <ul class="collection"> {{#each notes}} <li class="collection-item cyan"> {{text}} <a href="#" class="delete-note secondary-content btn-floating btn-small red"><i class="material-icons left">delete</i></a> </li> {{/each}} </ul> {{else}} <p>Please log in</p> <!-- kartice iz cards --> {{> cards}} {{/if}} </div> </template> <template name="displayWithoutAdd"> <div class="container"> <h1>Smart Bins</h1> {{#if currentUser}} <!-- <a href="/addForm" class="btn-floating btn-large waves-effect waves-light red"><i class="material-icons left">add</i></a> --> <ul class="collection"> {{#each notes}} <li class="collection-item cyan"> {{text}} <a href="#" class="delete-note secondary-content btn-floating btn-small red"><i class="material-icons left">delete</i></a> </li> {{/each}} </ul> {{else}} <p>Please log in</p> {{/if}} </div> </template> <template name="testnavbar"> <div class="container"> <div class="topnav"> <a href="/" class="brand-logo">Waste Manager</a> <a href="/maps" class="btn-floating btn-large waves-effect waves-light green"><i class="material-icons left">map</i></a> {{> loginButtons}} </div> </div> </template> <template name="footer"> <div class="container"> <div class="footerCustom"> <p>Zijah Mahmutbegovic, SSST 2020</p> </div> </div> </template> 目录不允许您安装npm软件包,并使您的docker容器动态跟踪容器中的这些更改。每次我都必须重建容器。为了避免这种情况,请仅在容器卷中指定bcrypt目录,并允许docker在安装或删除其他软件包时跟踪更改:

node_modules

答案 6 :(得分:0)

有一种简单的方法可以让我解决此问题,我认为这也可以在Docker中为您提供帮助,只需将Docker说明中的运行说明添加到此说明中

1。卸载bcrypt

npm uninstall bcrypt

2.-重新安装bcrypt

npm i bcrypt

编辑docker文件的这一部分,添加以下行

ADD package.json /src/package.json
RUN cd /src && npm install


#Solve the problem reinstaling bcrypt
RUN npm uninstall bcrypt
RUN npm i bcrypt


# Expose port
EXPOSE  8080

发生此错误的原因是,当您安装bcypt时,npm会为您的计算机和操作系统安装推荐的版本,但是当您在另一台计算机上时,则不起作用。