Search code examples
dockernestjsprisma

Docker container run failed with error: Error: Error relocating /node_modules/argon2/lib/binding/napi-v3/argon2.node: unsupported relocation type 7


I tried building a custom docker image for a nestjs server and prisma. But on running the container using the built image it throws the following error:

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

Error: Error relocating /node_modules/argon2/lib/binding/napi-v3/argon2.node: unsupported relocation type 7
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1280:18)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/node_modules/argon2/argon2.js:9:25)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12) {
  code: 'ERR_DLOPEN_FAILED'
}

Dockerfile looks like:

FROM --platform=linux/x86_64 node:16 AS builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/
COPY .env ./

RUN yarn

RUN yarn global add peer

COPY . .

RUN yarn run build

FROM node:16-alpine

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 8002 8001 9000

CMD [ "yarn", "start:prod" ]

Docker image build command: docker build -t nest-server .

Docker container run command: docker run -p 8002:8002 -p 8001:8001 nest-server

Environment details:

OS: OSX Ventura 13.3.1
Docker version: 20.10.24
NestJS: 9.0.0

.dockerignore file content:

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

I have tried using solutions mentioned in this question, but it doesn't works for me!


Solution

  • From the error description, it looks like the issue is caused by the argon2 library, which is a native module and requires platform-specific binaries. When you build your Docker image using the node:16 image, you are building on a Linux x86_64 platform. However, when you use the node:16-alpine image in the second stage, you are using a different platform which may cause incompatibilities with the argon2 native module.

    I would recommend you to change the second stage to use node:16 instead of node:16-alpine:

    FROM node:16
    
    COPY --from=builder /app/node_modules ./node_modules
    COPY --from=builder /app/package*.json ./
    COPY --from=builder /app/dist ./dist
    
    EXPOSE 8002 8001 9000
    
    CMD [ "yarn", "start:prod" ]
    

    This will ensure that both stages of the Docker build use the same platform. However, using node:16 instead of node:16-alpine will result in a larger image size.