notiz-dev / nestjs-prisma-docker

Dockerizing a NestJS app with Prisma and PostgreSQL
https://notiz.dev/blog/dockerizing-nestjs-with-prisma-and-postgresql
93 stars 28 forks source link

No seed part in dockerfile #12

Open kasir-barati opened 2 years ago

kasir-barati commented 2 years ago

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?

Furkan-Gulsen commented 2 years ago

image I get this error when I apply your management. How can I solve it? @kasir-barati

kasir-barati commented 2 years ago

@Furkan-Gulsen It is elementary, Did you have the prisma installed as a dev Dependency? if not do this npm i -D prisma. Then it should work. The other options is to install prisma globally before the RUN npx prisma generate. But please note that I used to use a custom images. My base.Dockerfile for the take-report:dep is this:

# Just because of too many socket timeout and bottleneck while installing 3rd party packages I did this.

FROM node:16.14.0-alpine3.15

WORKDIR /app
# When using COPY with more than one source file, the destination must be a directory and end with a /
COPY package*.json ./
RUN NODE_ENV=development npm ci

And eventually I docker build . -f base.Dockerfile -t take-report:dep. I hope this helps you