0

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?

2

Yes your understanding is correct. The RUN <commands> 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).

| improve this answer | |
  • 1
    To be clear - diamol:base image is not modified. New, intermediate image is created by the command. – Konrad Botor Aug 26 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.