Docker:中间容器是如何形成的

问题描述 投票:17回答:1

我想了解使用Dockerfile构建Docker镜像所涉及的执行步骤。我在下面列出了几个问题。请帮助我理解构建过程。

Dockerfile content

#from base image
FROM ubuntu:14.04
#author name
MAINTAINER RAGHU
#commands to run in the container
RUN echo "hello Raghu"
RUN sleep 10
RUN echo "TASK COMPLETED"

用于构建图像的命令:docker build -t raghavendar/hands-on:2.0 .

Sending build context to Docker daemon 20.04 MB
Step 1 : FROM ubuntu:14.04
---> b1719e1db756
Step 2 : MAINTAINER RAGHU
---> Running in 532ed79e6d55
---> ea6184bb8ef5
Removing intermediate container 532ed79e6d55
Step 3 : RUN echo "hello Raghu"
---> Running in da327c9b871a
hello Raghu
---> f02ff92252e2
Removing intermediate container da327c9b871a
Step 4 : RUN sleep 10
---> Running in aa58dea59595
---> fe9e9648e969
Removing intermediate container aa58dea59595
Step 5 : RUN echo "TASK COMPLETED"
---> Running in 612adda45c52
TASK COMPLETED
---> 86c73954ea96
Removing intermediate container 612adda45c52
Successfully built 86c73954ea96

在第2步中:

Step 2 : MAINTAINER RAGHU
    ---> Running in 532ed79e6d55 

问题1:它表明它在id为532ed79e6d55的容器中运行,但是这个容器形成了什么Docker镜像?

---> ea6184bb8ef5  

问题2:这个id是什么?它是图像还是容器?

Removing intermediate container 532ed79e6d55

问题3:是否从中间容器中保存了多层的最终图像?

docker dockerfile docker-container
1个回答
28
投票

是的,Docker图像是分层的。构建新映像时,Docker会为Dockerfile中的每条指令(RUNCOPY等)执行此操作:

  1. 从前一个图像层(或第一个命令的基础FROM图像)创建一个临时容器;
  2. 在临时“中间”容器中运行Dockerfile指令;
  3. 将临时容器保存为新图像层。

最终的图像层标记有您为图像命名的任何内容 - 如果您运行docker history raghavendar/hands-on:2.0,这将很清楚,您将看到每个图层以及创建它的指令的缩写。

您的具体查询:

1)532是一个临时容器,由图像ID b17创建,这是你的FROM图像,ubuntu:14.04

2)ea6是作为指令输出而创建的图像层,即来自保存中间容器532

3)是的。 Docker称之为Union File System,这是图像如此高效的主要原因。

© www.soinside.com 2019 - 2024. All rights reserved.