Error while building a docker image
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.
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.
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.
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" ]
+1
I have the same problem
+1
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
Did anyone solve this problem?
Adding node_modules to .dockerignore solved this.
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
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'
Create a .dockerignore file and add /node_modules
I have the same problem ☹️
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.
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.
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
Switching to bcryptjs worked for me
make .dockerignore and add /node_modules works for me
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.
Switching to
bcryptjsworked for me
+1
Switching to
bcryptjsworked 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.
Please do not copy
node_modulesinside Docker. It decreases your build time and prevents this kind of errors from happening.Use a
.dockerignoreand addnode_modules/to it
I worked for me
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