node.bcrypt.js icon indicating copy to clipboard operation
node.bcrypt.js copied to clipboard

Error while building a docker image

Open yacineBR opened this issue 6 years ago • 29 comments

this is the log :

Error: Error loading shared library /home/node/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: Exec format error

at Object.Module._extensions..node (internal/modules/cjs/loader.js:1206:18)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1042:19)

at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:80:39)

at require (internal/modules/cjs/helpers.js:77:18)

at Object. (/home/node/app/node_modules/bcrypt/bcrypt.js:6:16)

at Module._compile (internal/modules/cjs/loader.js:1156:30)

at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)

at Object.require.extensions. [as .js] (/home/node/app/node_modules/babel-register/lib/node.js:152:7)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1042:19)

at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:80:39)

at require (internal/modules/cjs/helpers.js:77:18)

at Object. (/home/node/app/utils/auth.js:2:16)

2020-08-19T12:52:14: PM2 log: App [access-backend:0] exited with code [1] via signal [SIGINT]

2020-08-19T12:52:16: PM2 log: [Watch] Stop watching access-backend

2020-08-19T12:52:16: PM2 log: PM2 successfully stopped

DockerFile

FROM keymetrics/pm2:12-alpine

RUN mkdir -p /app

WORKDIR /app ARG NODE_ENV ENV NODE_ENV $NODE_ENV

COPY package*.json ./

RUN npm cache clean --force && rm -rf node_modules && npm install

EXPOSE 7777 CMD [ "pm2-runtime", "start", "ecosystem.config.js" ]

yacineBR avatar Aug 19 '20 13:08 yacineBR

+1

MnifR avatar Aug 19 '20 15:08 MnifR

I have the same problem

asmafakhfakh avatar Aug 20 '20 08:08 asmafakhfakh

+1

aymenamaraa avatar Aug 20 '20 08:08 aymenamaraa

Please do not copy node_modules inside Docker. It decreases your build time and prevents this kind of errors from happening.

Use a .dockerignore and add node_modules/ to it

recrsn avatar Aug 20 '20 09:08 recrsn

Did anyone solve this problem?

tobslob avatar Feb 22 '21 21:02 tobslob

Adding node_modules to .dockerignore solved this.

tobslob avatar Feb 23 '21 03:02 tobslob

docker is not detecting changes in the layers above "run npm ..." therefore it does not build the complete image. I solved it by exchanging run position with copy

LeoCelador avatar Jul 12 '21 11:07 LeoCelador

I've just had the same issue. node_modules was in the docker ignore list;

the thing is there are two ways to solve the problem, the simplest one is to install bcryptjs instead of bcrypt

As for the solution for bcrypt (in my case)

Here is my **Dockerfile**

FROM node:16.13.0-alpine

WORKDIR /app

COPY ["package.json", "package-lock.json", "./"]

RUN npm install --silent

COPY . .

CMD ["npm", "run", "server"]

and the part of docker-compose.yml Mind WORKDIR, volumes, double check your paths

server:
    container_name: 'server'
    build: '.'
    volumes:
      - '.:/app:rw'
      - '/app/node_modules'
    environment:
      POSTGRES_URI: 'postgresql://postgres:root@db:5432/database'
      REDIS_URI: 'redis://redis:6379'
    links:
      - 'db'
      - 'redis'
    ports:
      - '5000:5000'

So I did a mistake in - '/app/node_modules'

hazartilirot avatar Nov 24 '21 20:11 hazartilirot

Create a .dockerignore file and add /node_modules

tufac2 avatar Jan 26 '22 12:01 tufac2

I have the same problem ☹️

rbourgeat avatar Feb 26 '22 08:02 rbourgeat

Well, actually you should Create a .dockerignore file and add /node_modules and in your docker-compose define an anonymous directory for node modules.

Volumes:

- /home/node/app/node_modules

after several attempts, I have realized that it is not necessary to add a script to remove bcrypt and then install it.

acnicolasdc avatar Jul 27 '22 12:07 acnicolasdc

I found the same issue, and learned in this article by Richard Kotze that this dependency needs to be build in the OS where is running.

So, basically in your dockerfile, after doing npm ci, adding a RUN npm rebuild bcrypt --build-from-source command should make the trick. Also adding an anonymous volume for node_modules will work because volumes are completely managed by Docker (by the OS who is running the image), thus installing the deps and building the bcrypt dependency in the OS/image used by the dockerfile.

Marcosaurios avatar Oct 06 '22 14:10 Marcosaurios

Adding node_modules in •dockerignore or adding anonymous volume didn't solve problem for me.

Only solution that did work is to rebuild bcrypt like this:

RUN npm install
RUN npm rebuild bcrypt 

gardelin avatar Dec 19 '22 09:12 gardelin

Switching to bcryptjs worked for me

rahuls360 avatar Apr 20 '23 17:04 rahuls360

make .dockerignore and add /node_modules works for me

gkqkehs7 avatar Apr 22 '23 11:04 gkqkehs7

My current problem is, if you use Next.js Standalone Output, you need to copy node_modules. Because they are not reinstalled so as not to add 700mb extra to the Docker image.

timheerwagen avatar Jun 26 '23 10:06 timheerwagen

Switching to bcryptjs worked for me

+1

macrochel avatar Jul 15 '23 17:07 macrochel

Switching to bcryptjs worked for me

The package has not been maintained for 6 years, I am unsure if I should choose this over a package that is more up to date.

timheerwagen avatar Jul 15 '23 19:07 timheerwagen

Please do not copy node_modules inside Docker. It decreases your build time and prevents this kind of errors from happening.

Use a .dockerignore and add node_modules/ to it

I worked for me

thesonpb avatar Aug 13 '23 10:08 thesonpb

Hello if you are facing the same error make sure you try to add the docker ignore file and add the node modules together with the npm debug role then try to rerun it will work image

jazzybruno avatar Sep 13 '23 10:09 jazzybruno