19

My Node 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

Here is my 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 runs successfully and Redis/Nginx setup correcly on docker-compose up but the node instances throw this error:

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...

I've searched all over and can't seem to find a solution that works. I've tried using the regular setup url: https://deb.nodesource.com/setup, specifically installing python2.7, and using the ubuntu:15.04 image.

I've read that this happens when people try to use bcrypt on a linux env after it was built on a OS X (my OS), but I don't think that would be the issue since I'm building everything using docker and boot2docker.

gblock
  • 790
  • 4
  • 11
  • 25
  • Can you show a complete reproducer? – user2915097 Aug 19 '15 at 21:48
  • What do you mean by 'reproducer'? – gblock Aug 19 '15 at 22:46
  • The shortest code that allows anybody to do exacty the same as you – user2915097 Aug 20 '15 at 04:47
  • 1
    If the previous responses haven't worked, switching from [bcrypt](https://www.npmjs.com/package/bcrypt) to [bcryptjs](https://www.npmjs.com/package/bcryptjs) seems to work fine for me. – ppak10 Dec 03 '18 at 03:15
  • I was using a Linux Docker image on Windows. Running `npm install` on Windows synced the files to the Docker image. Deleting `node_modules` and running `npm install` inside the container fixed the issue. – totymedli Jun 28 '21 at 05:33

10 Answers10

40

Make sure that you are not copying the node_modules folder. I got this error when using the official nodejs "onbuild" image which would copy everything...

Now I use:

.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

Edit: The official NodeJS Docker starter image project on Github has accepted my pull request for ther README which instructs to explicitly ignore the node_modules.

Joan-Diego Rodriguez
  • 2,439
  • 1
  • 27
  • 29
  • Doesn't this mean that I have to rebuild the docker image each time I change something in my app's code? And if so it can take a while to see the changes live if I have a lot of dependencies in my `package.json` and `npm install` takes a lot of time. It isn't suited for development purposes in that case. – through.a.haze Sep 02 '19 at 11:11
27

I was experiencing same thing, even though using Express, not Sails. I tried every suggestion here with no success. What made the trick was change the npm module bcrypt by bcryptjs:

npm uninstall bcrypt
npm install bcryptjs --save

Then change your require to something like

var bcrypt   = require('bcryptjs');

It is working flawlessly now.

rodurico
  • 751
  • 7
  • 17
5

In my package config I had "bcrypt":"^0.8.0" and when I took out the ^ and changed it to "bcrypt":"0.8.0" I was able to get everything running.

The issue was that it was trying to run bcrypt 0.8.5 and that was causing issues for some reason.

gblock
  • 790
  • 4
  • 11
  • 25
  • It would be great to know what is causing the issue. I checked the [Changelog](https://github.com/ncb000gt/node.bcrypt.js/blob/v0.8.7/CHANGELOG.md) but nothing stands out to me. – Lucas Aug 01 '16 at 10:38
  • @Lucas If this is still happening to you, try to delete the node_modules folder and do a fresh `npm install` in the docker container. Bcrypt needs to be built on the architecture it's running on, so if you are copying it from OS X to linux then it needs to get re-built. Though I don't remember if this was also part of the problem for this issue. – gblock Aug 17 '16 at 00:53
  • The developers decided to introduce breaking changes, seen in Issue #150 https://github.com/barrysteyn/node-scrypt/issues/150 "okay, I have no problem with throwing an error." – Sandwich Nov 02 '17 at 11:00
3

just to add a new possible cause. I tried to build my docker image for a nodejs app but i've gotthe error invalid ELF header. In my case i ve resolved the issue by adding the node_modules/* from the .dockerignore file.

Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
1

I have found that excluding the entire local node_modules directory does not allow you to install npm packages and have your docker container track those changes in the container on the fly. I will have to rebuild my container each time. To avoid this specify only the bcrypt directory in the container volume and allow docker to track changes when other packages are installed or removed:

volumes: 
  - .:/app
  - /app/node_modules/bcrypt/
Andrew Quartey
  • 101
  • 2
  • 7
1

There is a simple way that allowed me to solve this problem, I think this can helps you also in Docker, just add to the run instructions on you docker file this instructions

1. Uninstall bcrypt

npm uninstall bcrypt

2.- Install bcrypt again

npm i bcrypt

Edit this part of your docker file adding the lines

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

The error occurs because when you install bcypt, npm installs the recommended version for your machine and operating system, but when you are on another machine, this doesn't work.

-------- UPDATE ----------------------------------------

It also seems to me that another solution which is to grant root permissions to bcrypt installation, it happens because bcryp uses its own user but it has no permissions, so:

1. You must grant root permission to your project folder. go outside of your project folder and then

sudo su

Then enter your root password to get root user permissions

2. Grant permission to your project folder

chmod -R 777 <project_folder>

3. Go to your project folder and install bcrypt

cd <project_folder>

AND

npm i bcrypt --unsafe-perm=true --allow-root --save

Ready, if everything was OK, your bcrypt module will install without problems.

crazyProgrammer
  • 356
  • 2
  • 11
1

The root cause is => Docker Context. Docker context is copying everything from your root folder to destination container working directory.

So you have to add a .dockerignore file and add these entries (with any other files that you want to ignore)

  • node_modules
  • npm-debug.log

And now when you will build your container, it will build everything as per container's OS.

0

I spent a lot of time trying to run bcrypt in a docker container using docker-copmpose. Container worked under Linux, but the development was carried out in Windows, and it was necessary to mount the volume with the code, but bcrypt created conflicting files. So I leave this review in the future. bcrypt must only be installed inside the container, and copy to isolate. For example to block directory, or send to dev/null

    volumes:
      - ${WORKER_DIR}/api:${HOME}/api
      - /dev/null/:/api/node_modules/bcrypt/
0

In my case I fixed it by adding these lines to Dockerfile

CMD [ "npm", "uninstall", "bcrypt" ]
CMD [ "npm", "install", "bcrypt" ]
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '21 at 17:57
0

Including a .dockerignore file fixed this error for me

The basic .dockerignore looks like this

# See https://help.github.com/articles/ignoring-files/ for more about 

ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Uche Azinge
  • 682
  • 1
  • 8
  • 14