014、映象的快取特性(2019-01-03 週四)

三角形發表於2019-01-04
 
 
構建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 引數
    
 
 

相關文章