notiz-dev / nestjs-prisma-docker Goto Github PK
View Code? Open in Web Editor NEWDockerizing a NestJS app with Prisma and PostgreSQL
Home Page: https://notiz.dev/blog/dockerizing-nestjs-with-prisma-and-postgresql
Dockerizing a NestJS app with Prisma and PostgreSQL
Home Page: https://notiz.dev/blog/dockerizing-nestjs-with-prisma-and-postgresql
After launch:
docker-compose up
it receives this error:
(node:21) UnhandledPromiseRejectionWarning: Error: Can't reach database server at `postgresprisma`:`5432`
nest-api |
nest-api | Please make sure your database server is running at `postgresprisma`:`5432`.
nest-api | at /node_modules/@prisma/client/runtime/index.js:36260:21
nest-api | (Use `node --trace-warnings ...` to show where the warning was created)
nest-api | (node:21) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
nest-api | (node:21) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The database container started correctly, but the API container starts with an error.
When creating migrations using docker-compose it shows this error. In case I try to make prisma studio show error on clicking food. How to fix?
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "food", schema "food" at "postgresprisma:5432"
Error: P1001: Can't reach database server at postgresprisma
:5432
Please make sure your database server is running at postgresprisma
:5432
.
Hi, thank you for publishing this sample repo.
I'm having a problem - the app container doesn't connect to the database container.
.env
file so it looks like this:POSTGRES_USER=prisma
POSTGRES_PASSWORD=topsecret
POSTGRES_DB=food
# nest run in docker container
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?schema=food&sslmode=prefer
# nest run locally
# DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}?schema=food&sslmode=prefer
docker compose up
curl http://localhost:3000/foods
I'm getting {"statusCode":500,"message":"Internal server error"}%
in curl output.
Here are logs:
$ docker compose up
Attaching to nest-api, postgresprisma
postgresprisma |
postgresprisma | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgresprisma |
postgresprisma | 2024-01-16 22:08:32.262 UTC [1] LOG: starting PostgreSQL 13.13 (Debian 13.13-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
postgresprisma | 2024-01-16 22:08:32.263 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgresprisma | 2024-01-16 22:08:32.263 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgresprisma | 2024-01-16 22:08:32.265 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresprisma | 2024-01-16 22:08:32.267 UTC [27] LOG: database system was shut down at 2024-01-16 22:05:12 UTC
postgresprisma | 2024-01-16 22:08:32.271 UTC [1] LOG: database system is ready to accept connections
nest-api |
nest-api | > [email protected] start:prod /
nest-api | > node dist/main
nest-api |
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [NestFactory] Starting Nest application...
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [InstanceLoader] PrismaModule dependencies initialized +24ms
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [InstanceLoader] AppModule dependencies initialized +1ms
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [RoutesResolver] AppController {/}: +2ms
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [RouterExplorer] Mapped {/, GET} route +1ms
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [RouterExplorer] Mapped {/foods, GET} route +1ms
nest-api | [Nest] 19 - 01/16/2024, 10:08:32 PM LOG [NestApplication] Nest application successfully started +52ms
postgresprisma | 2024-01-16 22:09:38.937 UTC [34] ERROR: relation "food.Food" does not exist at character 54
postgresprisma | 2024-01-16 22:09:38.937 UTC [34] STATEMENT: SELECT "food"."Food"."id", "food"."Food"."name" FROM "food"."Food" WHERE 1=1 OFFSET $1
nest-api | [Nest] 19 - 01/16/2024, 10:09:38 PM ERROR [ExceptionsHandler]
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | Error:
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | at cb (/node_modules/@prisma/client/runtime/index.js:38504: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
postgresprisma | 2024-01-16 22:11:34.942 UTC [34] ERROR: relation "food.Food" does not exist at character 54
postgresprisma | 2024-01-16 22:11:34.942 UTC [34] STATEMENT: SELECT "food"."Food"."id", "food"."Food"."name" FROM "food"."Food" WHERE 1=1 OFFSET $1
nest-api | [Nest] 19 - 01/16/2024, 10:11:34 PM ERROR [ExceptionsHandler]
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | Error:
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | at cb (/node_modules/@prisma/client/runtime/index.js:38504: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
postgresprisma | 2024-01-16 22:11:37.499 UTC [34] ERROR: relation "food.Food" does not exist at character 54
postgresprisma | 2024-01-16 22:11:37.499 UTC [34] STATEMENT: SELECT "food"."Food"."id", "food"."Food"."name" FROM "food"."Food" WHERE 1=1 OFFSET $1
nest-api | [Nest] 19 - 01/16/2024, 10:11:37 PM ERROR [ExceptionsHandler]
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | Error:
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | at cb (/node_modules/@prisma/client/runtime/index.js:38504: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
I'll be very grateful for any help! I cannot seem to make Prisma work properly in my containerized Nest.js app.
I've cloned the repo, but getting this error:
postgresprisma | 2023-02-02 21:21:31.356 UTC [34] ERROR: relation "food.Food" does not exist at character 54
postgresprisma | 2023-02-02 21:21:31.356 UTC [34] STATEMENT: SELECT "food"."Food"."id", "food"."Food"."name" FROM "food"."Food" WHERE 1=1 OFFSET $1
nest-api | [Nest] 19 - 02/02/2023, 9:21:31 PM ERROR [ExceptionsHandler]
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | Error:
nest-api | Invalid `this.prismaService.food.findMany()` invocation in
nest-api | /dist/app.controller.js:25:40
nest-api |
nest-api | 22 return this.appService.getHello();
nest-api | 23 }
nest-api | 24 getFoods() {
nest-api | → 25 return this.prismaService.food.findMany()
nest-api | The table `food.Food` does not exist in the current database.
nest-api | at cb (/node_modules/@prisma/client/runtime/index.js:38504: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
Please add more details about when we have seeds. Personally I did this:
Dockerfile
# As you understood I have another base.Dockerfile to do npm ci and commit it as my base image. I did this because in my area Net bother me a lot and my image build time was around 20 min and mostly failed due to Socket timeout error while npm ci
# FROM node:16.14.0-alpine3.15 as build_stage
FROM take-report:dep as build_stage
# README: Because WORKDIR is set in the take-report:dep I ignore to use it here again
# WORKDIR /app
# When using COPY with more than one source file, the destination must be a directory and end with a /
COPY prisma ./prisma/
COPY . .
RUN npx prisma generate
RUN npm run build
# RUN npm prune --production
FROM node:16.14.0-alpine3.15
WORKDIR /app
COPY --from=build_stage /app/node_modules ./node_modules
COPY --from=build_stage /app/package*.json ./
COPY --from=build_stage /app/tsconfig*.json ./
COPY --from=build_stage /app/dist ./dist
COPY --from=build_stage /app/prisma ./prisma
EXPOSE $APP_PORT
And this is my docker-compose.yml
file:
version: '3.7'
services:
take-report:
image: take-report:v1
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
args:
- DATABASE_URL
ports:
- ${APP_EXPOSED_PORT}:$APP_PORT
env_file:
- .env
networks:
- take-report
- traefik_default
labels:
- "traefik.enable=true"
command: npm run start:prisma:prod
And in my package.json
I have these scripts:
"start:prisma:prod": "npm run prisma:dev && node dist/src/main",
"prisma:dev": "npx prisma generate && npx prisma deploy && npx prisma db seed",
But my problem with this solution is that I have to run npm prune --production
after my seed completed. Do you have any idea to improve this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.