014、镜像的缓存特性(2019-01-03 周四)

 
 
构建centos-wget 镜像前本地没有缓存centos镜像,在构建的第一步需要dokcer pull centos镜像
 
root@docker-lab:~/docker# cat Dockerfile
FROM centos
RUN yum install -y wget
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
root@docker-lab:~/111# docker build  -t centos-wget .     
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0
Status: Downloaded newer image for centos:latest
---> 1e1148e4cc2c
Step 2/2 : RUN yum install -y wget
.
.
.
.
 
构建centos-wget 镜像前本地有缓存centos镜像,在构建的第一步直接使用本地缓存,无需docker pull
 
root@docker-lab:~/docker# cat Dockerfile
FROM centos
RUN yum install -y wget
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
root@docker-lab:~/docker# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:38777a4106f0072649128ea4236241345a3ed21c55abbbd53bad5342549f6126
Status: Downloaded newer image for centos:latest
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        4 weeks ago         202MB
root@docker-lab:~/docker# docker build -t centos-wget .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
---> 1e1148e4cc2c
Step 2/2 : RUN yum install -y wget
.
.
.
.
 
继续构建一个新的镜像,比centos-wget多执行一个uptime,可以看到第一步和第二步直接使用的缓存,并未重新构建
 
root@docker-lab:~/docker# cat Dockerfile
FROM centos
RUN yum install -y wget
RUN uptime
root@docker-lab:~/docker# docker build -t centos-uptime .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : RUN yum install -y wget
---> Using cache
---> 31093d4c7d97
Step 3/3 : RUN uptime
---> Running in 303cb5e38dd3
16:35:42 up 203 days,  7:58,  0 users,  load average: 0.00, 0.00, 0.00
Removing intermediate container 303cb5e38dd3
---> cb7f8bec5d08
Successfully built cb7f8bec5d08
Successfully tagged centos-uptime:latest
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-uptime       latest              cb7f8bec5d08        11 seconds ago      272MB
centos-wget         latest              31093d4c7d97        7 minutes ago       272MB
centos              latest              1e1148e4cc2c        4 weeks ago         202MB
 
如果将yum和uptime两个对调,则不会使用之前的 centos-wget 镜像缓存
 
root@docker-lab:~/docker# cat Dockerfile
FROM centos
RUN uptime
RUN yum install -y wget
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-uptime       latest              cb7f8bec5d08        5 minutes ago       272MB
centos-wget         latest              31093d4c7d97        12 minutes ago      272MB
centos              latest              1e1148e4cc2c        4 weeks ago         202MB
root@docker-lab:~/docker# docker build -t centos-test .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos
---> 1e1148e4cc2c
Step 2/3 : RUN uptime
---> Running in 1724b8f64a50
16:41:11 up 203 days,  8:03,  0 users,  load average: 0.00, 0.02, 0.00
Removing intermediate container 1724b8f64a50
---> 91a208dedb3d
Step 3/3 : RUN yum install -y wget
---> Running in 85f7f29637d5
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7 will be installed
--> Finished Dependency Resolution
.
.
.
.
root@docker-lab:~/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos-test         latest              fc8b11ebfd31        About a minute ago   272MB
centos-uptime       latest              cb7f8bec5d08        7 minutes ago        272MB
centos-wget         latest              31093d4c7d97        14 minutes ago       272MB
centos              latest              1e1148e4cc2c        4 weeks ago          202MB
 
 
总结:
    1、构建镜像时,FROM 中的镜像如果本地有,就会直接使用本地缓存
    2、构建镜像时,如果Dockerfile前N行与之前某镜像构建时Dockerfile的前N行一样,那将直接使用之前镜像中的层,不比从头开始构建。除非在docker build 中添加 --no-cache 参数
    
 
 
posted @ 2019-01-04 00:54  三角形  阅读(163)  评论(0编辑  收藏  举报