Docker : 为什么 Docker 文件中的命令没有被执行?

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

我有一个关于在容器中从 Dockerfile 执行命令的问题。

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

构建过程

 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

疑问一:为什么下面的执行没有结果?我期待打印 "hello World"。发生了什么错误?

 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.

查询 2 :我可以 运行 按以下方式执行步骤。为什么它在下面的容器中执行了命令,为什么当我 运行 容器 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# 

Docker 图像是使用(您猜对了!)docker 容器逐层构建的。要在尝试 运行 容器时让 Hello World 打印,您需要在 Docker 文件中指定 ENTRYPOINT or a CMD

取自Docker文件参考:

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.

快速继续您的查询:

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

在你的 Docker 文件中,当你说 RUN echo "hello world" 时 docker 实际上做的是创建一个中间容器,执行命令并将容器的状态保存为一个层它将 运行 下一个命令。在您的示例中,您可以看到 "hello world" 实际上是在步骤 3 中打印的。

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

对于您的第二个查询:

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 ?

因此,当您使用 a8eede1874a2 的散列创建容器时,您实际上所做的是……没什么。您尚未通过命令行指定 CMD 或 ENTRYPOINT 或命令。由于没有可执行的内容,您的容器启动并停止。

现在,在您的第二个示例中,您正在执行一个交互式 shell,其中您 运行 宁 echo "hello world"

示例Docker文件:

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

有用的参考资料:

问题 1:

根据Dockerfile referenceRUN指令将在当前图像之上的新层中执行任何命令并提交结果。生成的提交图像将用于 Dockerfile.

中的下一步

所以当你执行RUN echo "hello World"时,echo "hello World"会打印出一个"hello World"。并且return code 0执行成功的执行结果,作为新lay添加到上一层的顶部。在你的例子中,前一层是 MAINTAINER RAGHU.

的执行结果

当您使用镜像 ff559047fd19 创建容器时,RUN echo "hello World" 将不会再次执行,因为此命令只会在镜像构建过程中执行。这是第 3 步:

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

问题 2:

您正在使用图像创建一个新容器 ff559047fd19。由于 ff559047fd19 基于 ubuntu,因此您可以附加到新容器的 bash。

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

在这里,您只需在新容器的 bash.

中执行 echo 命令