-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
92 lines (70 loc) · 2.17 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
# 🧑💻 Development
#
FROM node:18-alpine as dev
# add the missing shared libraries from alpine base image
RUN apk add --no-cache libc6-compat
# Create app folder
WORKDIR /app
# Set to dev environment
ENV NODE_ENV dev
# Set the Timezone
ENV TZ=Europe/Paris
# Copy source code into app folder
COPY --chown=node:node . .
# Install dependencies
RUN npm i
# Generate the prisma client
RUN npx prisma generate
# Set Docker as a non-root user
USER node
#
# 🏡 Production Build
#
FROM node:18-alpine as build
WORKDIR /app
RUN apk add --no-cache libc6-compat
# Set to production environment
ENV NODE_ENV production
# Set the Timezone
ENV TZ=Europe/Paris
# In order to run the Nest CLI, we need to install the dev dependencies.
# Nest CLI is a dev dependency.
COPY --chown=node:node --from=dev /app/node_modules ./node_modules
# Copy source code
COPY --chown=node:node . .
# Run the prisma generate
RUN npx prisma generate
# Generate the production build. The build script runs "nest build" to compile the application.
RUN npm run build
# Install only the production dependencies and clean cache to optimize image size.
RUN npm i --only=production && npm cache clean --force
# Set Docker as a non-root user
USER node
#
# 🚀 Production Server
#
FROM node:18-alpine as prod
WORKDIR /app
RUN apk add --no-cache libc6-compat
# Set to production environment
ENV NODE_ENV production
# Set the Timezone
ENV TZ=Europe/Paris
# Install PM2
RUN npm install -g pm2
# Copy only the necessary files
COPY --chown=node:node --from=build /app/dist dist
COPY --chown=node:node --from=build /app/node_modules node_modules
# Copy the prisma folder (schema + migrations)
COPY --chown=node:node --from=build /app/prisma prisma
# Create a the logs folder
RUN mkdir -p /app/logs
# Give the logs folder the right permissions (read, write, execute) for the node user
RUN chown -R node:node /app/logs
# Expose the port
EXPOSE 3000
# Set Docker as non-root user
USER node
# Push the prisma schema if possible and Run the app with PM2
CMD ["sh", "-c","npx prisma migrate deploy && pm2-runtime start dist/main.js --name nest-app"]