What if you could build your app in one place but run it in a completely different, lightweight environment?
Why Multiple FROM statements in Docker? - Purpose & Use Cases
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.
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.
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.
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y build-essential python3
COPY . /app
RUN make /appFROM build-image AS builder
COPY . /app
RUN make /app
FROM runtime-image
COPY --from=builder /app/bin /app/binYou can create smaller, faster, and more secure images by separating build and runtime environments cleanly.
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.
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.