(五) Dockerfile 构建镜像

Dockerfile 是一个文本文件,记录了镜像构建的所有步骤。

(1)第一个dockerfile

用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为:

FROM ubuntu
RUN apt-get update && apt-get install -y vim

下面我们运行 docker build 命令构建镜像并详细分析每个细节。

root@cuiyongchao:/dockerfile# pwd ①
/dockerfile
root@cuiyongchao:/dockerfile# ls ②
Dockerfile
root@cuiyongchao:/dockerfile# docker build -t ubuntu-with-vim-dockerfile . ③
Sending build context to Docker daemon  2.048kB ④
Step 1/2 : FROM ubuntu ⑤
 ---> d70eaf7277ea
Step 2/2 : RUN apt-get update && apt-get install -y vim ⑥
 ---> Running in 0d4f882d4635 ⑦
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
。。。。。。
Setting up vim (2:8.1.2269-1ubuntu5) ...
--->b75528ee6f18 ⑧
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Removing intermediate container 0d4f882d4635 ⑨
Successfully built b75528ee6f18 ⑩
Successfully tagged ubuntu-with-vim-dockerfile:latest

① 当前目录为 /dockerfile。

② Dockerfile 准备就绪。

③ 运行 docker build 命令,-t 将新镜像命名为 ubuntu-with-vim-dockerfile,命令末尾的 . 指明 build context 为当前目录。Docker 默认会从 build context 中查找 Dockerfile 文件,我们也可以通过 -f 参数指定 Dockerfile 的位置。

④ 从这步开始就是镜像真正的构建过程。 首先 Docker 将 build context 中的所有文件发送给 Docker daemon。build context 为镜像构建提供所需要的文件或目录。Dockerfile 中的 ADD、COPY 等命令可以将 build context 中的文件添加到镜像。此例中,build context 为当前目录 /dockerfile,该目录下的所有文件和子目录都会被发送给 Docker daemon。所以,使用 build context 就得小心了,不要将多余文件放到 build context,特别不要把 //usr` 作为 build context,否则构建过程会相当缓慢甚至失败。

⑤ Step 1:执行 FROM,将 ubuntu 作为 base 镜像。
ubuntu 镜像 ID 为 d70eaf7277ea。

⑥ Step 2:执行 RUN,安装 vim,具体步骤为 ⑦、⑧、⑨。

⑦ 启动 ID 为 0d4f882d4635 的临时容器,在容器中通过 apt-get 安装 vim。

⑧ 安装成功后,将容器保存为镜像,其 ID 为 b75528ee6f18。
这一步底层使用的是类似 docker commit 的命令。

⑨ 删除临时容器 0d4f882d4635。

⑩ 镜像构建成功。
通过 docker images 查看镜像信息。

root@cuiyongchao:~# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-vim-dockerfile   latest              b75528ee6f18        2 minutes ago       167MB
ubuntu-with-vi               latest              218e302a1e28        43 minutes ago      167MB
ubuntu                       latest              d70eaf7277ea        3 days ago          72.9MB

​ 镜像 ID 为 b75528ee6f18,与构建时的输出一致。在上面的构建过程中,我们要特别注意指令 RUN 的执行过程 ⑦、⑧、⑨。Docker 会在启动的临时容器中执行操作,并通过 commit 保存为新的镜像。

(2)查看镜像分层结构

​ ubuntu-with-vim-dockerfile 是通过在 base 镜像的顶部添加一个新的镜像层而得到的。这个新镜像层的内容由 RUN apt-get update && apt-get install -y vim 生成。这一点我们可以通过 docker history 命令验证。

root@cuiyongchao:~# docker history ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d70eaf7277ea        3 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           3 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           3 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B                  
<missing>           3 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B                
<missing>           3 days ago          /bin/sh -c #(nop) ADD file:435d9776fdd3a1834…   72.9MB              
root@cuiyongchao:~# docker history ubuntu-with-vim-dockerfile:latest 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
b75528ee6f18        10 minutes ago      /bin/sh -c apt-get update && apt-get install…   94.1MB              
d70eaf7277ea        3 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           3 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           3 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B                  
<missing>           3 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B                
<missing>           3 days ago          /bin/sh -c #(nop) ADD file:435d9776fdd3a1834…   72.9MB              
root@cuiyongchao:~# 

docker history 会显示镜像的构建历史,也就是 Dockerfile 的执行过程。ubuntu-with-vim-dockerfile 与 ubuntu 镜像相比,确实只是多了顶部的一层 b75528ee6f18,由 apt-get 命令创建,大小为 94.01MB。docker history 也向我们展示了镜像的分层结构,每一层由上至下排列。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/cuiyongchao007/p/13903793.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!