0

Working on my dev environment on MacOs.

I'm meeting the following error

Error: /usr/src/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header

when I run:

docker-compose up

I have been looking for all similar answers but none really helped me.

Here are the different files that could interest you:

.dockerignore

node_modules
npm-debug.log
coverage
.prettierrc
nodemon.json
.git
.gitignore
LICENSE
VERSION
README.md
Changelog.md
docker-compose.yml
upload
to_reboot

docker-compose.yml

version: '3.8'

services:
    db:
        image: mongo
        volumes:
            - ./backend:/data/db
            - /usr/src/app/node_modules
    dev:
        container_name: nestjs_api_dev
        image: nestjs-api-dev:1.0.0
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        command: npm run start:debug
        depends_on:
            - db
        ports:
            - 8000:8000
            - 9229:9229
        volumes:
            - .:/usr/src/app
        restart: unless-stopped
        environment:
            - MONGO_URI=${MONGO_URI}
            - SECRET_KEY=${SECRET_KEY}

> Dockerfile

    FROM node:16 AS development
    
    WORKDIR /usr/src/app
    
    COPY package*.json ./
    
    RUN npm install glob rimraf
    
    RUN npm install
    
    COPY . .
    
    RUN npm run build

If someone could be able to help me solve the issue as well as explaining the answer so I can understand and make sure it never happens again, I'd be glad.

  • The `volumes:` block is replacing the code in the image, and is replacing the Linux `node_modules` tree that the Dockerfile installs with the MacOS version from your host. Delete the `volumes:` block to use the code built into the image, and `brew install node` to get a Node interpreter for normal development. – David Maze Feb 21 '23 at 11:29
  • @DavidMaze Which ```volumes:``` block are you speaking about ? I have two in my docker-compose.yml, one in db part and one in dev part. – Arthur BERTAUD Feb 21 '23 at 13:23
  • In the `dev` container you're causing the image code to be overwritten, and you should delete that `volumes:` block. – David Maze Feb 22 '23 at 10:51

1 Answers1

0

Alright I achieved to solve my problem, I'll post the solution in case someone else would need it.

Put yourself in the root directory of your repo.

1 - Delete all the things currently running on your docker which are related to your repo.

docker rm <container> / docker rmi <image_id> / docker volume rm <volume_id>

2 - Add - /usr/src/app/node_modules to your docker-compose.yml so that it looks like:

version: '3.8'

services:
    db:
        image: mongo
        volumes:
            - ./backend:/data/db
    dev:
        container_name: nestjs_api_dev
        image: nestjs-api-dev:1.0.0
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        command: npm run start:debug
        depends_on:
            - db
        ports:
            - 8000:8000
            - 9229:9229
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        restart: unless-stopped
        environment:
            - MONGO_URI=${MONGO_URI}
            - SECRET_KEY=${SECRET_KEY}

3 - Delete your package-lock.json

4 - Reinstall your dependencies with npm install

5 - Run docker-compose up

The problem is solved.

  • If this resolves your problem, consider flagging this question as a duplicate of [Add a volume to Docker, but exclude a sub-folder](https://stackoverflow.com/questions/29181032/add-a-volume-to-docker-but-exclude-a-sub-folder). Note that your image content is totally ignored, except that the `node_modules` directory is copied into the anonymous volume the very first time it is used; the folder is not actually excluded from the mount, and any changes to `package.json` will be ignored. – David Maze Feb 22 '23 at 11:26