<p>Yes your understanding is correct. The <code>RUN &lt;commands&gt;</code> command in the Dockerfile is somewhat equivalent to</p>
<pre><code>container = $(docker run base-image) #1
docker exec base-image commands #2 
docker commit $container #3
</code></pre>
<p>Since images are static, we'll need a running instance/process to execute our command. this is why a container is created from the base image. #1</p>
<p>After the execution of the command in #2, the changes are committed back #3, which means a new layer is created on top of the previous layers. This layer will contain the changes introduced by the container in (#2).</p>

CC BY-SA 4.0
2020-08-26T14:40:43.877Z
To be clear - `diamol:base` image is not modified. New, intermediate image is created by the command.
Konrad Botor
CC BY-SA 4.0
2020-08-25T09:05:45.070Z
2020-08-25T09:05:45.070Z
Iduoad
<p>I'm new to Docker, sorry if my question sounds dumb.
below is a dockerfile from a textbook:</p>
<pre><code>FROM diamol/base AS build-stage                  
RUN echo 'Building...' &gt; /build.txt              #line 2

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

FROM diamol/base
COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt
</code></pre>
<p>and the author says:</p>
<p><em><strong>The RUN instruction executes a command inside a container during the build, and any output from that command is saved in the image layer.</strong></em></p>
<p>My question is:</p>
<p>Since the author mentioned RUN instruction executes a command inside a <strong>container</strong> during the build, does it mean that in the first RUN command in line 2, a contianer is created based on the <code>diamol/base</code> image, and this container modify the content of the files then commit the change back to <code>diamol/base</code> image, then is container is terminated? and same thing happen in line 6,container created to run and then terminate, image gets updated, is my understanding correct?</p>

CC BY-SA 4.0
2020-09-10T07:11:12.347Z
Multiply posted at https://stackoverflow.com/q/63574075/340790 .
JdeBP
<docker>
is a running container created by each RUN command during multistage build?
amjad
# is a running container created by each RUN command during 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:
FROM diamol/base AS build-stage                  
RUN echo 'Building...' > /build.txt              #line 2

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

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

and the author says:
The RUN instruction executes a command inside a container during the build, and any output from that command is saved in the image layer.
My question is:
Since the author mentioned RUN instruction executes a command inside a container during the build, does it mean that in the first RUN command in line 2, a contianer is created based on the diamol/base image, and this container modify the content of the files then commit the change back to diamol/base image, then is container is terminated? and same thing happen in line 6,container created to run and then terminate, image gets updated, is my understanding correct?

**Tags:** docker

> **Comments:**
> **username_1**: Multiply posted at https://stackoverflow.com/q/63574075/340790 .  

---

## 1 answers

### Answer by username_2 on 2020-08-25. Score: 2 | Accepted answer

Yes your understanding is correct. The RUN  command in the Dockerfile is somewhat equivalent to
container = $(docker run base-image) #1
docker exec base-image commands #2 
docker commit $container #3

Since images are static, we'll need a running instance/process to execute our command. this is why a container is created from the base image. #1
After the execution of the command in #2, the changes are committed back #3, which means a new layer is created on top of the previous layers. This layer will contain the changes introduced by the container in (#2).

> **Comments:**
> **username_3**: To be clear - `diamol:base` image is not modified. New, intermediate image is created by the command.  

---

