Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Failed - bcrypt_lib.node: Exec format error

I have tried to create a docker image of my backend API. but getting errors. I have googled about it, and everyone who has the same issue had to add node_module on the .dockerignore file.

I already did it, but, still have the same error.

I am adding my file info here.

Dockerfile

FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json .
#COPY yarn.lock .
RUN apk add --no-cache yarn --repository="http://dl-cdn.alpinelinux.org/alpine/edge/community"
#RUN yarn install --frozen-lockfile
RUN yarn install
RUN yarn
COPY . .
CMD ["yarn", "dev"];

.dockerignore

/node_modules
.env
docker-compose.yml

docker-compose.yml

version: "3.9"

services:
  mongo_db:
    container_name: mongodb_container
    image: mongo:latest
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - mongo_db:/data/db

  #EET service
  eetapi:
    container_name: eetapi_container
    build: .
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    environment:
      SITE_URL: http://localhost
      PORT: 3000
      MONGO_URL: mongodb://mongodb_container:27017/easyetapi
      JWT_SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      SENTRY_DSN: https://[email protected]/xxxxxxx
      MAILGUN_DOMAIN: mg.myeetdomain.tld
      MAILGUN_API_KEY: xxxxxxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxx
      NODE_ENV: production
    depends_on:
      - mongo_db
volumes:
  mongo_db: {}

The Error

Error Screenshot

Please help me out.

Thank You

like image 207
Sarequl Basar Avatar asked Mar 07 '26 10:03

Sarequl Basar


2 Answers

The volumes: block overwrites everything in the image with the current directory on the host. That includes the node_modules tree installed in the Dockerfile. If you have a MacOS or Windows host but a Linux container, replacing the node_modules tree will cause the error you get.

You should delete the volumes: block so that you run the code and library tree that are built into the image.

Since the bind-mount overwrites literally everything the Dockerfile does, it negates any benefit you get from building the Docker image. Effectively you're just running an unmodified node image with bind-mounted host content, and you'll get the same effect with a much simpler setup if you Node on the host without involving Docker. (You could still benefit from running the database in a container.)

like image 50
David Maze Avatar answered Mar 10 '26 10:03

David Maze


I ended up a difference solution , the problem occurred due to node modules created by my machine which is windows and I am creating docker images in alpine. So I use bcryptjs that is platform independent.

bcrypt: It is a native binding to the C++ bcrypt library. It requires compilation and contains bindings to the underlying system's C library. As a result, it might have better performance due to its native implementation. That means if I compiled the library in windows then it will not work in linux. But performance would be better.

bcryptjs: It is a pure JavaScript implementation of the bcrypt algorithm. It doesn't have native bindings and relies entirely on JavaScript. While it might be slower compared to bcrypt, especially in CPU-intensive operations, it's easier to install and use across different systems without requiring native compilation.

So I installed bcryptjs and I got the build success. Again it is just a choice, if you have big complex password hashing in your project then you should got for bcrypt for better performance.

like image 29
Vishnu KR Avatar answered Mar 10 '26 09:03

Vishnu KR