Bird
Raised Fist0
Dockerdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using COPY --from= in a multi-stage Docker build?
easy
A. To copy files from the host machine to the build stage
B. To copy files from a previous build stage to the final image
C. To copy files from the final image back to the build stage
D. To copy files between two running containers

Solution

  1. Step 1: Understand multi-stage builds

    Multi-stage builds allow you to create intermediate stages for building your app.
  2. Step 2: Purpose of COPY --from=

    The COPY --from= command copies files from these intermediate stages into the final image, keeping it clean and small.
  3. Final Answer:

    To copy files from a previous build stage to the final image -> Option B
  4. Quick Check:

    COPY --from= copies from build stage [OK]
Hint: COPY --from= copies from build stage, not host or containers [OK]
Common Mistakes:
  • Confusing COPY --from= with copying from host
  • Thinking it copies files between containers
  • Assuming it copies files back to build stage
2. Which of the following is the correct syntax to copy the file /app/build/output.bin from a build stage named builder to the final image's /app directory?
easy
A. COPY --from=builder /app/build/output.bin /app
B. COPY /app/build/output.bin /app --from=builder
C. COPY builder:/app/build/output.bin /app
D. COPY /app/build/output.bin builder:/app

Solution

  1. Step 1: Recall COPY --from= syntax

    The correct syntax is COPY --from=stage source_path destination_path.
  2. Step 2: Match the paths and stage name

    COPY --from=builder /app/build/output.bin /app correctly uses --from=builder followed by source and destination paths.
  3. Final Answer:

    COPY --from=builder /app/build/output.bin /app -> Option A
  4. Quick Check:

    Correct COPY --from= syntax is COPY --from=stage src dst [OK]
Hint: COPY --from= comes before source and destination paths [OK]
Common Mistakes:
  • Placing --from= after source and destination
  • Using colon syntax like builder:/path (invalid)
  • Swapping source and destination paths
3. Given this Dockerfile snippet, what files will be present in the final image's /app directory?
FROM node:18 AS builder
WORKDIR /app
RUN echo "build file" > build.txt

FROM alpine
COPY --from=builder /app/build.txt /app/
medium
A. No /app directory created
B. An empty /app directory
C. The file build.txt with content "build file"
D. The file build.txt but empty

Solution

  1. Step 1: Check build stage output

    The builder stage creates /app/build.txt with content "build file".
  2. Step 2: COPY from builder to final image

    The final stage copies /app/build.txt from builder to /app/ in the final image.
  3. Final Answer:

    The file build.txt with content "build file" -> Option C
  4. Quick Check:

    File copied with content preserved [OK]
Hint: COPY --from= copies actual files, not empty placeholders [OK]
Common Mistakes:
  • Assuming directory is empty after copy
  • Thinking file content is lost
  • Assuming /app directory is missing
4. Identify the error in this Dockerfile snippet that tries to copy from a build stage named builder:
FROM golang:1.20 AS builder
WORKDIR /app
RUN go build -o myapp

FROM alpine
COPY /app/myapp --from=builder /usr/local/bin/myapp
medium
A. The --from=builder flag is placed after the source path
B. The build stage name builder is missing
C. The destination path /usr/local/bin/myapp is invalid
D. The RUN go build command is incorrect

Solution

  1. Step 1: Check COPY command syntax

    The correct syntax is COPY --from=builder source destination. The flag must come before paths.
  2. Step 2: Identify error in given command

    The command places --from=builder after the source path, which is invalid syntax.
  3. Final Answer:

    The --from=builder flag is placed after the source path -> Option A
  4. Quick Check:

    --from= must come before source path [OK]
Hint: Place --from= before source path in COPY command [OK]
Common Mistakes:
  • Putting --from= after source path
  • Forgetting to name the build stage
  • Assuming destination path is invalid without checking
5. You want to build a Docker image for a Python app. The build stage installs dependencies and creates a dist/app.tar.gz file. The final stage should only copy this archive and extract it to /app. Which Dockerfile snippet correctly copies and extracts the archive from the build stage named builder?
hard
A. FROM python:3.12 AS builder\nWORKDIR /src\nRUN pip install -r requirements.txt\nRUN tar czf dist/app.tar.gz .\n\nFROM python:3.12-slim\nWORKDIR /app\nCOPY --from=builder /src/dist/app.tar.gz /app/\nRUN tar xzf /app/app.tar.gz
B. FROM python:3.12 AS builder\nWORKDIR /src\nRUN pip install -r requirements.txt\nRUN tar czf dist/app.tar.gz .\n\nFROM python:3.12-slim\nWORKDIR /app\nCOPY /src/dist/app.tar.gz --from=builder ./\nRUN tar xzf app.tar.gz
C. FROM python:3.12 AS builder\nWORKDIR /src\nRUN pip install -r requirements.txt\nRUN tar czf dist/app.tar.gz .\n\nFROM python:3.12-slim\nWORKDIR /app\nCOPY --from=builder /dist/app.tar.gz ./\nRUN tar xzf app.tar.gz
D. FROM python:3.12 AS builder\nWORKDIR /src\nRUN pip install -r requirements.txt\nRUN tar czf dist/app.tar.gz .\n\nFROM python:3.12-slim\nWORKDIR /app\nCOPY --from=builder /src/dist/app.tar.gz ./\nRUN tar xzf app.tar.gz

Solution

  1. Step 1: Verify COPY --from= syntax and source path

    The builder stage (WORKDIR /src) creates dist/app.tar.gz at /src/dist/app.tar.gz. Correctly uses COPY --from=builder /src/dist/app.tar.gz ./ with --from=builder first and absolute source path.
  2. Step 2: Confirm extraction command and paths

    Final stage WORKDIR /app, copy to ./ places app.tar.gz there. RUN tar xzf app.tar.gz extracts correctly.
  3. Final Answer:

    COPY --from=builder /src/dist/app.tar.gz ./\nRUN tar xzf app.tar.gz -> Option D
  4. Quick Check:

    COPY --from= before source path, extract in correct directory [OK]
Hint: COPY --from= before source path; extract where file copied [OK]
Common Mistakes:
  • Placing --from= after source path
  • Using wrong source path like /dist/app.tar.gz instead of /src/dist/app.tar.gz
  • Extracting archive from wrong directory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard