Bird
Raised Fist0
Dockerdevops~10 mins

Named build stages in Docker - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the Dockerfile to name the first build stage as 'builder'.

Docker
FROM node:18 AS [1]
WORKDIR /app
COPY package.json ./
RUN npm install
Drag options to blanks, or click blank then click option'
Abuilder
Bstage1
Cbuild
Dbase
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'build' or 'base' instead of 'builder' which is the conventional name.
Omitting the AS keyword and stage name.
2fill in blank
medium

Complete the Dockerfile to copy files from the named build stage 'builder'.

Docker
FROM nginx:alpine
COPY --from=[1] /app/build /usr/share/nginx/html
Drag options to blanks, or click blank then click option'
Abuild
Bbase
Cstage1
Dbuilder
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'build' or 'base' which are not the correct stage names.
Forgetting to use the --from option.
3fill in blank
hard

Fix the error in the Dockerfile by correctly naming the build stage.

Docker
FROM python:3.12 AS [1]
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
Drag options to blanks, or click blank then click option'
Astage
Bbuild-stage
Cbuilder
Dpython-builder
Attempts:
3 left
💡 Hint
Common Mistakes
Using names with hyphens which can cause issues in some contexts.
Using generic names like 'stage' that are not descriptive.
4fill in blank
hard

Fill both blanks to create a multi-stage Dockerfile that builds and then copies from the 'builder' stage.

Docker
FROM golang:1.20 AS [1]
WORKDIR /src
COPY . .
RUN go build -o app

FROM alpine:latest
COPY --from=[2] /src/app /usr/local/bin/app
Drag options to blanks, or click blank then click option'
Abuilder
Bbuild
Cstage1
Dbase
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the AS and --from parts.
Using generic names like 'build' or 'stage1' inconsistently.
5fill in blank
hard

Fill all three blanks to create a multi-stage Dockerfile with named stages and copy commands.

Docker
FROM rust:1.70 AS [1]
WORKDIR /app
COPY . .
RUN cargo build --release

FROM debian:buster-slim AS [2]
COPY --from=[3] /app/target/release/myapp /usr/local/bin/myapp
Drag options to blanks, or click blank then click option'
Abuilder
Bfinal
Cbuild
Dstage
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up stage names between AS and --from.
Using inconsistent or generic names that don't match.

Practice

(1/5)
1. What is the main purpose of using named build stages in a Dockerfile?
easy
A. To automatically update Docker images
B. To split the build process into parts and reuse files between them
C. To create multiple Dockerfiles in one file
D. To run multiple containers at the same time

Solution

  1. Step 1: Understand what named build stages do

    Named build stages let you divide your Docker build into parts with names, making it easier to manage.
  2. Step 2: Recognize the benefit of reusing files

    You can copy files from one named stage to another, which helps keep images smaller and cleaner.
  3. Final Answer:

    To split the build process into parts and reuse files between them -> Option B
  4. Quick Check:

    Named build stages = split and reuse [OK]
Hint: Named stages let you copy files between build steps [OK]
Common Mistakes:
  • Confusing named stages with running multiple containers
  • Thinking named stages create multiple Dockerfiles
  • Believing named stages update images automatically
2. Which of the following is the correct syntax to name a build stage in a Dockerfile?
easy
A. STAGE node:18 builder
B. BUILD node:18 AS builder
C. FROM node:18 AS builder
D. FROM node:18 WITH builder

Solution

  1. Step 1: Recall Dockerfile syntax for naming stages

    The correct syntax uses the FROM instruction followed by the image and AS plus the stage name.
  2. Step 2: Check each option

    Only FROM node:18 AS builder uses FROM node:18 AS builder, which is correct syntax.
  3. Final Answer:

    FROM node:18 AS builder -> Option C
  4. Quick Check:

    Named stage syntax = FROM ... AS ... [OK]
Hint: Use FROM image AS name to name stages [OK]
Common Mistakes:
  • Using BUILD or STAGE instead of FROM
  • Omitting the AS keyword
  • Using WITH instead of AS
