1

Below is my Docker file

ARG tag_info=latest
FROM alpine:${tag_info} AS my_apline
ARG message='Hello World'
RUN echo ${message}
RUN echo ${message} > myfile.txt
RUN echo "Hi Harry"

When i run docker image build -t myimage:v1_0 - < Dockerfile

the output is :

Sending build context to Docker daemon  2.048kB
Step 1/6 : ARG tag_info=latest
Step 2/6 : FROM alpine:${tag_info} AS my_apline
latest: Pulling from library/alpine
cbdbe7a5bc2a: Already exists
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
 ---> f70734b6a266
Step 3/6 : ARG message='Hello World'
 ---> Running in 74bcc8897e8e
Removing intermediate container 74bcc8897e8e
 ---> d8d50432d375
Step 4/6 : RUN echo ${message}
 ---> Running in 730ed8e1c1d3
Hello World
Removing intermediate container 730ed8e1c1d3
 ---> 8417e3167e80
Step 5/6 : RUN echo ${message} > myfile.txt
 ---> Running in c66018331383
Removing intermediate container c66018331383
 ---> 07dc27d8ad3d
Step 6/6 : RUN echo "Hi Harry"
 ---> Running in fb92fb234e42
Hi Harry
Removing intermediate container fb92fb234e42
 ---> a3bec122a77f

It displays "Hi Harry" and "Hello World" in the middle (which I do not understand why)

Why "Hi Harry and "Hello World" is not displayed when i spin the container from image file?

Harry S
  • 63
  • 1
  • 10

2 Answers2

3

Because the RUN command executes the commands when you are building the image not when you are spinning up the container using the image. It is used to alter the image like adding new packages using apt-get or changing file permissions etc

If you need something to run when the container is starting you need to use command to entrypoint instructions

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thanks for the Info. I am assuming if RUN instruction is adding new layer then the change will be seen during container's spin – Harry S Apr 27 '20 at 11:48
  • @HarryS That's not correct. RUN instructions do add new layers to the image built, but in this case, the layer(s) do nothing. The echo commands are run at build time, not runtime, when configured this way. Think of the Dockerfile as a build script - the commands are executing as the image is built - the image is a package. – ryanwebjackson Jun 30 '22 at 15:27
2

From the docker official documentation:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

You should use CMD if you want to obtain the described behavior.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

This has three forms:

 - CMD ["executable","param1","param2"] (exec form, this is the preferred form)
 - CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
 - CMD command param1 param2 (shell form)
Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
  • Thanks for your reply. To be more clear ,if my RUN instruction is adding something to content of previous layer (which echo "Hi Harry and Hello World is not adding anything) then it will create new layer and content will be displayed during container Spin? – Harry S Apr 27 '20 at 11:38