(六) 鏡像快取特性
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/199904.html
標籤:其他
