Search code examples
node.jsdockernode-modulesnestjsargon2-ffi

Argon2 with node docker container


I have NestJs application that works perfectly on my local machine (windows). Now I would like to create a docker container for my application. When i try to start my container i have one issue with Argon2. I install on the container all the argon needs. where's my mistake?

Thank

internal/modules/cjs/loader.js:1025
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: Error loading shared library /usr/src/app/node_modules/argon2/build/Release/argon2.node: Exec format error
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1025:18)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at load (/usr/src/app/node_modules/node-gyp-build/index.js:20:10)
    at Object.<anonymous> (/usr/src/app/node_modules/argon2/argon2.js:5:81)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)

Dockerfile :

FROM node:12.13-alpine As development

WORKDIR /usr/src/app

COPY package*.json ./

# --no-cache: download package index on-the-fly, no need to cleanup afterwards
# --virtual: bundle packages, remove whole bundle at once, when done
RUN apk --no-cache --virtual build-dependencies add \
    python \
    make \
    g++ \
    && npm install \
    && apk del build-dependencies
RUN npm install -g node-gyp
RUN npm install argon2
RUN npm install --only=development

COPY . .

RUN npm run build

FROM node:12.13-alpine as production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

# --no-cache: download package index on-the-fly, no need to cleanup afterwards
# --virtual: bundle packages, remove whole bundle at once, when done
RUN apk --no-cache --virtual build-dependencies add \
    python \
    make \
    g++ \
    && npm install \
    && apk del build-dependencies
RUN npm install -g node-gyp
RUN npm install argon2
RUN npm install --only=production

COPY . .

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/main"]

docker-compose :

version: "3.7"

services:
  main:
    container_name: NestApp
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      #- /usr/src/app/node_modules
    ports:
      - 3001:3001
    command: npm run start:dev
    networks:
      - webnet
    depends_on:
      - mysql
  mysql:
    image: mysql:5
    restart: always
    networks:
      - webnet
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: bdd
    ports:
      - "3306:3306"
    volumes:
      - my-db:/var/lib/mysql
  adminer:
    image: adminer
    restart: always
    ports:
      - 8085:8080
    networks:
      - webnet
networks:
  webnet:
    driver: bridge
# Names our volume
volumes:
  my-db:


Solution

  • Answering for posterity sake:

    Sounds like you have no .dockerignore file so when you COPY . . you are pulling over the node_modules folder from your local machine (Windows) and trying to run it in Docker (Alpine Linux), so while you correctly run npm install argon2 in the Docker context, you overwrite it when you copy from local to Docker. If you create a .dockerignore file, add /node_modules to it so that you don;t move the node_modules directory