0
0
Nginxdevops~10 mins

Multi-stage builds for static sites in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multi-stage builds for static sites
Start: Define build stage
Build static site files
Start: Define final stage
Copy built files from build stage
Configure nginx to serve files
Build final image
Run container serving static site
This flow shows how Docker builds a static site in one stage, then copies only the needed files into a smaller nginx image to serve the site.
Execution Sample
Nginx
FROM node:18 AS build
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
This Dockerfile builds a static site with Node.js, then copies the built files into an nginx image to serve them.
Process Table
StepActionStageFiles PresentImage Size Impact
1Start build stage with node:18buildEmptyBase node image size (~900MB)
2Copy source files to /appbuildSource files copiedNo size change yet
3Run npm installbuildNode modules installedIncreases size by dependencies
4Run npm run buildbuildBuilt static files in /app/distAdds build output files
5Start final stage with nginx:alpinefinalEmptySmall base image (~22MB)
6Copy /app/dist from build stage to nginx html folderfinalStatic files presentAdds only built files, keeps image small
7Build final imagefinalStatic site ready to serveFinal image size small (~30MB)
8Run containerfinalServing static siteContainer runs nginx serving static files
💡 Final image built with only static files and nginx, keeping size small and efficient.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
Files in build stageEmptySource filesSource + node_modules + distN/AN/A
Files in final stageEmptyEmptyEmptyCopied dist filesStatic site files only
Image size~900MB base~900MB~950MB~22MB base~30MB final
Key Moments - 3 Insights
Why do we use two FROM lines in the Dockerfile?
Each FROM starts a new stage. The first builds the site, the second copies only the needed files to keep the final image small, as shown in steps 1 and 5 of the execution_table.
What happens if we copy all files instead of just /app/dist in step 6?
The final image would include unnecessary source and node_modules files, making it much larger. The execution_table step 6 shows only copying /app/dist keeps the image small.
Why is the final image size much smaller than the build stage?
Because the final stage uses a lightweight nginx base and copies only the built static files, avoiding large build tools and dependencies present in the build stage, as tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what files are present in the build stage?
ASource files only
BSource files, node_modules, and built static files
CSource files and node_modules only
DEmpty
💡 Hint
Check the 'Files Present' column at step 4 in execution_table.
At which step does the final image start to include the static site files?
AStep 6
BStep 3
CStep 2
DStep 1
💡 Hint
Look for when files are copied into the final stage in execution_table.
If we skipped the build stage and copied source files directly to nginx, what would happen?
AThe site would serve correctly with smaller image size
BThe image size would be larger than build stage
CThe site would fail because static files are not built
DNginx would automatically build the site
💡 Hint
Refer to the purpose of the build stage in concept_flow and execution_table steps 3-4.
Concept Snapshot
Multi-stage builds use multiple FROM lines in Dockerfile.
First stage builds static files with tools (Node.js).
Second stage uses lightweight nginx image.
Copy only built files from build stage to final.
Keeps final image small and efficient.
Run container to serve static site with nginx.
Full Transcript
Multi-stage builds for static sites use Docker to separate building and serving. First, a build stage uses a Node.js image to install dependencies and build the static site files. Then, a final stage uses a small nginx image and copies only the built static files from the build stage. This keeps the final image small and efficient. The Dockerfile has two FROM lines, each starting a stage. The build stage creates the files, and the final stage serves them. This method avoids including unnecessary build tools and source files in the final image. The execution table shows each step: starting build, copying files, running build commands, starting final stage, copying built files, and running the container. The variable tracker shows how files and image size change. Key moments clarify why two stages are used, why only built files are copied, and why the final image is smaller. The quiz tests understanding of file presence, copying step, and consequences of skipping build. This approach is common for static sites to optimize Docker images.