0
0
Dockerdevops~10 mins

Copying from build stage to final stage in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Copying from build stage to final stage
Start: Multi-stage Dockerfile
Build Stage: Compile or build app
Copy artifacts from build stage
Final Stage: Use copied artifacts
Build final image
Run container with final image
This flow shows how Docker builds an image in stages, copying files from the build stage to the final stage to keep the final image small.
Execution Sample
Docker
FROM node:18 AS build
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
This Dockerfile builds a Node.js app in the build stage, then copies the build output to an Nginx image in the final stage.
Process Table
StepActionStageFiles PresentResult
1Start build stagebuildemptyWorking directory /app created
2Copy package.jsonbuildpackage.jsonpackage.json copied to /app
3Run npm installbuildnode_modules createdDependencies installed
4Copy all filesbuildall source filesSource code copied to /app
5Run npm run buildbuildbuild folder createdApp compiled into /app/build
6Start final stagefinalemptyBase nginx image ready
7Copy from build stagefinal/usr/share/nginx/htmlCopied /app/build to nginx html folder
8Build final imagefinal/usr/share/nginx/html with build filesFinal image ready with static files
9Run containerfinalrunning containerNginx serves the built app
10Exit--Process complete
💡 All build steps completed and final image contains only necessary files from build stage
Status Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
/app contentsemptypackage.json + node_modulespackage.json + node_modules + build folderemptyempty
/usr/share/nginx/html contentsemptyemptyemptybuild folder copied herebuild folder present
Key Moments - 3 Insights
Why is the build folder empty in the final stage before copying?
Because each stage has its own filesystem, files created in the build stage are not visible in the final stage until explicitly copied (see step 6 and 7 in execution_table).
What does the --from=build option do in the COPY command?
It tells Docker to copy files from the build stage named 'build' into the current stage (final), as shown in step 7.
Why do we use multi-stage builds instead of one big stage?
To keep the final image small by only copying necessary files, avoiding development dependencies and build tools in the final image (see final image contents in step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the build folder created?
AStep 5
BStep 3
CStep 7
DStep 2
💡 Hint
Check the 'Files Present' column for the build folder creation in the build stage
At which step does the final stage start copying files from the build stage?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look for the COPY --from=build action in the execution_table
If we remove the COPY --from=build command, what happens to the final image?
AIt will contain the build folder anyway
BIt will be empty and not serve the app
CIt will fail to build
DIt will copy files from the base image
💡 Hint
Refer to step 7 and 8 where copying build files is essential for final image content
Concept Snapshot
Multi-stage Docker builds use separate stages.
Build stage compiles or prepares files.
Final stage copies only needed files with COPY --from=build.
This keeps final images small and clean.
Use named stages for clarity.
Full Transcript
This visual execution shows how Docker multi-stage builds work. First, the build stage creates the app build files. Then the final stage starts fresh and copies only the build output from the build stage. This keeps the final image small by excluding build tools and source files. The COPY --from=build command is key to transfer files between stages. Each stage has its own filesystem, so files are not shared unless copied. This method is common for building and packaging apps efficiently in Docker.