0
0
Dockerdevops~3 mins

Why Multiple FROM statements in Docker? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could build your app in one place but run it in a completely different, lightweight environment?

The Scenario

Imagine you want to build a software image that needs two different environments: one to compile your code and another to run it. You try to install everything in one big image manually.

The Problem

This manual way makes your image huge, slow to build, and hard to maintain. You waste time downloading unnecessary tools and risk mixing incompatible software.

The Solution

Using multiple FROM statements lets you create separate stages in your Dockerfile. You build your code in one stage, then copy only the needed parts to a smaller final image. This keeps things clean and efficient.

Before vs After
Before
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y build-essential python3
COPY . /app
RUN make /app
After
FROM build-image AS builder
COPY . /app
RUN make /app
FROM runtime-image
COPY --from=builder /app/bin /app/bin
What It Enables

You can create smaller, faster, and more secure images by separating build and runtime environments cleanly.

Real Life Example

A developer compiles a complex app with many tools, then ships only the final executable in a tiny image for production, saving bandwidth and startup time.

Key Takeaways

Manual single-stage images become large and slow.

Multiple FROM statements create build stages for efficiency.

Final images contain only what is needed to run the app.