diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..882d1c9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +docker +.dockerignore +node_modules +npm-debug.log +dist \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index fe71525..77b6275 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,33 +1,55 @@ -# Stage 1: Build the app -FROM node:18 AS builder +# Stage 1: development +FROM node:20 AS development +RUN apt-get update && apt-get install -y openssl + +# create app directory WORKDIR /app # copy package.json and package-lock.json -COPY package*.json ./ +COPY --chown=node:node package*.json ./ # install dependencies -RUN npm install +RUN npm ci # copy source code -COPY . . +COPY --chown=node:node . . -# build the app -RUN npm run build +# Generate Prisma client +RUN npm run prisma:generate + +# Use the node user instead of root +USER node #------------------------------------------------------------ -# Stage 2: Run the app -FROM node:18-slim +# Stage 2: build for production +FROM node:20-alpine AS builder WORKDIR /app -# copy package.json and package-lock.json -COPY package*.json ./ +COPY --chown=node:node package*.json ./ + +# The build process needs access to NestJS CLI, which is a dev dependency. +COPY --chown=node:node --from=development /app/node_modules ./node_modules + +COPY --chown=node:node . . + +RUN npm run build + +ENV NODE_ENV=production + +# Install production dependencies overriding the previously copied node_modules: +RUN npm ci --only=production && \ + npm cache clean --force + +USER node -RUN npm install --only=production +# ----------------------------------------------------------- +# Stage 3: production +FROM node:20-alpine AS production -# copy build from stage 1 -COPY --from=builder /app/dist ./dist +COPY --chown=node:node --from=builder /app/node_modules ./node_modules +COPY --chown=node:node --from=builder /app/dist ./dist # expose port 3000 EXPOSE 3000 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..2b7dc8a --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,33 @@ +services: + api: + build: + dockerfile: Dockerfile + context: . + target: development + volumes: + - .:/app + env_file: + - .env + environment: + BASE_URL: https://${HOSTNAME}:${PORT} + DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:${DB_PORT}/${DB_NAME}?schema=public + command: npm run start:dev + ports: + - 3000:3000 + depends_on: + - postgres + + postgres: + image: postgres:16 + restart: always + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_NAME} + ports: + - 5432:5432 + volumes: + - postgres:/var/lib/postgresql/data + +volumes: + postgres: diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 39336e0..0f84fd4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -4,6 +4,9 @@ generator client { provider = "prisma-client-js" previewFeatures = ["metrics", "postgresqlExtensions"] + // binary targets are optional, omitting them will default to ["native"] + // the targets next to native, are for alpine linux and arm64 (Apple M1) respectively + binaryTargets = ["native", "linux-musl", "linux-arm64-openssl-3.0.x"] } generator erd {