Bird
Raised Fist0
Dockerdevops~5 mins

Copying from build stage to final stage in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When building Docker images, you often want to keep the final image small by copying only the necessary files from a larger build environment. This technique uses multiple stages in a Dockerfile to copy files from a build stage to the final stage, reducing image size and improving efficiency.
When you compile code in one stage but only need the compiled output in the final image.
When you want to install build tools temporarily and exclude them from the final image.
When you want to separate dependencies needed for building from those needed for running.
When you want to reduce the size of your Docker image for faster downloads and deployments.
When you want to keep your final image clean and secure by excluding unnecessary files.
Config File - Dockerfile
Dockerfile
FROM node:18 AS build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:1.23 AS final-stage
COPY --from=build-stage /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

The FROM node:18 AS build-stage starts the build stage where the app is built.

The WORKDIR sets the working directory inside the container.

COPY and RUN commands install dependencies and build the app.

The second stage FROM nginx:1.23 AS final-stage uses a lightweight web server image.

COPY --from=build-stage copies only the built files from the build stage to the final image.

This keeps the final image small and clean.

Commands
Builds the Docker image using the Dockerfile in the current directory. It runs both stages and creates the final image with the built app copied from the build stage.
Terminal
docker build -t my-app:latest .
Expected OutputExpected
[+] Building 12.3s (10/10) FINISHED => [build-stage 1/7] FROM docker.io/library/node:18 0.0s => CACHED [build-stage 2/7] WORKDIR /app 0.0s => [build-stage 3/7] COPY package*.json ./ 0.1s => [build-stage 4/7] RUN npm install 8.5s => [build-stage 5/7] COPY . . 0.1s => [build-stage 6/7] RUN npm run build 3.2s => [final-stage 7/10] FROM docker.io/library/nginx:1.23 0.0s => [final-stage 8/10] COPY --from=build-stage /app/build /usr/share/nginx/html 0.1s => [final-stage 9/10] EXPOSE 80 0.0s => [final-stage 10/10] CMD ["nginx", "-g", "daemon off;"] 0.0s => exporting to image 0.1s => => exporting layers 0.1s => => writing image sha256:abcdef1234567890 0.0s => => naming to docker.io/library/my-app:latest
Runs the built Docker image in detached mode, mapping port 8080 on the host to port 80 in the container so you can access the app in a browser.
Terminal
docker run -d -p 8080:80 my-app:latest
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
→
-d - Run container in detached mode (in background)
→
-p 8080:80 - Map host port 8080 to container port 80
Fetches the homepage from the running container to verify the app is served correctly from the final image.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>My App</title> </head> <body> <h1>Welcome to My App</h1> </body> </html>
Lists the Docker images named 'my-app' to check the image size and confirm the build was successful.
Terminal
docker images my-app
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app latest abcdef123456 10 seconds ago 45MB
Key Concept

If you remember nothing else from this pattern, remember: multi-stage builds let you copy only the needed files from a build stage to keep your final Docker image small and efficient.

Common Mistakes
Not using the --from flag in COPY to specify the build stage.
Docker will look for files in the current stage, causing a build error because the files don't exist there.
Use COPY --from=build-stage /path/in/build /path/in/final to copy files from the build stage.
Copying the entire build directory instead of just the output files.
This makes the final image unnecessarily large and may include sensitive or unnecessary files.
Copy only the build output folder or files needed to run the app.
Not naming the build stage with AS, so you can't reference it in COPY.
Without a stage name, you cannot use --from to copy files from that stage.
Name your build stage with AS build-stage (or any name) to reference it later.
Summary
Use multi-stage Dockerfiles to separate build and final stages.
Copy only the necessary build output from the build stage to the final stage using COPY --from.
This approach keeps the final image small and efficient for deployment.

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