0
0
Dockerdevops~10 mins

Multiple FROM statements in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multiple FROM statements
Start Dockerfile
FROM base image 1
Build stage 1
FROM base image 2
Build stage 2
Final image created
Use artifacts from previous stages if needed
End Dockerfile
Dockerfile starts with first FROM to create stage 1, then second FROM starts stage 2, allowing multi-stage builds and artifact sharing.
Execution Sample
Docker
FROM python:3.12-slim AS builder
RUN pip install --no-cache-dir flask
FROM python:3.12-slim
COPY --from=builder /usr/local/lib/python3.12/site-packages /app/libs
CMD ["python3"]
This Dockerfile uses two FROM statements to build dependencies in one stage and copy them into a clean final image.
Process Table
StepActionStageResult
1Start DockerfileNoneReady to read first FROM
2FROM python:3.12-slim AS builderbuilderBase image python:3.12-slim loaded as builder stage
3RUN pip install --no-cache-dir flaskbuilderFlask installed in builder stage
4FROM python:3.12-slimfinalNew stage 'final' starts with clean python:3.12-slim image
5COPY --from=builder /usr/local/lib/python3.12/site-packages /app/libsfinalCopied flask libs from builder stage to final image
6CMD ["python3"]finalSet command to run python3 in final image
7Build completefinalFinal image created with flask libs from builder stage
💡 Dockerfile ends after last command; final image ready with multi-stage build artifacts
Status Tracker
VariableStartAfter Step 3After Step 5Final
builder imageemptypython:3.12-slim + flask installedunchangedunchanged
final imageemptyemptypython:3.12-slim + flask libs copiedready to run python3
Key Moments - 2 Insights
Why do we start a new stage with a second FROM instead of continuing the first?
Each FROM starts a new build stage with a clean base image. This allows separating build dependencies from the final image, as shown in steps 2 and 4 of the execution_table.
How does COPY --from=builder work to share files between stages?
COPY --from=builder copies files from the named previous stage 'builder' into the current stage. Step 5 shows copying flask libraries from builder to final image.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the final image start building?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Stage' column to see when 'final' stage begins.
According to variable_tracker, what is the state of the builder image after step 3?
ABase image plus flask installed
BOnly base image loaded
CEmpty
DFinal image ready
💡 Hint
Look at 'builder image' row after 'After Step 3' column.
If we remove the COPY --from=builder command, what happens to the final image?
AIt still has flask installed
BIt will be empty
CIt will not have flask libraries
DIt will fail to build
💡 Hint
Refer to step 5 in execution_table where COPY copies flask libs to final image.
Concept Snapshot
Multiple FROM statements allow multi-stage Docker builds.
Each FROM starts a new stage with a clean base image.
Use AS to name stages for reference.
COPY --from=stage copies files between stages.
This reduces final image size by excluding build tools.
Full Transcript
This visual execution shows how Dockerfiles with multiple FROM statements work. The Dockerfile starts with a base image named 'builder' where dependencies like flask are installed. Then a second FROM starts a clean 'final' stage. Files are copied from the builder stage to the final stage using COPY --from=builder. This creates a smaller final image with only needed files. The execution table traces each step, showing when stages start and what actions happen. The variable tracker shows how the builder and final images change over time. Key moments clarify why new stages start with FROM and how files are shared. The quiz tests understanding of stage transitions and file copying. This method helps keep Docker images efficient and clean.