Docker Failed - bcrypt_lib.node: Exec format error
Asked Answered
P

5

8

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

Pyroelectricity answered 7/7, 2022 at 1:49 Comment(0)
S
9

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.)

Sea answered 7/7, 2022 at 10:56 Comment(2)
Thank you so much! It worked. but if I don't map my local machine app volumes: then if I update anything locally while developing, will it pull the change automatically (I will use nodemon)? Please let me know. Thank You?Pyroelectricity
Docker is designed as an isolation system, and the program running in a container can't usually access the code you're live-editing on your host. I'd recommend using Node without Docker for actual development.Sea
W
5

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.

Wizardly answered 25/11, 2023 at 15:2 Comment(0)
D
2

Delete the existing node_modules folder and rebuild Docker images.

Delaine answered 1/7, 2023 at 13:35 Comment(0)
R
1

I solved this problem. Just add in docker-compose.yml in volumes a code like - '/app/node_modules'.For ensure that the directory is available within the container.

Rootlet answered 24/8, 2023 at 22:54 Comment(0)
S
0

All you need to do is copy all your code inside a src/ folder. Then you should mount only that folder (i.e. src/) between the host and the docker image. That way, you will only need to rebuild your docker-compose file when you add a new package to package.json.

  #EET service
  eetapi:
    container_name: eetapi_container
    build: .
    volumes:
      - ./src/:/usr/src/app/src/
    ports:
      - "3000:3000"
    ...
Starling answered 14/4, 2024 at 11:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.