<p>First of all, you don't need the COPY instructions or multiple RUN instructions if you have only one stage:</p>
<pre><code>FROM diamol/base AS build-stage
RUN echo 'Building...' &gt; /build.txt &amp;&amp; echo 'Testing...' &gt;&gt; /build.txt
CMD cat /build.txt
</code></pre>
<p>Of course, the textbook example is just that - an example to illustrate the concept.</p>
<p>In reality multistage builds are used to create smaller final images, while keeping the entire build process inside the container.</p>
<p>Generally speaking one may install all development and runtime dependencies in a build stage, then copy just the final distribution package to the final stage, where only runtime dependencies are installed to reduce the final image size.</p>
<p>For example:</p>
<pre><code>FROM ubuntu:latest AS base
RUN apt -y install some-library

FROM base AS build
RUN apt -y install some-library-dev
COPY . /src
WORKDIR /src
RUN make &amp;&amp; make install

FROM base
COPY --from=build /usr/bin/app /usr/bin/app
CMD /usr/bin/app
</code></pre>

CC BY-SA 4.0
2020-08-26T10:44:16.857Z
2020-08-26T10:44:16.857Z
Konrad Botor
<p>I'm new to Docker, sorry if my question sounds dumb.
below is a dockerfile from a textbook shows how multistage build works:</p>
<pre><code>FROM diamol/base AS build-stage                  
RUN echo 'Building...' &gt; /build.txt              

FROM diamol/base AS test-stage
COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' &gt;&gt; /build.txt             

FROM diamol/base
COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt
</code></pre>
<p>My questions is:</p>
<p>Why use multiple From instructions? why we don't do sth like:</p>
<pre><code>FROM diamol/base AS build-stage                  
RUN echo 'Building...' &gt; /build.txt              

COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' &gt;&gt; /build.txt             

COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt
</code></pre>

<docker><dockerfile>
why use multiple From instructions in multistage build?
amjad
# why use multiple From instructions in multistage build?

(Asked by: username_0 on 2020-08-25)

I'm new to Docker, sorry if my question sounds dumb.
below is a dockerfile from a textbook shows how multistage build works:
FROM diamol/base AS build-stage                  
RUN echo 'Building...' > /build.txt              

FROM diamol/base AS test-stage
COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' >> /build.txt             

FROM diamol/base
COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt

My questions is:
Why use multiple From instructions? why we don't do sth like:
FROM diamol/base AS build-stage                  
RUN echo 'Building...' > /build.txt              

COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' >> /build.txt             

COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt

**Tags:** docker, dockerfile


---

## 1 answers

### Answer by username_1 on 2020-08-26. Score: 2

First of all, you don't need the COPY instructions or multiple RUN instructions if you have only one stage:
FROM diamol/base AS build-stage
RUN echo 'Building...' > /build.txt && echo 'Testing...' >> /build.txt
CMD cat /build.txt

Of course, the textbook example is just that - an example to illustrate the concept.
In reality multistage builds are used to create smaller final images, while keeping the entire build process inside the container.
Generally speaking one may install all development and runtime dependencies in a build stage, then copy just the final distribution package to the final stage, where only runtime dependencies are installed to reduce the final image size.
For example:
FROM ubuntu:latest AS base
RUN apt -y install some-library

FROM base AS build
RUN apt -y install some-library-dev
COPY . /src
WORKDIR /src
RUN make && make install

FROM base
COPY --from=build /usr/bin/app /usr/bin/app
CMD /usr/bin/app


---

