Scott
Scott

Reputation: 1342

Docker with node bcrypt — invalid ELF header

I've tried every solution from this post and this post

I'm not finding a solution to get rid of the following error when running docker-compose up:

module.js:598
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: /code/node_modules/bcrypt/lib/binding/bcrypt_lib.node: invalid ELF header

Here's my latest attempt docker-compose.yml

version: "2"

services:
  app:
    build: ./client
    ports:
      - "3000:3000"
    links:
      - auth
    volumes:
      - ./client:/code
  auth:
    build: ./auth-service
    ports:
      - "3002:3002"
    links:
      - db
    volumes:
      - ./auth-service:/code
  db:
    ...

And my auth service Dockerfile:

FROM node:7.7.1

EXPOSE 3002

WORKDIR /code

COPY package.json /code

RUN npm install

COPY . /code

CMD npm start

After trying each of the solution from the above two links, I rebuild the containers and it always results in the same error.

Also worth noting, the service runs fine locally, when I don't use docker.

How do I get docker to work with bcrypt?

Update

I was able to get it working by doing the following:

  1. finding the id of the container: docker ps
  2. accessing the container: docker exec -t -i containerId /bin/bash
  3. installing bcrypt: npm install bcrypt

This isn't ideal for portability

Upvotes: 5

Views: 7309

Answers (3)

Codehouze
Codehouze

Reputation: 15

The reason this error occurs is that the node module bcrypt is first compiled on your original machine (specific for your OS) and when an image is built on docker it cannot run since the OS is no longer the same. solution create a .dockerignore file in your root folder and add node_modules to this file.

Upvotes: 1

Intellidroid
Intellidroid

Reputation: 1055

I spent a few hours trying to solve this and in the end I came up with the following solution. My compose file looks like this.....

version: "3"

services:
  server:
    build:
      context: ./server
    volumes:
     - ./server:/usr/src/app
     - /usr/src/app/node_modules/
    ports:
      - 3050:3050
    depends_on:
  - db
command: ["nodemon", "./bin/www"]

The second volume mount there is the important one as this gets around the local node_module issue.

Just for reference my dockerfile is like this:

FROM node
RUN npm install -g nodemon
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
EXPOSE 3050
CMD ["nodemon", "./bin/www"]

Upvotes: 6

roy
roy

Reputation: 605

was struggling with this one few times, the ".dockerignore" solution wont work if you use volumes sadly, since its only related to the "Copy" command and only when you build the container.

the only solution i found which i think makes the most sense, is to volume only what you need, what does it mean - divide your source code and the "configurations" files (such as package.json):

- src
-- morecode
-- morecode2
-- server.js
- index.js
- package.json
- jslint.json
- tsconfig.json
- .env
- .dockerignore
- ... etc

and put the volume only on the "src" folder, that way your builds will also be much faster plus your node modules will be built and installed on the correct operation system, d'ont forget to add .dockerignore to node_modules to prevent builds form taking unnecessary longer time

do note that doing so will require re-build of the application every time your adding new package, but if you use npm best practice and you divide the npm installation in your docker file to be cached, it will be faster

Upvotes: 0

Related Questions