一、拉取nginx镜像并运行
docker run -d nginx
latest: Pulling from library/nginx
000eee12ec04: Pull complete
eb22865337de: Pull complete
bee5d581ef8b: Pull complete
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
c272a75180419d4513d2bc324f0413bc33f9bbbc6687a6c8fb23459e3e655b28
获取imageid
[root@VM_0_15_centos image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 11 days ago 126MB
现在把注意力集中到两个目录
- /var/lib/docker/overlay2
- /var/lib/docker/image
二、 /var/lib/docker/image
查看该目录下有啥,可以想象如果有多个存储驱动的话,这里还会有aufs、devicemapper等等。。。
[root@VM_0_15_centos docker]# ll image/
total 4
drwx------ 5 root root 4096 Dec 4 09:30 overlay2
让我们来关注imagedb和layerdb
[root@VM_0_15_centos docker]# ll image/overlay2/
total 16
drwx------ 4 root root 4096 Nov 15 12:18 distribution
drwx------ 4 root root 4096 Nov 8 18:32 imagedb
drwx------ 5 root root 4096 Nov 15 14:47 layerdb
-rw------- 1 root root 271 Dec 4 09:30 repositories.json
-imagedb
根据这个imageID查找
cat overlay2/imagedb/content/sha256/231d40e811cd970168fb0c4770f2161aa30b9ba6fe8e68527504df69643aa145 | grep "rootfs"
那么可以查找到该image组成部分即layerid:
"rootfs":{"type":"layers",
"diff_ids":[
"sha256:831c5620387fb9efec59fc82a42b948546c6be601e3ab34a87108ecf852aa15f",
"sha256:5fb987d2e54d85820d95d6c31f3fe4cd95bf71fe6d9d9e4684082cb551b728b0", "sha256:4fc1aa8003a3d0d2481f10d17773869cbff12c1008df30e0bab8259086a0311c"]} 这个三个sha256自上而下 就是底层到顶层
- layerdb
然后去查找overlay2/layerdb/
[root@VM_0_15_centos sha256]# ll
total 12
drwx------ 2 root root 4096 Dec 4 09:30 77fcff986d3b13762e4777046b9210a109fda20cb261bd3bbe5d7161d4e73c8e
drwx------ 2 root root 4096 Dec 4 09:30 831c5620387fb9efec59fc82a42b948546c6be601e3ab34a87108ecf852aa15f
drwx------ 2 root root 4096 Dec 4 09:30 dc8adf8fa0fc82a56c32efac9d0da5f84153888317c88ab55123d9e71777bc62
我们会发现只有831c...也就是最底层的一个符合,这是因为那是因为docker使用了chainID的方式去保存这些layer,简单来说就是chainID=sha256sum(H(chainID) diffid)
[root@VM_0_15_centos layerdb]# echo -n "sha256:831c5620387fb9efec59fc82a42b948546c6be601e3ab34a87108ecf852aa15f sha256:5fb987d2e54d85820d95d6c31f3fe4cd95bf71fe6d9d9e4684082cb551b728b0 sha256:4fc1aa8003a3d0d2481f10d17773869cbff12c1008df30e0bab8259086a0311c" | sha256sum -
77fcff986d3b13762e4777046b9210a109fda20cb261bd3bbe5d7161d4e73c8e
[root@VM_0_15_centos layerdb]# echo -n "sha256:77fcff986d3b13762e4777046b9210a109fda20cb261bd3bbe5d7161d4e73c8e sha256:4fc1aa8003a3d0d2481f10d17773869cbff12c1008df30e0bab8259086a0311c" | sha256sum -
dc8adf8fa0fc82a56c32efac9d0da5f84153888317c88ab55123d9e71777bc62 -
接着这样就计算出了另外2个,那么里面有用的其实就只有cache-id,查看里面的内容
[root@VM_0_15_centos sha256]# cat 831c5620387fb9efec59fc82a42b948546c6be601e3ab34a87108ecf852aa15f/cache-id
c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
查看ll /var/lib/docker/overlay2/c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
[root@VM_0_15_centos sha256]# ll /var/lib/docker/overlay2/c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
total 8
-rw------- 1 root root 0 Dec 4 09:30 committed
drwxr-xr-x 21 root root 4096 Dec 4 09:30 diff
-rw-r--r-- 1 root root 26 Dec 4 09:30 link
三、/var/lib/docker/overlay2
查看该目录结构
[root@VM_0_15_centos overlay2]# tree -L 2
.
|-- 2eb27304188212e16b609526a72ce4c53a229fb32a8594e33c15990558080ed8
| |-- committed
| |-- diff
| |-- link
| |-- lower
| `-- work
|-- 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298
| |-- diff
| |-- link
| |-- lower
| |-- merged
| `-- work
|-- 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298-init
| |-- committed
| |-- diff
| |-- link
| |-- lower
| `-- work
|-- c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
| |-- committed
| |-- diff
| `-- link
|-- d2bf9687091baddb10648d0d31aa109b9f17a0d3f692aeb3fa4fe70993c2625e
| |-- committed
| |-- diff
| |-- link
| |-- lower
| `-- work
`-- l
|-- IWTEMXLGOJEHS7CQKSWJFT4XRQ -> ../c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138/diff
|-- KNCOQBAWZLDBN5NYMNPYZ5ZJWL -> ../683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298-init/diff
|-- MDG2AFNTJJZDMA5TBJJ4DE5JWP -> ../2eb27304188212e16b609526a72ce4c53a229fb32a8594e33c15990558080ed8/diff
|-- VQUQ2ITD4H3LNWZ62MUGR3VGLO -> ../683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298/diff
`-- ZIMQGNNP6T74NOVF5U3W63YV6V -> ../d2bf9687091baddb10648d0d31aa109b9f17a0d3f692aeb3fa4fe70993c2625e/diff
仔细查看可以发现
2eb27304188212e16b609526a72ce4c53a229fb32a8594e33c15990558080ed8
c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
d2bf9687091baddb10648d0d31aa109b9f17a0d3f692aeb3fa4fe70993c2625e
这三个目录就是我们第二部分在layerdb里面对应sha256/xxx的cache-id
那么我们再来回想一下,也就是我们拉取的nginx镜像由三个layer组成,即上面三个目录,因为docker就是分层拉取的(注意这是存储的概念),你在拉取的时候就能看到。
那么另外两个
683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298-init
683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298是干嘛的,回想一下我们一开始就运行了nginx是不是,所以这两个就是容器层了,
[root@VM_0_15_centos overlay2]# cat 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298-init/lower
l/ZIMQGNNP6T74NOVF5U3W63YV6V:l/MDG2AFNTJJZDMA5TBJJ4DE5JWP:l/IWTEMXLGOJEHS7CQKSWJFT4XRQ
[root@VM_0_15_centos overlay2]# cat 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298/lower
l/KNCOQBAWZLDBN5NYMNPYZ5ZJWL:l/ZIMQGNNP6T74NOVF5U3W63YV6V:l/MDG2AFNTJJZDMA5TBJJ4DE5JWP:l/IWTEMXLGOJEHS7CQKSWJFT4XRQ
init在基础的三层之上683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298在基础的三层再加上init。
为什么要有init层?
还记得hostname /etc/resolve.conf等等吗,对了,容器在启动的时候单独搞了个init层用来干这个活。
docker exec c272a7518041 sh -c "echo 'hellpo'>/root/test"
[root@VM_0_15_centos overlay2]# find . -name "test"
./683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298/merged/root/test
./683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298/diff/root/test
四、最后
我们再来运行一个nginx看看overlay2目录
[root@VM_0_15_centos overlay2]# docker run -d nginx
95340856ead001938ef1b39a880be6a3383fc6e411d0e83b8b54786a66d410bd
[root@VM_0_15_centos overlay2]# ll
total 32
drwx------ 4 root root 4096 Dec 4 09:30 2eb27304188212e16b609526a72ce4c53a229fb32a8594e33c15990558080ed8
drwx------ 5 root root 4096 Dec 4 11:06 680c8de45227b2ba21176b1c25c29a6bb41668c4ad72ee07ee4ab7e1b2f70fae
drwx------ 4 root root 4096 Dec 4 11:06 680c8de45227b2ba21176b1c25c29a6bb41668c4ad72ee07ee4ab7e1b2f70fae-init
drwx------ 5 root root 4096 Dec 4 09:30 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298
drwx------ 4 root root 4096 Dec 4 09:30 683b45065f2c731b1bf889fbc9cee24033cd0d0367d076e546e4d14903dc5298-init
drwx------ 3 root root 4096 Dec 4 09:30 c2fe5d413708a33f7884b3756210580725b055fc446ae3d4278f5cd86acf6138
drwx------ 4 root root 4096 Dec 4 09:30 d2bf9687091baddb10648d0d31aa109b9f17a0d3f692aeb3fa4fe70993c2625e
drwx------ 2 root root 4096 Dec 4 11:06 l
新增了680开头的两个目录,一个是init一个是可读可写层,底层nginx还是那三层,这样就节省了空间了。