Docker容器:文件系统
联合文件系统–UnionFS
Union File System,简称UnionFS,是一种为Linux、FreeBSD、和NetBSD操作系统设计的把其他文件系统联合到一个挂载点的文件系统服务;
联合文件系统是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加, 同时可以将不同目录挂载到同一个虚拟文件系统下;
联合文件系统是Docker镜像的基础;
镜像通过分层来进行继承,基于基础镜像可以制作具体的应用镜像;
不同docker容器可以共享基础的文件系统层,提高了存储效率;
AUFS-Advanced Multi-Layered Unification Filesystem
AUFS is a union filesystem. The aufs storage driver was previously the default storage driver used for managing images and layers on Docker for Ubuntu, and for Debian versions prior to Stretch. If your Linux kernel is version 4.0 or higher, and you use Docker Engine - Community, consider using the newer overlay2, which has potential performance advantages over the aufs storage driver.
AUFS完全重写了早期的UnionFS 1.x,其主要目的是为了可靠性和性能,并且引入了一些新的功能,比如可写分支的负载均衡;
overlay
overlay2是目前docker默认的存储驱动;
OverlayFS is a modern union filesystem that is similar to AUFS, but faster and with a simpler implementation. Docker provides two storage drivers for OverlayFS: the original overlay, and the newer and more stable overlay2.
overlay2
overlayFS将单个主机上的两个目录合并成一个目录;这些目录被称为层;
实践
AUFS是Docker选用的第一种存储驱动;AUFS具有快速启动容器、高效利用存储和内存的优点;
查看docker文件系统
docker info | grep "Storage Driver"
Storage Driver: overlay2
storage drivers
Images and layers
每一个Docker image都是由一系列read-only layer组成;image layer的内容都存储在Docker host filesystem的/var/lib/docker/aufs/diff目录下;
而/var/lib/docker/aufs/layers目录,则存储着image layer如何堆栈这些layer的metadata;
# docker images
# docker history d1f44b02a83a
IMAGE CREATED CREATED BY SIZE COMMENT
d1f44b02a83a 7 weeks ago 1.78 kB
d995a852933b 13 months ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/sh" "... 0 B
f6826b456351 13 months ago /bin/sh -c cat /root/scripts/start_web_bac... 0 B
95013f1114b1 13 months ago /bin/sh -c chmod +x /root/scripts/start_we... 262 B
5de8bc485f44 13 months ago /bin/sh -c #(nop) ADD file:851f59133b035a0... 262 B
064dd85732fd 13 months ago /bin/sh -c mkdir /root/scripts 0 B
8480074cbad5 13 months ago /bin/sh -c chmod +x /usr/bin/web_backend 11.9 MB
a799d9f9fff7 13 months ago /bin/sh -c #(nop) ADD file:78803691ed64c2a... 11.9 MB
67fa590cfc1c 14 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 14 months ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
<missing> 14 months ago /bin/sh -c #(nop) ADD file:4e7247c06de9ad1... 202 MB
Container and layers
Docker使用AUFS的CoW技术来实现image layer共享和减少磁盘空间占用; CoW意味着一旦某个文件只有很小部分有改动,AUFS也需要复制整个文件;
不过,对于一个容器而言,每个image layer最多只需要复制一次,后续的改动都会在第一次拷贝的container layer上进行;
启动一个container的时候,Docker会为其创建一个read-only的init layer,用来存储与这个容器内环境相关的内容; 还会为其创建一个read-write的layer来执行所有写操作;
container layer的mount目录是/var/lib/docker/aufs/mnt; metadata和配置文件都存放在/var/lib/docker/containers/<-container-id>目录中;
container的read-write layer存储在/var/lib/docker/aufs/diff/目录;即使容器停止,这个可读写层仍然存在,所以重启容器不会丢失数据, 只有当一个容器被删除的时候,这个可读写层才会一起删除;
如果要删除文件,AUFS会在container的read-write层生成一个.wh.filename的文件来隐藏所有read-only的filename文件;
refs
https://docs.docker.com/storage/storagedriver/overlayfs-driver/
https://juejin.im/post/6844903574137208839
https://staight.github.io/2019/10/04/%E5%AE%B9%E5%99%A8%E5%AE%9E%E7%8E%B0-overlay2/