Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while building a docker image #824

Closed
yacineBR opened this issue Aug 19, 2020 · 27 comments
Closed

Error while building a docker image #824

yacineBR opened this issue Aug 19, 2020 · 27 comments
Labels

Comments

@yacineBR
Copy link

yacineBR commented Aug 19, 2020

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" ]

@MnifR
Copy link

MnifR commented Aug 19, 2020

+1

@asmafakhfakh
Copy link

I have the same problem

@aymenamaraa
Copy link

+1

@recrsn
Copy link
Collaborator

recrsn commented Aug 20, 2020

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

@tobslob
Copy link

tobslob commented Feb 22, 2021

Did anyone solve this problem?

@tobslob
Copy link

tobslob commented Feb 23, 2021

Adding node_modules to .dockerignore solved this.

@recrsn recrsn closed this as completed Mar 8, 2021
@recrsn recrsn added the question label Mar 8, 2021
@LeoCelador
Copy link

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

@hazartilirot
Copy link

hazartilirot commented Nov 24, 2021

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'

@tufac2
Copy link

tufac2 commented Jan 26, 2022

Create a .dockerignore file and add /node_modules

@rbourgeat
Copy link

I have the same problem ☹️

@acnicolasdc
Copy link

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.

@Marcosaurios
Copy link

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.

@gardelin
Copy link

gardelin commented Dec 19, 2022

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 

@rahuls360
Copy link

Switching to bcryptjs worked for me

@gkqkehs7
Copy link

make .dockerignore and add /node_modules works for me

@timheerwagen
Copy link

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.

@macrochel
Copy link

Switching to bcryptjs worked for me

+1

@timheerwagen
Copy link

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.

@thesonpb
Copy link

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

@jazzybruno
Copy link

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

@shide1989
Copy link

shide1989 commented Nov 16, 2023

this is yet another solution that worked for me when using a docker-compose file:
Simply specify that your container /app/node_modules should be anonymous :

volumes:
      - /app/node_modules

@ahmnadim
Copy link

ahmnadim commented Dec 4, 2023

Adding node_modules to .dockerignore works for me also.

@cauta
Copy link

cauta commented Dec 22, 2023

Adding node_modules to .dockerignore works for me also.

me too, adding docker ignore and volume to docker compose

@kazim-asif
Copy link

Adding node_modules to .dockerignore solved this.

@castilloedwin
Copy link

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'

It works for me!

@HARISHKUMAR023
Copy link

adding node_modules/ .dockerignore file is worked for me , Happy coding !!

@MarthL
Copy link

MarthL commented Apr 28, 2024

adding node_modules/ .dockerignore file and remove node_modules volume worked for me ! thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests