Skip to content

Commit 47781d2

Browse files
heiskrCopilot
andauthored
Optimize Dockerfile with granular stages for better parallelization (#56191)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d62b00f commit 47781d2

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

Dockerfile

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,28 @@ RUN --mount=type=secret,id=DOCS_BOT_PAT_BASE,mode=0444 \
5454
. ./build-scripts/fetch-repos.sh
5555

5656
# -----------------------------------------
57-
# DEPENDENCIES STAGE: Install node packages
57+
# PROD_DEPS STAGE: Install production dependencies
5858
# -----------------------------------------
59-
FROM base AS dependencies
59+
FROM base AS prod_deps
6060
USER node:node
6161
WORKDIR $APP_HOME
6262

6363
# Copy what is needed to run npm ci
6464
COPY --chown=node:node package.json package-lock.json ./
6565

66-
RUN npm ci --omit=optional --registry https://registry.npmjs.org/
66+
# Install only production dependencies (skip scripts to avoid husky)
67+
RUN npm ci --omit=dev --ignore-scripts --registry https://registry.npmjs.org/
6768

6869
# -----------------------------------------
69-
# BUILD STAGE: Prepare for production stage
70+
# ALL_DEPS STAGE: Install all dependencies on top of prod deps
71+
# -----------------------------------------
72+
FROM prod_deps AS all_deps
73+
74+
# Install dev dependencies on top of production ones
75+
RUN npm ci --registry https://registry.npmjs.org/
76+
77+
# -----------------------------------------
78+
# BUILD STAGE: Build the application
7079
# -----------------------------------------
7180
FROM base AS build
7281
USER node:node
@@ -84,14 +93,27 @@ COPY --chown=node:node --from=clones $APP_HOME/assets assets/
8493
COPY --chown=node:node --from=clones $APP_HOME/content content/
8594
COPY --chown=node:node --from=clones $APP_HOME/translations translations/
8695

87-
# From the dependencies stage
88-
COPY --chown=node:node --from=dependencies $APP_HOME/node_modules node_modules/
96+
# From the all_deps stage (need dev deps for build)
97+
COPY --chown=node:node --from=all_deps $APP_HOME/node_modules node_modules/
98+
99+
# Build the application
100+
RUN npm run build
101+
102+
# -----------------------------------------
103+
# WARMUP_CACHE STAGE: Warm up remote JSON cache
104+
# -----------------------------------------
105+
FROM build AS warmup_cache
106+
107+
# Generate remote JSON cache
108+
RUN npm run warmup-remotejson
89109

90-
# Generate build files
91-
RUN npm run build \
92-
&& npm run warmup-remotejson \
93-
&& npm run precompute-pageinfo -- --max-versions 2 \
94-
&& npm prune --production
110+
# -----------------------------------------
111+
# PRECOMPUTE STAGE: Precompute page info
112+
# -----------------------------------------
113+
FROM build AS precompute_stage
114+
115+
# Generate precomputed page info
116+
RUN npm run precompute-pageinfo -- --max-versions 2
95117

96118
# -------------------------------------------------
97119
# PRODUCTION STAGE: What will run on the containers
@@ -112,13 +134,17 @@ COPY --chown=node:node --from=clones $APP_HOME/assets assets/
112134
COPY --chown=node:node --from=clones $APP_HOME/content content/
113135
COPY --chown=node:node --from=clones $APP_HOME/translations translations/
114136

115-
# From dependencies stage (*modified in build stage)
116-
COPY --chown=node:node --from=build $APP_HOME/node_modules node_modules/
137+
# From prod_deps stage (production-only node_modules)
138+
COPY --chown=node:node --from=prod_deps $APP_HOME/node_modules node_modules/
117139

118140
# From build stage
119141
COPY --chown=node:node --from=build $APP_HOME/.next .next/
120-
COPY --chown=node:node --from=build $APP_HOME/.remotejson-cache ./
121-
COPY --chown=node:node --from=build $APP_HOME/.pageinfo-cache.json.br* ./
142+
143+
# From warmup_cache stage
144+
COPY --chown=node:node --from=warmup_cache $APP_HOME/.remotejson-cache ./
145+
146+
# From precompute_stage
147+
COPY --chown=node:node --from=precompute_stage $APP_HOME/.pageinfo-cache.json.br* ./
122148

123149
# This makes it possible to set `--build-arg BUILD_SHA=abc123`
124150
# and it then becomes available as an environment variable in the docker run.

0 commit comments

Comments
 (0)