skip to Main Content

I am able to connect and use prisma in my nextjs project on my local but when i try to use same code using dockerfile.
I am getting error. I am using the following Dockerfile

FROM node:16-slim
RUN apt-get update
RUN apt-get install -y openssl

WORKDIR /usr/src/app

ENV PORT 3030

COPY ["package.json", "package-lock.json", "./"]
COPY prisma ./prisma/

RUN npm install --production --silent && mv node_modules ../
RUN npx prisma generate

COPY . .

RUN npx next build

EXPOSE 3030

CMD ["npx", "next", "start"]

When code reaches prisma line application crashes with error. Error message is not clear as it returns errorCode and client as Object.

try {
        const { id } = req.body as any;
        const user = await prisma.user.findUnique({
            where: { sfid: id },

        });

    } catch (error) {

        res.status(200).json({ id: 'error', cart: error, products: [] })

    }

This line const user = await prisma.user.findUnique({ crashes the application in docker.

2

Answers


  1. Chosen as BEST ANSWER

    Used the following docker file and its working now

    FROM node:lts
    RUN apt-get update
    RUN apt-get install -y openssl
    
    WORKDIR /usr/src/app
    
    ENV PORT 3030
    
    COPY ["package.json", "package-lock.json", "./"]
    COPY .env ./.env
    COPY prisma ./prisma/
    
    RUN npm install --production --silent && mv node_modules ../
    
    RUN npm i -g prisma
    
    COPY . .
    
    RUN npx prisma generate --schema ./prisma/schema.prisma
    RUN npx next build
    
    EXPOSE 3030
    
    CMD ["npx", "next", "start"]
    

  2. Do database run on same host as your docker image ?
    What address do you use ?

    In case of localhost and 0.0.0.0 => you are looking for database inside you docker container, not full host server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search