5

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

Scott
  • 1,322
  • 4
  • 23
  • 37
  • it might sound silly, but check if `bcrypt` exists in you `package.json`. Also add the following to you `.dockerignore` file; `node_modules` and `node_modules/*` – Evan P Mar 15 '17 at 22:58
  • Yeah, it's in there @EvanP. I had `node_modules` id .dockerignore but not `node_modules/*`. I need both? – Scott Mar 16 '17 at 00:37
  • You shouldn't need to. Also can you check please whether your `volume` that you are creating, if it copies the `node_modules` of your host machine? Mind; the `volume` doesn't respect the the `.dockerignore` neither the `.gitignore`. And is generated during the `docker run` – Evan P Mar 20 '17 at 00:40
  • 1
    It seems there is some confusion over whether .dockerignore is taken into account when running docker-compose up --build - From the docs, it seems that .dockerignore is indeed taken into account: https://docs.docker.com/engine/reference/builder/#dockerignore-file – schalkneethling Dec 14 '18 at 19:35

3 Answers3

5

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"]
Intellidroid
  • 1,053
  • 1
  • 9
  • 15
0

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

roy
  • 585
  • 5
  • 14
0

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.

Codehouze
  • 5
  • 5