镜像的缓存特性 - 每天5分钟玩转 Docker 容器技术(14)

上一节我们学习了镜像的分层结构,今天讨论镜像的缓存特性。
 

Docker 会缓存已有镜像的镜像层,构建新镜像时,如果某镜像层已经存在,就直接使用,无需重新创建。

举例说明。
在前面的 Dockerfile 中添加一点新内容,往镜像中复制一个文件:

73.png

root@ubuntu:~# ls           ①

Dockerfile  testfile

root@ubuntu:~#

root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-2 .

Sending build context to Docker daemon 32.77 kB

Step 1 : FROM ubuntu

 ---> f753707788c5

Step 2 : RUN apt-get update && apt-get install -y vim

 ---> Using cache         ②

 ---> 35ca89798937

Step 3 : COPY testfile /          ③

 ---> 8d02784a78f4

Removing intermediate container bf2b4040f4e9

Successfully built 8d02784a78f4


① 确保 testfile 已存在。

② 重点在这里:之前已经运行过相同的 RUN 指令,这次直接使用缓存中的镜像层 35ca89798937。

③ 执行 COPY 指令。
其过程是启动临时容器,复制 testfile,提交新的镜像层 8d02784a78f4,删除临时容器。

在 ubuntu-with-vi-dockerfile 镜像上直接添加一层就得到了新的镜像 ubuntu-with-vi-dockerfile-2。

如果我们希望在构建镜像时不使用缓存,可以在 docker build 命令中加上 --no-cache 参数。

Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。

也就是说,如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。

举例说明,比如交换前面 RUN 和 COPY 的顺序:

虽然在逻辑上这种改动对镜像的内容没有影响,但由于分层的结构特性,Docker 必须重建受影响的镜像层。

root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-3 .

Sending build context to Docker daemon 37.89 kB

Step 1 : FROM ubuntu

 ---> f753707788c5

Step 2 : COPY testfile /

 ---> bc87c9710f40

Removing intermediate container 04ff324d6af5

Step 3 : RUN apt-get update && apt-get install -y vim

 ---> Running in 7f0fcb5ee373

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]

......


从上面的输出可以看到生成了新的镜像层 bc87c9710f40,缓存已经失效。

除了构建时使用缓存,Docker 在下载镜像时也会使用。例如我们下载 httpd 镜像。

docker pull 命令输出显示第一层(base 镜像)已经存在,不需要下载。

由 Dockerfile 可知 httpd 的 base 镜像为 debian,正好之前已经下载过 debian 镜像,所以有缓存可用。通过 docker history 可以进一步验证。

下一节我们学习如何调试 Dockerfile。

二维码+指纹.png

                                    </div>
                                        </div>

转载于:https://my.oschina.net/u/2397560/blog/897929

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.youkuaiyun.com/weixin_33859231">
                <img src="https://profile.csdnimg.cn/D/C/6/3_weixin_33859231" class="avatar_pic" username="weixin_33859231">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.youkuaiyun.com/weixin_33859231" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">weixin_33859231</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog4.png">                                            </div>
                <div class="text"><span>原创文章 135</span><span>获赞 45</span><span>访问量 27万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">关注</a>
                                                            <a href="https://im.youkuaiyun.com/im/main.html?userName=weixin_33859231" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值