Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library may be corrupt (missing libz.so.1) when using distroless Docker images #16205

Closed
vimutti77 opened this issue Nov 9, 2022 · 18 comments · Fixed by prisma/prisma-engines#3951
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: docker topic: node-api formerly `nApi` topic: prisma-client topic: Unable to load ... Library may be corrupt Unable to load Node-API Library from ... , Library may be corrupt
Milestone

Comments

@vimutti77
Copy link

vimutti77 commented Nov 9, 2022

Bug description

When using prisma in gcr.io/distroless/nodejs18-debian11. I got this error

/app/node_modules/@prisma/client/runtime/index.js:34316
      throw new PrismaClientInitializationError(message, this.client._clientVersion);
            ^

PrismaClientInitializationError:
Invalid `prisma.user.count()` invocation in
/app/index.js:5:13

  2
  3 const prisma = new PrismaClient();
  4
→ 5 prisma.user.count(
Unable to load Node-API Library from /app/node_modules/.prisma/client/libquery_engine-debian-openssl-1.1.x.so.node, Library may be corrupt
    at RequestHandler.handleRequestError (/app/node_modules/@prisma/client/runtime/index.js:34316:13)  
    at /app/node_modules/@prisma/client/runtime/index.js:34737:25
    at async PrismaClient._executeRequest (/app/node_modules/@prisma/client/runtime/index.js:35301:22) 
    at async PrismaClient._request (/app/node_modules/@prisma/client/runtime/index.js:35273:16) {      
  clientVersion: '4.6.0',
  errorCode: undefined
}

How to reproduce

  1. Clone https://github.com/vimutti77/prisma-napi-distroless-bug
  2. Run npm run build
  3. Run npm run start
  4. See error

Expected behavior

It should no error and log count: 0

Prisma information

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String?
  published Boolean @default(false)
  author    User    @relation(fields: [authorId], references: [id])
  authorId  Int
}
const { PrismaClient } = require("@prisma/client");

const prisma = new PrismaClient();

prisma.user.count().then((count) => {
  console.log(`count: ${count}`);
});

Environment & setup

  • OS: gcr.io/distroless/nodejs18-debian11
  • Database: SQL Server
  • Node.js version: 18

Prisma Version

I got this error when I try to check the prisma version inside the docker container

Error: Unable to require(`/app/node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node`)
 libz.so.1: cannot open shared object file: No such file or directory
    at load (/app/node_modules/prisma/build/index.js:89146:11)
    at getEngineVersion (/app/node_modules/prisma/build/index.js:89944:16)
