2017年11月30日 星期四

[docker] Docker image 怎麼 build 沒 cache 的 images --no-cache


我們會使用 docker build 來 build image
當docker 辨識是同樣 layer 的時候他會使用已經產生的 layer cache。

當 我們Dockerfile 中有 RUN apt-get update 這類型的指令時,很容易就有過時的狀況,
container 沒有辦法獲取過期或是已經消失的 file 或是url 。

這時候我們就要使用 $ docker build -t "mytest" --no-cache .

舉個例子來看 Dockerfile
$ cat Dockerfile
FROM ubuntu:16.04
RUN apt-get update

第一次 build image

$ docker build -t "mytest" .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu:16.04
 ---> d355ed3537e9
Step 2/2 : RUN apt-get update
 ---> Running in e9efeedd4b03
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [53.1 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [505 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.9 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [229 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [3479 B]
Get:10 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [231 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [866 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.7 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [719 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [18.5 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5174 B]
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7150 B]
Fetched 24.6 MB in 8s (2966 kB/s)
Reading package lists...
 ---> 5821cf494987
Removing intermediate container e9efeedd4b03
Successfully built 5821cf494987
Successfully tagged mytest:latest

第二次 build 的時候已經有cache

$ docker build -t "mytest" .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu:16.04
 ---> d355ed3537e9
Step 2/2 : RUN apt-get update
 ---> Using cache
 ---> 5821cf494987
Successfully built 5821cf494987
Successfully tagged mytest:latest

這時候注意到已經沒有在 apt-get update 要是有些檔案已經從sourcelist移除 就發生狀況

怎麼不使用 cache 再 build 一次呢

$ docker build -t "mytest" --no-cache .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu:16.04
 ---> d355ed3537e9
Step 2/2 : RUN apt-get update
 ---> Running in 76da71f4b31b
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [53.1 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [505 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:6 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.9 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [229 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [3479 B]
Get:9 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [231 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [866 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.7 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [719 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [18.5 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5174 B]
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7150 B]
Fetched 24.6 MB in 9s (2597 kB/s)
Reading package lists...
 ---> 7ffb5d5b40ab
Removing intermediate container 76da71f4b31b
Successfully built 7ffb5d5b40ab
Successfully tagged mytest:latest



ref
「Allen 谈 Docker 系列」docker build 的 cache 机制
http://open.daocloud.io/docker-build-de-cache-ji-zhi/

沒有留言:

張貼留言