容器技术(三) 镜像缓存特性【9】
(六) 镜像缓存特性
Docker 会缓存已有镜像的镜像层,构建新镜像时,如果某镜像层已经存在,就直接使用,无需重新创建。
例如,在前面的 Dockerfile 中添加一点新内容,往镜像中复制一个文件:
root@cuiyongchao:/dockerfile# cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y vim
COPY testfile /
root@cuiyongchao:/dockerfile# ls ①
Dockerfile testfile
root@cuiyongchao:/dockerfile# docker build -t ubuntu-with-dockerfile2 .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM ubuntu
---> d70eaf7277ea
Step 2/3 : RUN apt-get update && apt-get install -y vim
---> Using cache ②
---> b75528ee6f18
Step 3/3 : COPY testfile / ③
---> 898ef2d48c22
Successfully built 898ef2d48c22
Successfully tagged ubuntu-with-dockerfile2:latest
root@cuiyongchao:/dockerfile#
① 确保 testfile 已存在。
② 重点,之前已经运行过相同的 RUN 指令,这次直接使用缓存中的镜像层 b75528ee6f18。
③ 执行 COPY 指令。其过程是启动临时容器,复制 testfile,提交新的镜像层 898ef2d48c22。
查看镜像创建历史:
root@cuiyongchao:/dockerfile# docker history ubuntu-with-vim-dockerfile:latest
IMAGE CREATED CREATED BY SIZE COMMENT
b75528ee6f18 14 hours ago /bin/sh -c apt-get update && apt-get install… 94.1MB
d70eaf7277ea 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 4 days ago /bin/sh -c #(nop) ADD file:435d9776fdd3a1834… 72.9MB
root@cuiyongchao:/dockerfile# docker history ubuntu-with-dockerfile2:latest
IMAGE CREATED CREATED BY SIZE COMMENT
898ef2d48c22 9 minutes ago /bin/sh -c #(nop) COPY file:b1006b61af59c628… 7B
b75528ee6f18 14 hours ago /bin/sh -c apt-get update && apt-get install… 94.1MB
d70eaf7277ea 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 4 days ago /bin/sh -c #(nop) ADD file:435d9776fdd3a1834… 72.9MB
root@cuiyongchao:/dockerfile#
history ubuntu-with-dockerfile2增加了镜像层898ef2d48c22 ,下面两层d70eaf7277ea、b75528ee6f18 与ubuntu-with-vim-dockerfile相同。
898ef2d48c22 COPY | |
---|---|
b75528ee6f18 RUN | b75528ee6f18 RUN |
d70eaf7277ea ubuntu | d70eaf7277ea ubuntu |
ubuntu-with-vim-dockerfile | ubuntu-with-dockerfile2 |
如果我们希望在构建镜像时不使用缓存,可以在 docker build
命令中加上 --no-cache
参数。Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。也就是说,如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。
例如,将上例中的命令执行顺序变化后:
root@cuiyongchao:/dockerfile# cat Dockerfile
FROM ubuntu
COPY testfile /
RUN apt-get update && apt-get install -y vim
root@cuiyongchao:/dockerfile# ls
Dockerfile testfile
root@cuiyongchao:/dockerfile# docker build -t ubuntu-with-dockerfile3 .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM ubuntu
---> d70eaf7277ea
Step 2/3 : COPY testfile /
---> fe8e4a66edda
Step 3/3 : RUN apt-get update && apt-get install -y vim
---> Running in 695a1cfbaf0e
......
从上面的输出可以看到生成了新的镜像层 fe8e4a66edda,历史的898ef2d48c22缓存已经失效 。
root@cuiyongchao:/dockerfile# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
bb79b6b2107f: Already exists
26694ef5449a: Already exists
7b85101950dd: Already exists
da919f2696f2: Already exists
3ae86ea9f1b9: Already exists
Digest: sha256:b82fb56847fcbcca9f8f162a3232acb4a302af96b1b2af1c4c3ac45ef0c9b968
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
root@cuiyongchao:/dockerfile#
docker pull 命令输出显示各层都已经存在,不需要下载。通过 docker history 可以进一步验证。
root@cuiyongchao:/dockerfile# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
bb79b6b2107f: Already exists
26694ef5449a: Already exists
7b85101950dd: Already exists
da919f2696f2: Already exists
3ae86ea9f1b9: Already exists
Digest: sha256:b82fb56847fcbcca9f8f162a3232acb4a302af96b1b2af1c4c3ac45ef0c9b968
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
root@cuiyongchao:/dockerfile# docker history httpd:latest
IMAGE CREATED CREATED BY SIZE COMMENT
3dd970e6b110 2 weeks ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B
<missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B
<missing> 2 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 60.9MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_SHA256=740eddf6… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.46 0B
<missing> 2 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 7.38MB
<missing> 2 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:0dc53e7886c35bc21… 69.2MB