系列文章:
Docker镜像存储-overlayfs
docker背景知识3 镜像分层(overlay2)
Docker存储驱动之–overlay2
1. docker支持哪些分层文件系统
当前docker支持的分层文件系统参见下表
Linux发行版本 | 推荐使用的存储系统 | 可替代的其他技术 |
---|---|---|
Ubuntu | overlay2 or aufs | overlay, devicemapper, zfs, vfs |
Debian | overlay2, aufs or devicemapper | overlay, vfs |
CentOS | overlay2 | overlay, devicemapper, zfs, vfs |
Fedora | overlay2 | overlay, devicemapper, zfs, vfs |
linux overlay filesystem
在overlayFS,中底层目录叫lowerdir,顶层目录叫upperdir,对外统一提供服务的是merged,当修改下层文件时,会触发写时复制
2. 探索overlayFS
❯ cd /root/test/test
❯ mkdir layer1 layer2 upperlayer workdir mountedfs
❯ echo "content for file1.txt in layer1" > layer1/file1.txt
❯ echo "content for file2.txt in layer1" > layer1/file2.txt
❯ echo "content for file1.txt in layer2" > layer2/file1.txt
❯ echo "content for file3.txt in layer2" > layer2/file3.txt
此时的目录结构:
[root@localhost test]# pwd
/root/test/test
[root@localhost test]# tree
.
├── layer1
│ ├── file1.txt
│ └── file2.txt
├── layer2
│ ├── file1.txt
│ └── file3.txt
├── mountedfs
├── upperlayer
└── workdir
挂载生成一个overlayFS
sudo mount -t overlay -o lowerdir=layer1:layer2,upperdir=upperlayer,workdir=workdir overlay mountedfs
此时的目录结构:
[root@localhost test]# pwd
/root/test/test
[root@localhost test]# tree
.
├── layer1
│ ├── file1.txt
│ └── file2.txt
├── layer2
│ ├── file1.txt
│ └── file3.txt
├── mountedfs
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
├── upperlayer
└── workdir
└── work
挂载后查看所有目录, mountedfs中有三个文件,查看文件file1发现使用的是上层layer1中的
❯ ls -l * '类似tree,显示全部文件列表,包含子目录,只不过是平铺的'
layer1:
total 8
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:41 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file2.txt
layer2:
total 8
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:41 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file3.txt
//merged层,2个同名的file1.txt过滤为1个
mountedfs:
total 12
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:41 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file2.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file3.txt
//upperdir目前为空,此层为复制层,没有产生复制文件
upperlayer:
total 0
workdir:
total 0
d--------- 2 root root 6 Apr 13 00:39 work
//mountedfs/file1.txt 发现使用的是底层layer1中的,为何使用的是layer1中的呢?因为lowerdir=layer1:layer2中layer1在layer2前,保留先出现的
❯ cat mountedfs/file1.txt
content for file1.txt in layer1
修改mountedfs/file1.txt
❯ echo newfile in mountedfs > mountedfs/file1.txt
再次查看所有目录,发现upperlayer中多一个file1.txt,其中内容为newfile,layer1还是原值
❯ ls -l *
layer1:
total 8
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:41 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file2.txt
layer2:
total 8
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:41 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file3.txt
mountedfs:
total 12
-rw-r--r-- 1 wsl zdevel 21 Apr 13 00:44 file1.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file2.txt
-rw-r--r-- 1 wsl zdevel 32 Apr 13 00:37 file3.txt
//复制层,产生一个文件
upperlayer:
total 4
-rw-r--r-- 1 wsl zdevel 21 Apr 13 00:44 file1.txt
workdir:
total 0
d--------- 2 root root 6 Apr 13 00:44 work
//查看变化,生效了
❯ cat mountedfs/file1.txt
newfile in mountedfs
//复制层的内容就是 mountedfs/file1.txt
❯ cat upperlayer/file1.txt
newfile in mountedfs
//layer1中的原始文件没变,说明修改下层文件时,会触发写时复制
❯ cat layer1/file1.txt
content for file1.txt in layer1
3. docker中的使用
3.1 在docker中启用overlayfs
查看当前版本的docker采用的分层类型:
❯ cat /etc/docker/daemon.json
{
"storage-driver": "overlay2", '说明分层类型是overlay2'
"insecure-registries": ["0.0.0.0/0"],
"registry-mirrors": ["https://f0z3hvz3.mirror.aliyuncs.com"]
}
3.2 查看容器的overlay挂载情况
❯ sudo docker inspect xxxDockerId
...
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/fd7a593c88623dbbeb12a897d5d293751d11a17a4f720ddfe83a1f381271686c-init/diff:/var/lib/docker/overlay2/064fae2fb1a163e591db5c9d6f3829b0beee31c079687fff523a52a712fea746/diff:/var/lib/dock
er/overlay2/254f7f0509dfae6f4a11c1f11149ce1367ccc3415f5727ce2e75d5891f3de68e/diff",
"MergedDir": "/var/lib/docker/overlay2/fd7a593c88623dbbeb12a897d5d293751d11a17a4f720ddfe83a1f381271686c/merged",
"UpperDir": "/var/lib/docker/overlay2/fd7a593c88623dbbeb12a897d5d293751d11a17a4f720ddfe83a1f381271686c/diff",
"WorkDir": "/var/lib/docker/overlay2/fd7a593c88623dbbeb12a897d5d293751d11a17a4f720ddfe83a1f381271686c/work"
},
"Name": "overlay2"
},
...