Here_2_learn
Here_2_learn

Reputation: 5451

Docker : Why the Commands from Dockerfile were not being executed?

I have a query regarding the execution of commands in a container from the Dockerfile.

#Dockerfile Content:
  FROM ubuntu:14.04
  MAINTAINER RAGHU
  RUN echo "hello World"

Build procedure

 docker build -t helloworld .
 Sending build context to Docker daemon 20.04 MB
 Step 1 : FROM ubuntu:14.04
 ---> b1719e1db756
 Step 2 : MAINTAINER RAGHU
 ---> Using cache
 ---> 1704b62d66e2
 Step 3 : RUN echo "hello World"
 ---> Running in 2b513872628e
 hello World
 ---> ff559047fd19
 Removing intermediate container 2b513872628e
 Successfully built ff559047fd19

Query 1: Why does the below execution has not resulted in any result ? I am expecting to print "hello World". What wrong has happened ?

 root@labadmin-VirtualBox:/home/labadmin# docker run ff559047fd19
 root@labadmin-VirtualBox:/home/labadmin# docker ps -a
 CONTAINER ID   IMAGE         COMMAND        CREATED     STATUS     PORTS         NAMES
 a8eede1874a2   ff559047fd19  "/bin/bash"    27 seconds ago Exited (0) 26 seconds ago  tiny_williams
 root@labadmin-VirtualBox:/home/labadmin# docker logs a8eede1874a2
 #Above command has not resulted in any logs for this container.

Query 2 :I am able to run the execution steps in the following way. Why it has executed the command in the below container and why not when I run the container a8eede1874a2 ?

 root@labadmin-VirtualBox:/home/labadmin# docker run -it ff559047fd19
 root@486595ac9110:/# echo "hello world"
 hello world
 root@486595ac9110:/# exit
 exit
 root@labadmin-VirtualBox:/home/labadmin/RAGHU/welcome-page# docker ps -a
 CONTAINER ID   IMAGE   COMMAND      CREATED         STATUS         PORTS  NAMES
 486595ac9110   ff559047fd19  "/bin/bash"  22 seconds ago Exited (0) 7 seconds ago    goofy_noyce
 a8eede1874a2   ff559047fd19   "/bin/bash"  About a minute ago  Exited (0)      About a minute ago   tiny_williams
 root@labadmin-VirtualBox:/home/labadmin# docker logs 486595ac9110
 root@486595ac9110:/# echo "hello world"
 hello world
 root@486595ac9110:/# exit
 exit
 root@labadmin-VirtualBox:/home/labadmin/RAGHU/welcome-page# 

Upvotes: 4

Views: 13561

Answers (2)

Haoming Zhang
Haoming Zhang

Reputation: 2842

Question 1:

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

So when you execute RUN echo "hello World", echo "hello World" will print out a "hello World". And the execute result, which is executed successfully with return code 0, has been added as a new lay to the top of previous layer. In your case, the previous layer is the execute result of MAINTAINER RAGHU.

When you create container with your image ff559047fd19, RUN echo "hello World" will NOT be executed again, because this command will only be executed during image building process. Which is the step 3:

 Step 3 : RUN echo "hello World"
 ---> Running in 2b513872628e
 hello World

Question 2:

You were create a new container with your image ff559047fd19. Because of ff559047fd19 is based on ubuntu, so you are be able to attach to your new container's bash.

 root@486595ac9110:/# echo "hello world"
 hello world

Here, you are simply execute an echo command in your new container's bash.

Upvotes: 3

Hevlastka
Hevlastka

Reputation: 1948

Docker images are built layer by layer using (you guessed it!) docker containers. To get Hello World to print when you're trying to run a container you'll need to specify an ENTRYPOINT or a CMD within your Dockerfile.

Taken from Dockerfile reference:

Note: don’t confuse RUN with CMD. RUN actually runs a command and commits the result; CMD does not execute anything at build time, but specifies the intended command for the image.

Moving on swiftly to your queries:

Query 1: Why does the below execution has not resulted in any result ? I am expecting to print "hello World". What wrong has happened ?

In your Dockerfile, when you say RUN echo "hello world" what docker actually does is it creates an intermediate container, executes the command and saves the state of the container as a layer on which it'll run the next command. In your example you can see that "hello world" was actually printed in Step 3.

 Step 3 : RUN echo "hello World"
 ---> Running in 2b513872628e
 hello World

And for your second query:

Query 2: I am able to run the execution steps in the following way. Why it has executed the command in the below container and why not when I run the container a8eede1874a2 ?

So when you've created container with the hash of a8eede1874a2 what you're actually doing is well... nothing. You haven't specified CMD or ENTRYPOINT or a command through the command line. Your container started and stopped since there was nothing to execute.

Now in your second example you're executing an interactive shell in which you're running the echo "hello world".

Example Dockerfile:

FROM ubuntu:14.04
MAINTAINER RAGHU
CMD "echo hello World"

Useful references:

Upvotes: 4

Related Questions