#Issue with containerizing NestJS + prisma back-end using Docker

1 messages · Page 1 of 1 (latest)

sly zenith
#

Hi all,

I'm trying to containerize my NestJS + prisma app using Docker, but running into some issues.

My dockerfile looks like this:

FROM node:20 as builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/
COPY docker.env ./

RUN npm install

COPY . .

RUN npm run build

RUN npx prisma migrate dev --name init

FROM node:20

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD [ "npm", "run", "start:prod" ]```

I'm getting a lot of errors like this:
```bash
backend: ERROR in /app/src/users/dto/get-user-with-track.dto.ts
backend: ./src/users/dto/get-user-with-track.dto.ts 9:35-49
backend: [tsl] ERROR in /app/src/users/dto/get-user-with-track.dto.ts(9,36)
backend:       TS2694: Namespace '"/app/node_modules/.prisma/client/index".Prisma' has no exported member 'UserGetPayload'.
backend:  @ ./src/users/users.controller.ts 67:46-106
backend:  @ ./src/users/users.module.ts 12:27-56
backend:  @ ./src/app.module.ts 16:23-54
backend:  @ ./src/main.ts 5:21-44
backend: 
backend: ERROR in /app/src/users/dto/get-user.dto.ts
backend: ./src/users/dto/get-user.dto.ts 1:9-13
backend: [tsl] ERROR in /app/src/users/dto/get-user.dto.ts(1,10)
backend:       TS2305: Module '"@prisma/client"' has no exported member 'Role'.
backend:  @ ./src/users/users.controller.ts 26:23-52
backend:  @ ./src/users/users.module.ts 12:27-56
backend:  @ ./src/app.module.ts 16:23-54
backend:  @ ./src/main.ts 5:21-44
backend: 
backend: ERROR in /app/src/users/dto/get-user.dto.ts
backend: ./src/users/dto/get-user.dto.ts 1:15-19
backend: [tsl] ERROR in /app/src/users/dto/get-user.dto.ts(1,16)
backend:       TS2305: Module '"@prisma/client"' has no exported member 'User'.
backend:  @ ./src/users/users.controller.ts 26:23-52
backend:  @ ./src/users/users.module.ts 12:27-56
backend:  @ ./src/app.module.ts 16:23-54
backend:  @ ./src/main.ts 5:21-44
backend: 
backend: 137 errors have detailed information that is not shown.
backend: Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.
backend: 
backend: webpack 5.89.0 compiled with 137 errors in 2357 ms```

Anyone have any idea how to solve this?
lament skiff
#

Hi @sly zenith 👋

Are you generating the Prisma Client in the Docker image? The error points towards the Prisma Client not being generated correctly.

sly zenith
#

Hey @lament skiff !

Yeah, I managed for it to generate the prisma client by some adjustments to my dockerfile:

FROM node:20 as builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/
COPY docker.env ./.env

RUN npm install

COPY . .

RUN npm run build

RUN npx prisma generate --no-engine

FROM node:20

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD [ "npm", "run", "start:prod" ]```

Yet when I run it inside my docker-compose, it still gives the same errors for some reason. If I run it on its own it doesn't give those errors.
#

My docker-compose looks like this

version: '3.5'

services:
  db:
    image: postgres
    restart: always
    environment:
      - POSTGRES_PASSWORD=postgres
    container_name: postgres
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - '0.0.0.0:5432:5432'

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: backend
    env_file:
      - docker.env
    environment:
      - PORT=${PORT}
    ports:
      - '3000:3000'
    depends_on:
      - db
    volumes:
      - ./src:/app/src
      - ./audio:/app/audio```
#

so if I build my image and then use it it works, but my database connection string is apparently wrong (using a postgres database)

``` InvalidDatasourceError: Error validating datasource db: the URL must start with the protocol prisma://


```postgresql://postgres:[email protected]:5432/postgres?schema=public``` with this as my connection string
#

while on localhost this works fine

lament skiff
sly zenith
#

I don't think I am (on localhost). Is it this way because i'm using generate --no-engine to generate my prisma files in my docker container?

lament skiff
#

The --no-engine option is used when using Accelerate in Serverless or Edge applications

sly zenith
#

Ah okay. Is there a way to generate the prisma client inside docker without being connected to a database? I don't think my container is being created correctly

#

It can't seem to reach my database running in a container called postgres during the build step with connection string postgresql://postgres:postgres@postgres:5432/postgres?schema=public

#

0.0.0.0:5432 or 127.0.0.1:5432 also didn't work

lament skiff
#

When generating the Prisma Client in your docker file, can you remove the --no-engine flag? Also, which error is being thrown when you try to build it?