3. Given this Dockerfile snippet, what will be the final image size compared to a single-stage build?
FROM node:18 AS builder
RUN npm install
FROM node:18
COPY --from=builder /app/node_modules /app/node_modules
medium
A. Smaller, because only needed files are copied from builder
B. Larger, because it includes both stages fully
C. Same size, because both stages use the same base image
D. Build will fail due to syntax error

Solution

  1. Step 1: Understand multi-stage build effect on image size

    Multi-stage builds copy only required files from earlier stages, reducing final image size.
  2. Step 2: Analyze the COPY --from usage

    The final stage copies only /app/node_modules from the builder, not the whole builder image.
  3. Final Answer:

    Smaller, because only needed files are copied from builder -> Option A
  4. Quick Check:

    Multi-stage copy reduces image size [OK]
Hint: Copy only needed files from builder stage to reduce size [OK]
Common Mistakes:
  • Assuming both stages are fully included in final image
  • Thinking same base image means same size
  • Confusing syntax causing build failure
4. Identify the error in this Dockerfile snippet using named build stages:
FROM python:3.12 AS build
RUN pip install -r requirements.txt
FROM python:3.12
COPY --from=builder /app /app
medium
A. The stage name 'build' is misspelled as 'builder' in COPY
B. Missing RUN command in second stage
C. Cannot use COPY between stages
D. FROM instruction must be last in Dockerfile

Solution

  1. Step 1: Check stage names consistency

    The first stage is named build, but COPY uses --from=builder, which is incorrect.
  2. Step 2: Understand COPY --from usage

    The --from must match the exact stage name to copy files correctly.
  3. Final Answer:

    The stage name 'build' is misspelled as 'builder' in COPY -> Option A
  4. Quick Check:

    Stage name mismatch causes COPY error [OK]
Hint: Match stage names exactly in COPY --from [OK]
Common Mistakes:
  • Assuming COPY can't copy between stages
  • Ignoring stage name spelling
  • Thinking RUN is required in second stage
5. You want to build a Docker image that compiles a Go app in one stage and produces a small final image with only the binary. Which Dockerfile snippet correctly uses named build stages to achieve this?
hard
A.
FROM alpine:latest
RUN apk add go
WORKDIR /app
COPY . .
RUN go build -o myapp
CMD ["myapp"]
B.
FROM alpine:latest AS builder
RUN apk add go
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM golang:1.20
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
C.
FROM golang:1.20
WORKDIR /app
COPY . .
RUN go build -o myapp
CMD ["myapp"]
D.
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]

Solution

  1. Step 1: Identify multi-stage build for compiling and final image

    FROM golang:1.20 AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o myapp
    FROM alpine:latest
    COPY --from=builder /app/myapp /usr/local/bin/myapp
    CMD ["myapp"]
    uses a builder stage with golang image to compile, then copies only the binary to a small alpine image.
  2. Step 2: Check other options for correctness

    FROM alpine:latest AS builder
    RUN apk add go
    WORKDIR /app
    COPY . .
    RUN go build -o myapp
    FROM golang:1.20
    COPY --from=builder /app/myapp /usr/local/bin/myapp
    CMD ["myapp"]
    installs Go in alpine, which is less efficient. Single-stage builds do not use multi-stage builds, so final image is large.
  3. Final Answer:

    Uses golang:1.20 AS builder to compile and alpine:latest for the final binary-only image -> Option D
  4. Quick Check:

    Multi-stage build with builder and final stage =
    FROM golang:1.20 AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o myapp
    FROM alpine:latest
    COPY --from=builder /app/myapp /usr/local/bin/myapp
    CMD ["myapp"]
    [OK]
Hint: Use builder stage to compile, final stage to copy binary only [OK]
Common Mistakes:
  • Installing build tools in final image
  • Not using multi-stage build
  • Copying entire source instead of just binary
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