Home > Blockchain >  Unable to run prisma code in docker container
Unable to run prisma code in docker container

Time:12-25

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.

CodePudding user response:

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

CodePudding user response:

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"]
  • Related