Multiple FROM statements in Docker - Time & Space Complexity
We want to understand how the build time changes when using multiple FROM statements in a Dockerfile.
How does adding more stages affect the total work Docker does?
Analyze the time complexity of the following Dockerfile snippet.
FROM node:18 AS builder
WORKDIR /app
RUN npm install
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
This Dockerfile uses two FROM statements to create a multi-stage build: one to build the app and one to serve it.
Look for repeated steps or commands that run multiple times.
- Primary operation: Each stage runs its own set of commands like RUN and COPY.
- How many times: The number of stages equals the number of FROM statements; each stage runs sequentially once.
Each new FROM adds a new stage that runs its commands fully.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 stage | Runs commands once |
| 2 stages | Runs commands twice (once per stage) |
| 5 stages | Runs commands five times (once per stage) |
Pattern observation: The total work grows roughly linearly with the number of FROM stages.
Time Complexity: O(n)
This means the build time grows linearly as you add more FROM stages in your Dockerfile.
[X] Wrong: "Adding more FROM statements does not affect build time much because stages run independently."
[OK] Correct: Each stage runs its commands fully and sequentially, so more stages mean more total work and longer build time.
Understanding how multiple stages affect build time helps you design efficient Dockerfiles and shows you can reason about build processes clearly.
What if we combined all commands into a single stage instead of multiple FROM statements? How would the time complexity change?
Practice
FROM statements in a Dockerfile?Solution
Step 1: Understand the role of
TheFROMin DockerfileFROMstatement sets the base image for building a Docker image.Step 2: Recognize multi-stage build benefits
Using multipleFROMstatements allows building in stages, copying only needed files to the final image, reducing size.Final Answer:
To create multi-stage builds that reduce final image size -> Option AQuick Check:
Multiple FROM = multi-stage build [OK]
- Thinking multiple FROM runs multiple containers
- Confusing FROM with environment variable setting
- Assuming FROM defines runtime commands
builder?Solution
Step 1: Recall the correct COPY syntax for multi-stage
The correct flag to copy from another stage is--from=stage_name.Step 2: Match the option with correct syntax
Only COPY --from=builder /app/build /app uses--from=builder, which is valid syntax.Final Answer:
COPY --from=builder /app/build /app -> Option BQuick Check:
Copy from stage uses --from [OK]
- Using --stage or --source instead of --from
- Misspelling the flag name
- Omitting the equals sign (=) after --from
FROM node:18 AS builder RUN npm install && npm run build FROM nginx:alpine COPY --from=builder /app/build /usr/share/nginx/html
Solution
Step 1: Analyze the multi-stage build steps
The first stage builds the app with Node.js. The second stage uses nginx base image.Step 2: Understand the COPY --from usage
The built files from the builder stage are copied into nginx image, but Node.js layers are not included.Final Answer:
Final image only contains nginx and the built files, keeping it small -> Option CQuick Check:
Multi-stage copy keeps final image small [OK]
- Assuming all build layers stay in final image
- Thinking COPY --from copies the entire image
- Confusing build failure with missing files
FROM python:3.12 AS builder RUN pip install -r requirements.txt FROM alpine COPY --from=build /app /app
Solution
Step 1: Check stage names in FROM statements
The first stage is named 'builder', but COPY uses 'build' which is undefined.Step 2: Understand COPY --from requires exact stage name
Stage names must match exactly; otherwise, Docker throws an error.Final Answer:
The stage name 'build' does not match the defined stage 'builder' -> Option AQuick Check:
Stage names must match exactly in COPY --from [OK]
- Assuming similar names are accepted
- Thinking base image order matters here
- Ignoring COPY destination path correctness
Solution
Step 1: Identify the build stage with Go compiler
Stage named 'builder' uses golang image to compile the app.Step 2: Use a minimal runtime image for final stage
The final stage uses alpine (small image) and copies only the binary from builder.Step 3: Verify COPY --from syntax and CMD
COPY uses --from=builder to get binary; CMD runs the binary correctly.Final Answer:
FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM alpine COPY --from=builder /app/myapp /myapp CMD ["/myapp"] -> Option DQuick Check:
Multi-stage build with builder and minimal final image [OK]
- Using scratch without proper setup
- Copying files without --from flag
- Building in final stage instead of builder
Ready to prove it?
Pass the test and this pattern moves unknown
