N
N
No2021-03-04 19:54:35
Node.js
No, 2021-03-04 19:54:35

How to solve error in loading bcrypt_lib when building docker-compose?

Good evening, I'm trying to run an application and cluster mongodb on docker containers. But on startup, an error occurs in the bcrypt library. Application code , some files used in the docker are not yet committed and are not on the github.

internal/modules/cjs/loader.js:1122
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: Error loading shared library /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: Exec format error
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1122:18)
at Module.load (internal/modules/cjs/loader.js:928:32)


Dockerfile
FROM node:14.16.0-alpine3.10
RUN mkdir -p /app/node_modules && chown -R node:node /app/
WORKDIR /app
COPY package*.json .
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 80
CMD [ "npm", "start" ]


docker-compose:
version: '3.9'

services: 
  node-app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: node-app
    env_file: .env
    environment:
      - MONDO_PROTOCOL=mongodb
      - MONGO_USERNAME=$MONGO_USERNAME
      - MONGO_PASSWORD=$MONGO_PASSWORD
      - MONGO_HOSTNAME=mongo0
      - MONGO_PORT=27017 #default mongo port
      - MONGO_DB=$MONGO_DB
    ports:
      - "80:80"
    volumes:
      - .:/app
      - node_modules:/app/node_modules
    networks: 
      - app-network
    command: npm start
    depends_on:
      - mongo1
      - mongo2
      - mongo3
      - mongo-replica-setup

  # setup MongoDB cluster
  mongo-replica-setup:
    container_name: mongo-setup
    image: mongo
    restart: on-failure
    networks:
      - app-network
    volumes:
      - ./.docker/mongodb/scripts/mongosetup.sh:/scripts/mongosetup.sh
    # entrypoint: ["bash"]
    entrypoint: ["bash", "/scripts/mongosetup.sh" ]
    env_file:
      - .env
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
    depends_on:
      - mongo1
      - mongo2
      - mongo3

  mongo1:
    hostname: 'mongo1'
    container_name: 'mongo1'
    image: mongo
    restart: 'on-failure'
    command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
    expose: 
      - 27017
    ports: 
      - 30001:27017 
    networks: 
      - app-network
    volumes:
      - mongoData1:/data/db
      - mongoLog1:/var/log/mongodb
      - ./.docker/mongodb/initdb.d/:/docker-entrypoint-initdb.d/
      - ./.docker/mongodb/mongod.conf:/etc/mongod.conf
      - ./.docker/mongodb/file.key:/auth/file.key
    healthcheck:
      test: test $$(echo "rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
      interval: 30s
      start_period: 60s
    env_file:
      - .env
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}

  mongo2:
    hostname: 'mongo2'
    container_name: 'mongo2'
    image: mongo
    command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
    restart: 'on-failure'
    expose: 
      - 27017
    ports: 
      - 30002:27017  
    networks: 
      - app-network
    volumes:
      - mongoData2:/data/db
      - mongoLog2:/var/log/mongodb
      - ./.docker/mongodb/mongod.conf:/etc/mongod.conf
      - ./.docker/mongodb/file.key:/auth/file.key
    env_file:
      - .env
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    depends_on: 
      - mongo1

  mongo3:
    hostname: 'mongo3'
    container_name: 'mongo3'
    image: mongo
    command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
    restart: 'on-failure'
    expose: 
      - 27017
    ports: 
      - 30003:27017  
    networks: 
      - app-network
    volumes:
      - mongoData3:/data/db
      - mongoLog3:/var/log/mongodb
      - ./.docker/mongodb/mongod.conf:/etc/mongod.conf
      - ./.docker/mongodb/file.key:/auth/file.key
    env_file:
      - .env
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    depends_on: 
      - mongo1

volumes: 
  mongoData1:
  mongoData2:
  mongoData3:
  mongoLog1:
  mongoLog2:
  mongoLog3:
  node_modules:

networks: 
  app-network: 
    driver: bridge

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
No, 2021-03-04
@Ppechenka

Error solved by adding node_modules to .dockerignore

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question