3

I tried to build server and db through docker

Here is my docker-compose.yml

version: '3'

services:
  api-server:
    build: ./api
    links:
      - 'db'
    ports:
      - '3000:3000'
    volumes:
      - ./api:/src
      - ./src/node_modules
    tty: true
    container_name: api-server

  db:
    build:
      context: .
      dockerfile: ./db/Dockerfile
    restart: always
    hostname: db
    environment:
      MYSQL_ROOT_PASSWORD: test
      MYSQL_USER: root
      MYSQL_PASSWORD: test
      MYSQL_DATABASE: db
    volumes:
      - './db:/config'
    ports:
      - 3306:3306
    container_name: db

Here is my Dockerfile

FROM node:alpine

WORKDIR /src
COPY . .

RUN rm -rf /src/node_modules
RUN rm -rf /src/package-lock.json

RUN yarn install

CMD yarn start:dev

After set up servers,I tried to access. but following error occured

Error: Error loading shared library /src/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: Exec format error

I wonder where is the problem. And How to fix it.

If someone has opinion,please let me know.

Thanks

Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • Does deleting the `volumes:` in the `docker-compose.yml` file help? Or equivalently, `docker-compose down -v`? – David Maze Sep 19 '20 at 11:35
  • this seems to be because bcrypt build for you machine (mac os?) is not able to work on the docker image (linux) https://stackoverflow.com/a/20590261/2422826 for me, I tried several approaches to try to get docker to compile it's own bcrypt including https://www.richardkotze.com/top-tips/install-bcrypt-docker-image-exclude-host-node-modules; however, it didn't work even making sure I had a .dockerignore file and making sure everything was removed...I did get it to work by replacing `bcrypt` with `bcryptjs` package and then running `npm rebuild`. I would love to see better steps to solve this. – auerbachb Nov 03 '20 at 00:31
  • I am in the same boat as auerbachb, I have tried everything and can't get it to work – foba Feb 11 '21 at 00:22
  • 2
    Replace `bcrypt` to `bcryptjs`. It works for me on Mac M1 – Iago Leão Feb 01 '22 at 18:48

1 Answers1

5

For everybody looking for an solution like me, the solution for me was adding a .dockerignore file with the following content:

.dockerignore
node_modules

My Dockerfile looks like this:

FROM node:14-alpine as development
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

Adding the .dockerignore-file prevents the COPY . .-command from copying the node_modules-folder and fixes the issue with bcrypt not loading.

Grooviee
  • 51
  • 1
  • 2