# 映像檔分層概念

把映像檔拆開，裡面是一層又一層的Layer，像堆疊積木一樣，堆疊而成。

容器則是在每個映像檔上面，再加上一個R/W層。

![](https://1309293527-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lt87Z2Vgpk1kICGRgqP%2F-MAZ35e-gFxQLhnuTHoQ%2F-MAZ4VNtNaBRq_mQYVlG%2Fcontainer-layers.jpg?alt=media\&token=2ca40e40-9d1d-4529-94d8-d307467b2e79)

建立二個映像檔來比較看看。

#### 映像檔`cutejaneii/curl_alpine:3.7`的`Dockerfile`

```
FROM alpine:3.7
RUN apk add --no-cache curl
ENTRYPOINT ["/usr/bin/curl"]
```

#### 映像檔`cutejaneii/curl2_alpine:3.7`的`Dockerfile`

```
FROM alpine:3.7
RUN apk add --no-cache curl
RUN mkdir my_data
ENTRYPOINT ["/usr/bin/curl"]
```

用`docker history`觀察一下

映像檔`alpine:3.7`的ID是`6d1ef012b567`

映像檔`curl_alpine:3.7`是由3個Layer組成，最底下的ID是`6d1ef012b567（也就是映像檔alpine:3.7）`，再往上每一個指令就多一個Layer

映像檔`curl2_alpine:3.7`是由4個Layer組成，最底下的ID是`6d1ef012b567（也就是映像檔alpine:3.7）`，再往上每一個指令就多一個Layer

```
root@vm:/home/jennifer# docker history alpine:3.7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
6d1ef012b567        15 months ago       /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           15 months ago       /bin/sh -c #(nop) ADD file:aa17928040e31624c…   4.21MB

root@vm:/home/jenniferg# docker history cutejaneii/curl_alpine:3.7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
119f0ca622ff        11 seconds ago      /bin/sh -c #(nop)  ENTRYPOINT ["/usr/bin/cur…   0B
ebd37ddf2c7c        12 seconds ago      /bin/sh -c apk add --no-cache curl              1.39MB
6d1ef012b567        15 months ago       /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           15 months ago       /bin/sh -c #(nop) ADD file:aa17928040e31624c…   4.21MB

root@vm:/home/jennifer# docker history cutejaneii/curl2_alpine:3.7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
cbf3378be8a3        11 seconds ago      /bin/sh -c #(nop)  ENTRYPOINT ["/usr/bin/cur…   0B
5dc5243126ed        11 seconds ago      /bin/sh -c mkdir my_data                        0B
ebd37ddf2c7c        2 minutes ago       /bin/sh -c apk add --no-cache curl              1.39MB
6d1ef012b567        15 months ago       /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           15 months ago       /bin/sh -c #(nop) ADD file:aa17928040e31624c…   4.21MB

```

來看建置過程中有什麼不同

映像檔`curl_alpine:3.7`的建置過程：

```
root@vm:/home/jennifer# docker build -t cutejaneii/curl_alpine:3.7 .
Sending build context to Docker daemon  4.096kB
Step 1/3 : FROM alpine:3.7
 ---> 6d1ef012b567
Step 2/3 : RUN apk add --no-cache curl
 ---> Running in 4225ed3fd744
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/4) Installing ca-certificates (20190108-r0)
(2/4) Installing libssh2 (1.9.0-r1)
(3/4) Installing libcurl (7.61.1-r3)
(4/4) Installing curl (7.61.1-r3)
Executing busybox-1.27.2-r11.trigger
Executing ca-certificates-20190108-r0.trigger
OK: 6 MiB in 17 packages
Removing intermediate container 4225ed3fd744
 ---> ebd37ddf2c7c
Step 3/3 : ENTRYPOINT ["/usr/bin/curl"]
 ---> Running in f36250362075
Removing intermediate container f36250362075
 ---> 119f0ca622ff
Successfully built 119f0ca622ff
Successfully tagged cutejaneii/curl_alpine:3.7
```

映像檔`curl2_alpine:3.7`的建置過程：（因為已經存在`RUN apk add --no-cache curl`指令的Layer，所以在這裡直接使用，就不需再建置，節省很多時間）

```
root@vm:/home/jennifer# docker build -t cutejaneii/curl2_alpine:3.7 .
Sending build context to Docker daemon   5.12kB
Step 1/4 : FROM alpine:3.7
 ---> 6d1ef012b567
Step 2/4 : RUN apk add --no-cache curl
 ---> Using cache
 ---> ebd37ddf2c7c
Step 3/4 : RUN mkdir my_data
 ---> Running in abaf1b78c167
Removing intermediate container abaf1b78c167
 ---> 5dc5243126ed
Step 4/4 : ENTRYPOINT ["/usr/bin/curl"]
 ---> Running in 520bffdaf310
Removing intermediate container 520bffdaf310
 ---> cbf3378be8a3
Successfully built cbf3378be8a3
Successfully tagged cutejaneii/curl2_alpine:3.7
```

以一張示意圖來看`curl_alpine:3.7`，就會是這個樣子

![](https://1309293527-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lt87Z2Vgpk1kICGRgqP%2F-MAZPisD2FAjh5yIb0BE%2F-MAZTLBxq8deUp3LWRQC%2Fdocker.png?alt=media\&token=a40305b7-173a-44d8-8da6-046666cf688b)

利用`docker images -a`查看映像檔的資訊

```
root@vm:/home/jennifer# docker images -a
REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
<none>                                        <none>              5dc5243126ed        2 hours ago         5.6MB
cutejaneii/curl2_alpine                       3.7                 cbf3378be8a3        2 hours ago         5.6MB
cutejaneii/curl_alpine                        3.7                 119f0ca622ff        2 hours ago         5.6MB
<none>                                        <none>              ebd37ddf2c7c        2 hours ago         5.6MB
alpine                                        3.7                 6d1ef012b567        15 months ago       4.21MB
```

除了`docker build`之外，在`docker pull`及`docker push`時，都會略過已存在的Layer。也就是說，變動的部份在Dockerfile的愈下層，能夠共用的Layer愈多。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cutejaneii.gitbook.io/docker/docker-image-1/ying-xiang-dang-fen-ceng-gai-nian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