Error: Command failed with exit code 127: /app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x --version
/app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
    at makeError (/app/node_modules/prisma/build/index.js:3641:18)
    at handlePromise (/app/node_modules/prisma/build/index.js:4409:33)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     
    at async getEngineVersion (/app/node_modules/prisma/build/index.js:89947:20) {    
  shortMessage: 'Command failed with exit code 127: /app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x --version',
  command: '/app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x --version',
  escapedCommand: '"/app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x" --version',
  exitCode: 127,
  signal: undefined,
  signalDescription: undefined,
  stdout: '',
  stderr: '/app/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory',
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}
Error: Command failed with exit code 127: /app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x --version
/app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
    at makeError (/app/node_modules/prisma/build/index.js:3641:18)
    at handlePromise (/app/node_modules/prisma/build/index.js:4409:33)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     
    at async getEngineVersion (/app/node_modules/prisma/build/index.js:89947:20) {    
  shortMessage: 'Command failed with exit code 127: /app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x --version',
  command: '/app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x --version',
  escapedCommand: '"/app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x" --version',
  exitCode: 127,
  signal: undefined,
  signalDescription: undefined,
  stdout: '',
  stderr: '/app/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory',
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}
prisma                  : 4.6.0
@prisma/client          : 4.6.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : E_CANNOT_RESOLVE_VERSION (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : E_CANNOT_RESOLVE_VERSION (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : E_CANNOT_RESOLVE_VERSION (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 2e719efb80b56a3f32d18a62489de95bb9c130e3 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Format Wasm             : @prisma/prisma-fmt-wasm 4.6.0-53.2e719efb80b56a3f32d18a62489de95bb9c130e3
Default Engines Hash    : 2e719efb80b56a3f32d18a62489de95bb9c130e3
Studio                  : 0.476.0
@vimutti77 vimutti77 added the kind/bug A reported bug. label Nov 9, 2022
@vimutti77 vimutti77 changed the title Get unable to load Node-API Library when using prisma in docker from gcr.io/distroless/nodejs18-debian11 Get unable to load Node-API Library when using prisma in docker container from gcr.io/distroless/nodejs18-debian11 Nov 9, 2022
@SevInf SevInf added team/client Issue for team Client. topic: docker bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Nov 10, 2022
@janpio janpio changed the title Get unable to load Node-API Library when using prisma in docker container from gcr.io/distroless/nodejs18-debian11 Get Unable to load Node-API Library when using prisma in docker container from gcr.io/distroless/nodejs18-debian11 Nov 10, 2022
@nassim-kachroud
Copy link

nassim-kachroud commented Nov 27, 2022

Same error on my side using Docker, any news?

{
ds-back |   message: '\n' +
ds-back |     'Invalid `helpers_1.getPrismaFromContext)(ctx).categories.findMany()` invocation in\n' +
ds-back |     '/node_modules/@generated/type-graphql/resolvers/crud/Categories/FindManyCategoriesResolver.js:13:68\n' +
ds-back |     '\n' +
ds-back |     '  10 let FindManyCategoriesResolver = class FindManyCategoriesResolver {\n' +
ds-back |     '  11     async findManyCategories(ctx, info, args) {\n' +
ds-back |     '  12         const { _count } = (0, helpers_1.transformFields)((0, graphql_fields_1.default)(info));\n' +
ds-back |     '→ 13         return (0, helpers_1.getPrismaFromContext)(ctx).categories.findMany(\n' +
ds-back |     'Unable to load Node-API Library from /node_modules/.prisma/client/libquery_engine-debian-openssl-1.1.x.so.node, Library may be corrupt',
ds-back |   code: 'INTERNAL_SERVER_ERROR',
ds-back |   exception: {
ds-back |     clientVersion: '4.6.1',
ds-back |     errorCode: undefined,
ds-back |     stacktrace: [
ds-back |       'Error: ',
ds-back |       'Invalid `helpers_1.getPrismaFromContext)(ctx).categories.findMany()` invocation in',
ds-back |       '/node_modules/@generated/type-graphql/resolvers/crud/Categories/FindManyCategoriesResolver.js:13:68',
ds-back |       '',
ds-back |       '  10 let FindManyCategoriesResolver = class FindManyCategoriesResolver {',
ds-back |       '  11     async findManyCategories(ctx, info, args) {',
ds-back |       '  12         const { _count } = (0, helpers_1.transformFields)((0, graphql_fields_1.default)(info));',
ds-back |       '→ 13         return (0, helpers_1.getPrismaFromContext)(ctx).categories.findMany(',
ds-back |       'Unable to load Node-API Library from /node_modules/.prisma/client/libquery_engine-debian-openssl-1.1.x.so.node, Library may be corrupt',
ds-back |       '    at RequestHandler.handleRequestError (/node_modules/@prisma/client/runtime/index.js:34316:13)',
ds-back |       '    at /node_modules/@prisma/client/runtime/index.js:34737:25',
ds-back |       '    at processTicksAndRejections (node:internal/process/task_queues:96:5)',
ds-back |       '    at async PrismaClient._executeRequest (/node_modules/@prisma/client/runtime/index.js:35301:22)',
ds-back |       '    at async PrismaClient._request (/node_modules/@prisma/client/runtime/index.js:35273:16)'
ds-back |     ]
ds-back |   }

@jkomyno
Copy link
Contributor

jkomyno commented Dec 5, 2022

Hi @nassim-kachroud, could you tell us exactly which Docker image you're using? Ideally, are you able to share your Dockerfile with us? Thanks.

@jee-man
Copy link

jee-man commented Dec 6, 2022

Having the same issue.

@Toilal
Copy link

Toilal commented Dec 13, 2022

Same error here using a node:16-alpine based docker Image.

@ssliman
Copy link

ssliman commented Dec 13, 2022

Is there a workaround for this issue?

@Toilal
Copy link

Toilal commented Dec 13, 2022

I have tried to upgrade to node 18, but no luck.

@Toilal
Copy link

Toilal commented Dec 13, 2022

I can workaround by using node:18-alpine3.16 instead of default node:18-alpine3.17 image.

So it seems the issue only occurs with newest distrib versions, maybe the toolchain used to build prisma binary is a bit dated ?

@arapl3y
Copy link

arapl3y commented Dec 14, 2022

I was getting this issue on node:16-alpine. Switching to node:16.17-alpine seems to have done the trick.

@iamchanii
Copy link

I faced same issue. this is my workaround: set up image tag explicitly to use alpine 3.16.

-FROM node:16-alpine
+FROM node:16-alpine3.16
WORKDIR /app
COPY --from=prod-build /app/prod_node_modules ./node_modules
COPY --from=build /app/dist              ./dist
COPY --from=build /app/prisma            ./prisma
COPY --from=build /app/package.json ./package.json

@chaychoong
Copy link

Is this a problem with docker or prisma?

@janpio
Copy link
Member

janpio commented Dec 14, 2022

Compatibility between the content of the Docker image and the software it is running, and how Prisma detects and handles these.

@josethz00
Copy link

Having the same problem with docker running on WSL-2 at Windows 11, already tried different versions of node, without success so far

@josethz00
Copy link

This trick solved the problem for me: t3-oss/create-t3-app#975 (comment)

@jkomyno
Copy link
Contributor

jkomyno commented Jan 4, 2023

For people mentioning node:16-alpine, please refer to this solution (the easiest one is upgrading to prisma@4.8.0).

Let's keep the discussion on this issue focused on gcr.io/distroless/nodejs18-debian11 (and potentially other distroless images), thank you.

@jkomyno jkomyno added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jan 4, 2023
@jkomyno
Copy link
Contributor

jkomyno commented Jan 4, 2023

@vimutti77 a deeper inspection of this issue revealed that the Prisma Client fails to import the query engine because the libz system library dependency doesn't exist on grc.io/distroless images.

From your reproduction, I see you're using a multi-stage Docker build process, with node:18.12.1-bullseye-slim (aliased as deps) as your base image. When using the distroless image, should import the libz library from deps.

Assuming you're running Docker on an amd64 machine, you can change the final part of your Dockerfile as:

FROM gcr.io/distroless/nodejs18-debian11:debug

# set working directory
WORKDIR /app/

# copy node_modules
COPY --from=deps /app/node_modules/ /app/node_modules/

# copy libz.so.1 from Debian, which was causing the
# "Library may be corrupt" error
COPY --from=deps /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1

# copy db & script
COPY prisma /app/prisma
COPY index.js /app/index.js

CMD ["/app/index.js"]

If you're on arm64, simply substitute x86_64 in the libz.so.1 path with aarch64.

@jkomyno jkomyno changed the title Get Unable to load Node-API Library when using prisma in docker container from gcr.io/distroless/nodejs18-debian11 Library may be corrupt (missing libz.so.1) when using the gcr.io/distroless/nodejs18-debian11 Docker image Jan 4, 2023
@jkomyno jkomyno changed the title Library may be corrupt (missing libz.so.1) when using the gcr.io/distroless/nodejs18-debian11 Docker image Library may be corrupt (missing libz.so.1) when using distroless Docker images Jan 4, 2023
@vimutti77
Copy link
Author

@vimutti77 a deeper inspection of this issue revealed that the Prisma Client fails to import the query engine because the libz system library dependency doesn't exist on grc.io/distroless images.

From your reproduction, I see you're using a multi-stage Docker build process, with node:18.12.1-bullseye-slim (aliased as deps) as your base image. When using the distroless image, should import the libz library from deps.

Assuming you're running Docker on an amd64 machine, you can change the final part of your Dockerfile as:

FROM gcr.io/distroless/nodejs18-debian11:debug

# set working directory
WORKDIR /app/

# copy node_modules
COPY --from=deps /app/node_modules/ /app/node_modules/

# copy libz.so.1 from Debian, which was causing the
# "Library may be corrupt" error
COPY --from=deps /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1

# copy db & script
COPY prisma /app/prisma
COPY index.js /app/index.js

CMD ["/app/index.js"]

If you're on arm64, simply substitute x86_64 in the libz.so.1 path with aarch64.

Thanks, now it works.

Maybe we should add about this in the documentation then close the issue?

@aqrln
Copy link
Member

aqrln commented May 4, 2023

Reopening for now because engineer must be updated in prisma-engines now, will be closed by prisma/prisma-engines#3951

@aqrln
Copy link
Member

aqrln commented May 5, 2023

Merged, the issue won't occur anymore starting with Prisma 4.14.0.

@janpio janpio added this to the 4.14.0 milestone May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: docker topic: node-api formerly `nApi` topic: prisma-client topic: Unable to load ... Library may be corrupt Unable to load Node-API Library from ... , Library may be corrupt
Projects
None yet
Development

Successfully merging a pull request may close this issue.