21

I am using prisma, postgres, docker, kubernets.

npx prisma migrate dev working.

and npx prisma generate produce below output:

✔ Generated Prisma Client (2.23.0) to ./node_modules/@prisma/client in 68ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

but when I tried to use in my route file produce the error:

new-route.ts

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

my docker file:

FROM node:alpine

WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .

CMD ["npm", "start"]
Rafiq
  • 8,987
  • 4
  • 35
  • 35
  • 1
    I think if you copy `COPY prisma ./prisma/` that should work. But I'm a few steps behind you as I can't get the `npx prisma migrate dev` to work with the error message: `Error: P1001: Can't reach database server at auth-postgres-srv.default.svc.cluster.local:5432` – SakoBu Mar 23 '22 at 15:25
  • Just wanted to add my 2 cents to this. Recently spent half a day trying to figure out why my schema changes weren't being reflected when calling Prisma service. Turns out I had to restart the node environment or as I like to do it just restart the whole IDE and voila the changes start reflecting. – Haroon Ramay Oct 12 '22 at 14:20

6 Answers6

10

I know that this has been marked as solved, but I just wanted to share my setup for anyone interested.

Dockerfile

# Build image
FROM node:16.13-alpine as builder
WORKDIR /app

# Not sure if you will need this
# RUN apk add --update openssl

COPY package*.json ./
RUN npm ci --quiet

COPY ./prisma prisma
COPY ./src src
RUN npm run build

# Production image

FROM node:16.13-alpine
WORKDIR /app
ENV NODE_ENV production

COPY package*.json ./
RUN npm ci --only=production --quiet

COPY --chown=node:node --from=builder /app/prisma /app/prisma
COPY --chown=node:node --from=builder /app/src /app/src

USER node

EXPOSE 8080
CMD ["node", "src/index.js"]

package.json

{
  "name": "example",
  "description": "",
  "version": "0.1.0",
  "scripts": {
    "generate": "npx prisma generate",
    "deploy": "npx prisma migrate deploy",
    "dev": "npm run generate && nodemon --watch \"src/**\" --ext \"js,json\" --exec \"node src/index.js\"",
    "build": "npm run generate",
    "start": "npm run build && node build/index.js"
  },
  "prisma": {
    "schema": "prisma/schema.prisma"
  },
  "dependencies": {
    "@prisma/client": "^3.6.0"
  },
  "devDependencies": {
    "@tsconfig/node16": "^1.0.2",
    "@types/node": "^16.11.12",
    "nodemon": "^2.0.15",
    "prisma": "^3.6.0"
  }
}

I run this in Kubernetes. To make things smooth with database and migrations I run an initContainer that runs the prisma migrate deploy.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: EXAMPLE
spec:
  replicas: 1
  selector:
    matchLabels:
      app: EXAMPLE
  strategy:
    rollingUpdate:
      maxSurge: 100%
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: EXAMPLE
    spec:
      containers:
        image: DOCKER_IMAGE
        imagePullPolicy: IfNotPresent
        name: SERVICE_NAME
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
      initContainers:
      - command:
        - npm
        - run
        - deploy
        image: DOCKER_IMAGE
        imagePullPolicy: IfNotPresent
        name: database-migrate-deploy

(This is a live service I just copied and stripped away anything non essential)

I hope this could be of use to someone

CalvinMcGee
  • 203
  • 3
  • 9
  • 1
    how do you run prisma migrations and seed for production? you just ran generate in Dockerfile – marko kraljevic Dec 30 '21 at 11:38
  • thanks for this, very useful. However, can you explain why you copy your `node_modules` from your build image? Doesn't that overwrite your lean production `node_modules` with all the dev dependencies and bloat your final image? – JP Lew Jan 01 '22 at 11:41
  • 1
    @JPLew You're absolutely correct, that statement should not be there. I'm updating my answer. – CalvinMcGee Jan 02 '22 at 13:25
  • 2
    @markokraljevic As far as i know `npx prisma generate` only creates the `./prisma/generated` directory. So it makes perfect sense to have that in the Dockerfile. – CalvinMcGee Jan 02 '22 at 13:40
  • And I am facing this error , ``` Error: Unknown binaryTarget linux-arm64-openssl-undefined and no custom engine files were provided```. I have added the binary target in schema.prisma but I couldn’t resolve the error. ``` binaryTargets = ["native", "rhel-openssl-1.0.x"]``` – Subash Shrestha Aug 21 '22 at 10:51
8

I usually don't use docker for this while developing, but I have this issue every time I change something in my schema.prisma and have to use npx prisma generate. The solution for me is to restart the node application running npm start again. Maybe if you restart your containers it might work.

if you are inside kubernets pod then access the pod using terminal then give generate command

kubectl exec -it pod_name sh
npx prisma generate
Rafiq
  • 8,987
  • 4
  • 35
  • 35
  • The solution given by Rafiq to run `npx prisma generate` in the pod is likely a bad one - next time the pod is restarted it will fail again. Just [copy the `prisma` dir when building your image](https://stackoverflow.com/a/72846505/675721) – Codebling Jun 30 '23 at 05:36
5

Here is another way to solve this.

Since the .prisma folder is needed by the prisma client as shown in the picture below or in the docs another way is to make sure it ships with your code. You can do this as follows.

Wrong way: ship the generated folder

You would think you can just include the generated files in the image by adding an exception rule for the .prisma folder to your .dockerignore (notice the exclamation point)

node_modules/
!nodes_modules/.prisma

But the query engine used by prisma is different for each operating system so you could run into troubles.

Correct way : generate the files with the image

Just add RUN npx prisma generate to your Dockerfile before the build command. This way the files are generated during the image creation and you don't have to run the prisma generate command on every container. The downside of this method is that your docker image will be larger. If this is an issue you can try with the other answers.

.prisma folder is needed to reference your generated code

fcrozatier
  • 350
  • 3
  • 9
4

You forgot to copy the prisma directory, since generating the Prisma Client requires the schema.prisma file. You should copy the whole prisma directory in case you also need the migrations.

Your final Dockerfile should contain the following:

WORKDIR /app
COPY package*.json .
COPY prisma ./prisma/ 
RUN npm install --only=prod
Alex Zenin
  • 49
  • 1
1

For me, I just changed my start under scripts in my package.json so that my app would generate prisma types before running:

"start": "npx prisma generate && nodemon server.ts"

This worked for me – I just wanted to drop this here for anyone who runs into the same issue.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

This error happens because docker doesn't install devDependencies, which means that it doesn't pick up the Prisma CLI.

To remedy this you can add the prisma CLI package to your dependencies instead of the devDependencies. (Making sure to run npm install afterward to update the package-lock.json)

GuardianAngel
  • 103
  • 1
  • 7