-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile.nest.production
76 lines (58 loc) · 2.23 KB
/
Dockerfile.nest.production
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
# syntax=docker/dockerfile:1
ARG NODE_VERSION=21.1.0
ARG PNPM_VERSION=9.15.2
#-------------------------------------------------------------------------------
# Base stage: Set up the common environment
#-------------------------------------------------------------------------------
FROM node:${NODE_VERSION}-slim AS base
WORKDIR /app
# Install pnpm.
RUN npm install -g pnpm@${PNPM_VERSION}
# Install turbo.
RUN npm install -g turbo
#-------------------------------------------------------------------------------
# Pruner stage: Prune the workspace for the specified application
#-------------------------------------------------------------------------------
FROM base AS pruner
ARG WORKSPACE_APP
WORKDIR /app
# Copy all files to the container
COPY . .
# Prune the workspace for the specified WORKSPACE_APP
RUN turbo prune --scope=${WORKSPACE_APP} --docker
#-------------------------------------------------------------------------------
# Builder stage: Build the specified application and optimize for production
#-------------------------------------------------------------------------------
FROM base AS builder
ARG WORKSPACE_APP
WORKDIR /app
# Copy pruned files and install dependencies
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=pruner /app/out/json/ .
# Install dependencies
RUN pnpm install --frozen-lockfile
# Copy source and build the specified application
COPY --from=pruner /app/out/full/ .
RUN pnpm turbo run build --filter=${WORKSPACE_APP}
# Optimize for production
RUN pnpm install --prod
#-------------------------------------------------------------------------------
# Runner stage: Set up the environment and run the application
#-------------------------------------------------------------------------------
FROM base AS runner
ARG WORKSPACE_APP
# Set up non-root user for security
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs
WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app .
# Navigate to the WORKSPACE_APP directory
WORKDIR /app/apps/${WORKSPACE_APP}
# Set environment variables
ARG PORT=8080
ENV PORT=${PORT}
ENV NODE_ENV="production"
EXPOSE ${PORT}
CMD [ "pnpm", "run", "start:prod" ]