-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
125 lines (106 loc) · 3.91 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# ADOPTED FROM: https://github.com/Pocket/pocket-monorepo/tree/main
# Adapted from https://turbo.build/repo/docs/handbook/deploying-with-docker
# and https://github.com/vercel/turbo/issues/5462#issuecomment-1624792583
#----------------------------------------
# Docker build step that creates our
# base image used in all steps
#----------------------------------------
FROM node:20.12-alpine AS base
ARG SCOPE
ARG APP_PATH
ARG PORT
ARG GIT_SHA
ARG SENTRY_ORG
ARG SENTRY_PROJECT
## Add curl for health checks
RUN apk add --no-cache curl
## Add turbo and pnpm to all followup builder images
# Dockerfile
RUN corepack enable && corepack prepare [email protected] --activate
# Enable `pnpm add --global` on Alpine Linux by setting
# home location environment variable to a location already in $PATH
# https://github.com/pnpm/pnpm/issues/784#issuecomment-1518582235
ENV PNPM_HOME=/usr/local/bin
RUN pnpm add -g [email protected]
#----------------------------------------
# Docker build step that prunes down to
# the active project.
#----------------------------------------
FROM base AS setup
ARG SCOPE
ARG APP_PATH
ARG PORT
ARG GIT_SHA
ARG SENTRY_ORG
ARG SENTRY_PROJECT
RUN apk add --no-cache curl
RUN apk update
# Set working directory
WORKDIR /app
COPY . .
# Prune the structure to an optimized folder structure with just the `scopes` app dependencies.
RUN turbo prune --scope=$SCOPE --docker
#----------------------------------------
# Docker build step that:
# 1. Installs all the dependencies
# 2. Builds the application
# 3. Exports it as a built application
#----------------------------------------
# Add lockfile and package.json's of isolated subworkspace
FROM base AS builder
ARG SCOPE
ARG APP_PATH
ARG PORT
ARG GIT_SHA
ARG SENTRY_ORG
ARG SENTRY_PROJECT
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app
# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=setup /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=setup /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
# First install dependencies (as they change less often)
COPY --from=setup /app/out/json/ ./
RUN pnpm install --filter=${SCOPE}... --frozen-lockfile
# Build the project and its dependencies
COPY --from=setup /app/out/full/ ./
COPY turbo.json turbo.json
RUN pnpm run build --filter=${SCOPE}...
# Special handling for prisma node_modules
# This is a temporary hack, hopefully
RUN cp -r ${APP_PATH}/node_modules/.prisma ./.prisma.tmp | true
## Installing only the dev dependencies after we used them to build
RUN rm -rf node_modules/ && pnpm install --prod --filter=${SCOPE}... --frozen-lockfile
# Inject sentry source maps
RUN pnpm --filter=$SCOPE --prod deploy pruned
RUN pnpx @sentry/cli sourcemaps inject pruned/dist
RUN mv ./.prisma.tmp pruned/node_modules/.prisma | true
# If sentry project was passed, upload the source maps
RUN --mount=type=secret,id=sentry_token \
if [ -n "$SENTRY_PROJECT" ] ; then pnpx @sentry/cli sourcemaps upload pruned/dist --release ${GIT_SHA} --auth-token $(cat /run/secrets/sentry_token) --org ${SENTRY_ORG} --project ${SENTRY_PROJECT} ; fi
#----------------------------------------
# Docker build step that:
# 1. Sets up our actual runner
#----------------------------------------
FROM base AS runners
ARG PORT
ARG GIT_SHA
WORKDIR /app
COPY --from=builder /app/pruned/ ./
# Bug in PNPM that is not grabbing all the deps with a deploy, so we need to copy it all
# https://github.com/pnpm/pnpm/issues/6259
COPY --from=builder /app/node_modules/ ./node_modules/
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
RUN chown -R nodejs:nodejs /app
USER nodejs
ENV NODE_ENV=production
ENV PORT=${PORT}
ENV GIT_SHA=${GIT_SHA}
ENV RELEASE_SHA=${GIT_SHA}
EXPOSE $PORT
CMD [ "npm", "run", "start" ]