medz / prisma-dart

Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.
https://prisma.pub
BSD 3-Clause "New" or "Revised" License
453 stars 31 forks source link

Issue with Prisma Client Dart - Query Engine Not Found in Dockerized Environment (QE404 Error) #439

Closed Hackman-Adu closed 6 days ago

Hackman-Adu commented 6 days ago

What version of Prisma Dart is running?

5.20.0

What version of Prisma CLI is running?

5.20.0

What type of app are you using?

Dart Native (Server/CLI)

What database are you using?

MySQL

What steps can reproduce the bug?

Set up a Dart project with Prisma, and add a Prisma schema (schema.prisma). Create the following Dockerfile to containerize the app:

FROM dart:stable AS build
WORKDIR /app

# Install Node.js and Prisma
RUN apt-get update && apt-get install -y curl && \
    curl -sL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs
RUN npm install -g prisma

# Install Dart dependencies and Prisma schema
COPY pubspec.* ./
RUN dart pub get
COPY prisma ./prisma

# Generate Prisma Client and Binaries
RUN dart run orm generate

# Compile Dart app
COPY . .
RUN dart compile exe bin/server.dart -o bin/server

FROM alpine:latest
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/
CMD ["/app/bin/server"]

Ensure that the schema.prisma file includes the necessary binary targets

generator client {
  provider      = "dart run orm"
  binaryTargets = ["native", "linux-musl"]
}

This ensures the Prisma query engine is built for a Linux-based environment. In my case, it's AWS EC2 Instance, Ubuntu

Build the Docker image and run the container

docker build -t your-image-name .
docker run -p 8080:8080 your-image-name

What is the expected behavior?

Prisma should generate the necessary query engine binaries inside the Docker container during the dart run orm generate step, and the application should run without errors.

What do you see instead?

At runtime, the following error is raised PrismaClientInitializationError: QE404 No binary engine found, please make sure any of the following locations contain the executable file: {prisma-query-engine, prisma/prisma-query-engine, .dart_tool/prisma-query-engine}

No prisma-query-engine binary is found in the expected locations inside the Docker container (/app/.dart_tool/prisma-query-engine).

Additional information

On query , the below error throws

PrismaClientInitializationError: QE404 No binary engine found, please make sure any of the following locations contain the executable file: {prisma-query-engine, prisma/prisma-query-engine, .dart_tool/prisma-query-engine}

medz commented 6 days ago

Docs:

By looking at your Dockerfile, I found two problems:

  1. You did not find the so library required by Prisma engine in your Dockerfile

  2. You did not copy the prisma binary engine downloaded in the build build step to the same level as your app binary.

Please visit https://prisma.pub/getting-started/deployment.html to read the deployment document.

medz commented 6 days ago

Ensure that the schema.prisma file includes the necessary binary targets

It is not required, Prisma CLI will automatically find the appropriate engine.

[!TIP] If you specify a target for it, make sure it is correct.

medz commented 6 days ago

In addition, I noticed that there are potential bugs in the order of instructions in some dockerfiles:

  1. Build the client command before copying the source code - if you have the generated client locally, the client of the instruction will be overwritten and cause an error
  2. Same as 1, which will cause the binary engine architecture to be incorrect

Correct approach:

  1. Find the so library that prisma depends on and add it to /runtime/
  2. You should copy pubspec.* first to install deps
  3. Copy the source code
  4. Generate prisma client
Hackman-Adu commented 5 days ago

Issue resolved, thank you for your help @medz