'Problem about dockerizing a NestJS app with Prisma and PostgreSQL

I am trying to build a NestJS app with Prisma and PostgreSQL. I want to use docker; however, I got an error when I sent the request to the backend. Here is my docker file

FROM node:14 AS builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/

RUN npm install
RUN npx prisma generate

COPY . .

RUN npm run build

FROM node:14

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

Here is my docker-compose.yml

version: '3.8'
services:
  nest-api:
    container_name: nest-api
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    depends_on:
      - postgres
    env_file:
      - .env

  postgres:
    image: postgres:13
    container_name: postgres
    restart: always
    ports:
      - 5432:5432
    environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_DB: task-management

    env_file:
      - .env

Here is my schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  //url      = "postgresql://postgres:postgres@localhost:5432/task-management?schema=public"
   
}

model Task {
  id              Int      @id @default(autoincrement())
  title           String 
  description     String 
  status          TaskStatus  @default(OPEN)
 
}

enum TaskStatus {
  OPEN
  IN_PRO
  DOooNE
}

Here is the .env

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL=postgresql://postgres:postgres@postgres:5432/task-management?schema=public

After I run the command:docker-compose up, everything is fine. However, if I send the request to the app, I get the following error:

nest-api  | [Nest] 19  - 11/02/2021, 5:52:43 AM   ERROR [ExceptionsHandler] 
nest-api  | Invalid `this.prisma.task.create()` invocation in
nest-api  | /dist/tasks/tasks.service.js:29:33
nest-api  |
nest-api  |   26     return found;
nest-api  |   27 }
nest-api  |   28 async creatTask(data) {
nest-api  | → 29     return this.prisma.task.create(
nest-api  |   The table `public.Task` does not exist in the current database.
nest-api  | Error:
nest-api  | Invalid `this.prisma.task.create()` invocation in
nest-api  | /dist/tasks/tasks.service.js:29:33
nest-api  |
nest-api  |   26     return found;
nest-api  |   27 }
nest-api  |   28 async creatTask(data) {
nest-api  | → 29     return this.prisma.task.create(
nest-api  |   The table `public.Task` does not exist in the current database.
nest-api  |     at cb (/node_modules/@prisma/client/runtime/index.js:38537:17)
nest-api  |     at async /node_modules/@nestjs/core/router/router-execution-context.js:46:28
nest-api  |     at async /node_modules/@nestjs/core/router/router-proxy.js:9:17

What changes should I make in the docker file to solve the problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source