高级java每日一道面试题-2027年11月20日-容器与虚拟化题[Dockerj]-Union File System (联合文件系统) 是如何工作的?有哪些实现? - dominant-baseline:middle

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力网 👁️ 258 次阅读

Union File System(联合文件系统)是 Docker 镜像分层存储与容器轻量化的核心技术基石。它解决了“如何让多个容器共享同一份基础镜像,同时又拥有各自独立的可写空间”这一核心问题。以下完全从理论架构层面剖析其工作原理、写时复制机制与主流实现对比。

一、联合文件系统在 Docker 中的核心定位

Union FS 并非单一软件,而是一类文件系统的统称。它在 Docker 架构中处于镜像存储驱动的位置,直接决定了容器启动速度、磁盘占用效率和 I/O 性能。

#mermaid-svg-K7dCbwGgZsmDxtKY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-K7dCbwGgZsmDxtKY .error-icon{fill:#552222;}#mermaid-svg-K7dCbwGgZsmDxtKY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-K7dCbwGgZsmDxtKY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-K7dCbwGgZsmDxtKY .marker.cross{stroke:#333333;}#mermaid-svg-K7dCbwGgZsmDxtKY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-K7dCbwGgZsmDxtKY p{margin:0;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge{stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 text{fill:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth--1{stroke-width:17;}#mermaid-svg-K7dCbwGgZsmDxtKY .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-0{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-0{stroke-width:14;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-1{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-1{stroke-width:11;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 text{fill:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-2{stroke-width:8;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-3{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-3{stroke-width:5;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-4{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-4{stroke-width:2;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-5{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-5{stroke-width:-1;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-6{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-6{stroke-width:-4;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-7{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-7{stroke-width:-7;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-8{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-8{stroke-width:-10;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-9{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-9{stroke-width:-13;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 polygon,#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 text{fill:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .node-icon-10{font-size:40px;color:black;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .edge-depth-10{stroke-width:-16;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled circle,#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:lightgray;}#mermaid-svg-K7dCbwGgZsmDxtKY .disabled text{fill:#efefef;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-root rect,#mermaid-svg-K7dCbwGgZsmDxtKY .section-root path,#mermaid-svg-K7dCbwGgZsmDxtKY .section-root circle,#mermaid-svg-K7dCbwGgZsmDxtKY .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-K7dCbwGgZsmDxtKY .section-root text{fill:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-root span{color:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .section-2 span{color:#ffffff;}#mermaid-svg-K7dCbwGgZsmDxtKY .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-K7dCbwGgZsmDxtKY .edge{fill:none;}#mermaid-svg-K7dCbwGgZsmDxtKY .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-K7dCbwGgZsmDxtKY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Docker 联合文件系统定位镜像分层存储基础层 OS 只读中间层 依赖库 只读应用层 代码 只读容器运行时层容器读写层 R/W写时复制 COW主要功能特性多层目录联合挂载同名文件覆盖遮挡修改在顶层副本进行主流存储驱动Overlay2 默认首选AUFS 历史最早DeviceMapper 块级存储Btrfs/ZFS 高级特性

二、联合文件系统工作原理流程图

核心思想可以概括为:将多个只读目录与一个可写目录合并成单一视图,任何修改动作都只在可写层进行,底层镜像永远不被污染。

#mermaid-svg-ouQ25O8KBo7phzpS{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ouQ25O8KBo7phzpS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ouQ25O8KBo7phzpS .error-icon{fill:#552222;}#mermaid-svg-ouQ25O8KBo7phzpS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ouQ25O8KBo7phzpS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ouQ25O8KBo7phzpS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ouQ25O8KBo7phzpS .marker.cross{stroke:#333333;}#mermaid-svg-ouQ25O8KBo7phzpS svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ouQ25O8KBo7phzpS p{margin:0;}#mermaid-svg-ouQ25O8KBo7phzpS .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ouQ25O8KBo7phzpS .cluster-label text{fill:#333;}#mermaid-svg-ouQ25O8KBo7phzpS .cluster-label span{color:#333;}#mermaid-svg-ouQ25O8KBo7phzpS .cluster-label span p{background-color:transparent;}#mermaid-svg-ouQ25O8KBo7phzpS .label text,#mermaid-svg-ouQ25O8KBo7phzpS span{fill:#333;color:#333;}#mermaid-svg-ouQ25O8KBo7phzpS .node rect,#mermaid-svg-ouQ25O8KBo7phzpS .node circle,#mermaid-svg-ouQ25O8KBo7phzpS .node ellipse,#mermaid-svg-ouQ25O8KBo7phzpS .node polygon,#mermaid-svg-ouQ25O8KBo7phzpS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ouQ25O8KBo7phzpS .rough-node .label text,#mermaid-svg-ouQ25O8KBo7phzpS .node .label text,#mermaid-svg-ouQ25O8KBo7phzpS .image-shape .label,#mermaid-svg-ouQ25O8KBo7phzpS .icon-shape .label{text-anchor:middle;}#mermaid-svg-ouQ25O8KBo7phzpS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ouQ25O8KBo7phzpS .rough-node .label,#mermaid-svg-ouQ25O8KBo7phzpS .node .label,#mermaid-svg-ouQ25O8KBo7phzpS .image-shape .label,#mermaid-svg-ouQ25O8KBo7phzpS .icon-shape .label{text-align:center;}#mermaid-svg-ouQ25O8KBo7phzpS .node.clickable{cursor:pointer;}#mermaid-svg-ouQ25O8KBo7phzpS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ouQ25O8KBo7phzpS .arrowheadPath{fill:#333333;}#mermaid-svg-ouQ25O8KBo7phzpS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ouQ25O8KBo7phzpS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ouQ25O8KBo7phzpS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ouQ25O8KBo7phzpS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ouQ25O8KBo7phzpS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ouQ25O8KBo7phzpS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ouQ25O8KBo7phzpS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ouQ25O8KBo7phzpS .cluster text{fill:#333;}#mermaid-svg-ouQ25O8KBo7phzpS .cluster span{color:#333;}#mermaid-svg-ouQ25O8KBo7phzpS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ouQ25O8KBo7phzpS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ouQ25O8KBo7phzpS rect.text{fill:none;stroke-width:0;}#mermaid-svg-ouQ25O8KBo7phzpS .icon-shape,#mermaid-svg-ouQ25O8KBo7phzpS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ouQ25O8KBo7phzpS .icon-shape p,#mermaid-svg-ouQ25O8KBo7phzpS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ouQ25O8KBo7phzpS .icon-shape .label rect,#mermaid-svg-ouQ25O8KBo7phzpS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ouQ25O8KBo7phzpS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ouQ25O8KBo7phzpS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ouQ25O8KBo7phzpS :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Union Mount Point容器读写层 UpperDir镜像只读层 LowerDir用户修改 /etc/config.ini用户读取 /bin/bash基础层: 操作系统文件如 Ubuntu Rootfs依赖层: 运行时环境如 OpenJDK 11应用层: 业务代码如 app.jar可写层: 日志/临时文件容器停止后保留容器内可见的统一文件系统视图

视角统一逻辑:容器内进程看到的是 L1 + L2 + L3 + U1 四层目录的叠加合并结果。如果上层目录存在与下层同路径的文件,则上层覆盖下层,即“白障遮蔽”效应。

三、核心机制:写时复制(COW)详解

写时复制是联合文件系统最关键的资源节约技术。它确保多个容器共享同一份基础镜像文件,直到某个容器需要修改该文件时才进行私有拷贝。

只读层 LowerDir读写层 UpperDir联合挂载点容器内进程只读层 LowerDir读写层 UpperDir联合挂载点容器内进程#mermaid-svg-w9pqImoQAkQfX5P9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-w9pqImoQAkQfX5P9 .error-icon{fill:#552222;}#mermaid-svg-w9pqImoQAkQfX5P9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w9pqImoQAkQfX5P9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w9pqImoQAkQfX5P9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w9pqImoQAkQfX5P9 .marker.cross{stroke:#333333;}#mermaid-svg-w9pqImoQAkQfX5P9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w9pqImoQAkQfX5P9 p{margin:0;}#mermaid-svg-w9pqImoQAkQfX5P9 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-w9pqImoQAkQfX5P9 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-w9pqImoQAkQfX5P9 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-w9pqImoQAkQfX5P9 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-w9pqImoQAkQfX5P9 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-w9pqImoQAkQfX5P9 .sequenceNumber{fill:white;}#mermaid-svg-w9pqImoQAkQfX5P9 #sequencenumber{fill:#333;}#mermaid-svg-w9pqImoQAkQfX5P9 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-w9pqImoQAkQfX5P9 .messageText{fill:#333;stroke:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-w9pqImoQAkQfX5P9 .labelText,#mermaid-svg-w9pqImoQAkQfX5P9 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .loopText,#mermaid-svg-w9pqImoQAkQfX5P9 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-w9pqImoQAkQfX5P9 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-w9pqImoQAkQfX5P9 .noteText,#mermaid-svg-w9pqImoQAkQfX5P9 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-w9pqImoQAkQfX5P9 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-w9pqImoQAkQfX5P9 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-w9pqImoQAkQfX5P9 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-w9pqImoQAkQfX5P9 .actorPopupMenu{position:absolute;}#mermaid-svg-w9pqImoQAkQfX5P9 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-w9pqImoQAkQfX5P9 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-w9pqImoQAkQfX5P9 .actor-man circle,#mermaid-svg-w9pqImoQAkQfX5P9 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-w9pqImoQAkQfX5P9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}场景A: 纯只读操作(无开销)场景B: 首次写入(触发COW)后续读取时,上层文件覆盖下层读取 /lib/jdk-11.jar查找到文件返回文件内容直接读取底层数据(零拷贝)修改 /etc/hosts 文件检查文件是否存在于只读层文件位于 Layer 2将文件从下层拷贝到读写层拷贝完成在读写层修改文件内容修改成功再次读取 /etc/hosts直接在读写层命中(已拷贝)返回修改后的内容

COW 的理论价值:

启动速度:启动 100 个 Redis 容器,底层基础镜像层只需要一份,无需反复拷贝。磁盘节约:每个容器仅存储增量修改(通常仅数 MB),而非完整操作系统副本。不可变性保障:镜像层一旦构建就不可修改,天然支持版本溯源和回滚。

四、主流 Union FS 实现对比表

实现方案内核支持镜像层数限制性能特征Docker 使用历史Overlay2Linux Kernel >= 4.0(现代默认)最多 128 层 LowerDir基于节点索引,内存占用低,读写性能优异当前默认推荐驱动AUFS非主线内核(需补丁)最多 127 层Docker 最早的联合文件系统,成熟稳定但未进主线早期 Ubuntu 默认,现已废弃DeviceMapper内核原生 LVM 支持无层数限制块级快照技术,性能受预分配空间影响较大CentOS / RHEL 曾默认使用Btrfs内核原生利用子卷/快照支持写时复制、压缩、校验等高级特性适合高级用户,非默认ZFS需额外安装模块类似 Btrfs企业级特性丰富(压缩/去重/快照克隆)适合 NAS/存储密集型场景OverlayFS (v1)Linux Kernel >= 3.18仅 2 层(单 LowerDir)Overlay2 的前身,已被完全取代已淘汰VFS (experimental)内核原生不限基于原生文件系统的简单叠加实现实验阶段,非生产可用

五、Overlay2 工作原理详解(当前主流方案)

Overlay2 是当前 Docker 的默认存储驱动,它将联合文件系统的概念具象化为四个核心目录。

#mermaid-svg-BlzcThe6n8SCapJ0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BlzcThe6n8SCapJ0 .error-icon{fill:#552222;}#mermaid-svg-BlzcThe6n8SCapJ0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BlzcThe6n8SCapJ0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BlzcThe6n8SCapJ0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BlzcThe6n8SCapJ0 .marker.cross{stroke:#333333;}#mermaid-svg-BlzcThe6n8SCapJ0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BlzcThe6n8SCapJ0 p{margin:0;}#mermaid-svg-BlzcThe6n8SCapJ0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster-label text{fill:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster-label span{color:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster-label span p{background-color:transparent;}#mermaid-svg-BlzcThe6n8SCapJ0 .label text,#mermaid-svg-BlzcThe6n8SCapJ0 span{fill:#333;color:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 .node rect,#mermaid-svg-BlzcThe6n8SCapJ0 .node circle,#mermaid-svg-BlzcThe6n8SCapJ0 .node ellipse,#mermaid-svg-BlzcThe6n8SCapJ0 .node polygon,#mermaid-svg-BlzcThe6n8SCapJ0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-BlzcThe6n8SCapJ0 .rough-node .label text,#mermaid-svg-BlzcThe6n8SCapJ0 .node .label text,#mermaid-svg-BlzcThe6n8SCapJ0 .image-shape .label,#mermaid-svg-BlzcThe6n8SCapJ0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-BlzcThe6n8SCapJ0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-BlzcThe6n8SCapJ0 .rough-node .label,#mermaid-svg-BlzcThe6n8SCapJ0 .node .label,#mermaid-svg-BlzcThe6n8SCapJ0 .image-shape .label,#mermaid-svg-BlzcThe6n8SCapJ0 .icon-shape .label{text-align:center;}#mermaid-svg-BlzcThe6n8SCapJ0 .node.clickable{cursor:pointer;}#mermaid-svg-BlzcThe6n8SCapJ0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-BlzcThe6n8SCapJ0 .arrowheadPath{fill:#333333;}#mermaid-svg-BlzcThe6n8SCapJ0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-BlzcThe6n8SCapJ0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-BlzcThe6n8SCapJ0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-BlzcThe6n8SCapJ0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-BlzcThe6n8SCapJ0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-BlzcThe6n8SCapJ0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster text{fill:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 .cluster span{color:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-BlzcThe6n8SCapJ0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-BlzcThe6n8SCapJ0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-BlzcThe6n8SCapJ0 .icon-shape,#mermaid-svg-BlzcThe6n8SCapJ0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-BlzcThe6n8SCapJ0 .icon-shape p,#mermaid-svg-BlzcThe6n8SCapJ0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-BlzcThe6n8SCapJ0 .icon-shape .label rect,#mermaid-svg-BlzcThe6n8SCapJ0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-BlzcThe6n8SCapJ0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-BlzcThe6n8SCapJ0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-BlzcThe6n8SCapJ0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Overlay2 四层目录模型叠加合并顶层写入辅助协调LowerDir多个只读镜像层/var/lib/docker/overlay2/l/XXXMergedDir联合挂载后的统一视图容器内进程看到的 rootfsUpperDir容器唯一可写层/var/lib/docker/overlay2/YYY/diffWorkDirOverlay2 内部工作目录用于原子操作准备

Overlay2 关键设计点:

LowerDir 是一个目录列表,按优先级从低到高排列。多个 Lower 层的文件如果同路径,则更高优先级层的文件生效。WorkDir 是临时工作区,与 UpperDir 必须在同一文件系统,用于保证写操作的原子性。Merged 是最终呈现在容器内的视图。容器内的 / 实际上就是宿主机的这个 Merged 目录。

六、不同实现的技术特点思维导图 拼多多帮助力入口

#mermaid-svg-GPjG8rIS8csRQMvw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GPjG8rIS8csRQMvw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GPjG8rIS8csRQMvw .error-icon{fill:#552222;}#mermaid-svg-GPjG8rIS8csRQMvw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GPjG8rIS8csRQMvw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GPjG8rIS8csRQMvw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GPjG8rIS8csRQMvw .marker.cross{stroke:#333333;}#mermaid-svg-GPjG8rIS8csRQMvw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GPjG8rIS8csRQMvw p{margin:0;}#mermaid-svg-GPjG8rIS8csRQMvw .edge{stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .section--1 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section--1 path,#mermaid-svg-GPjG8rIS8csRQMvw .section--1 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section--1 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section--1 text{fill:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth--1{stroke-width:17;}#mermaid-svg-GPjG8rIS8csRQMvw .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-0 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-0 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-0 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-0 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-0 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-0{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-0{stroke-width:14;}#mermaid-svg-GPjG8rIS8csRQMvw .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-1 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-1 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-1 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-1 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-1 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-1{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-1{stroke-width:11;}#mermaid-svg-GPjG8rIS8csRQMvw .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-2 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-2 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-2 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-2 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-2 text{fill:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-2{stroke-width:8;}#mermaid-svg-GPjG8rIS8csRQMvw .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-3 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-3 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-3 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-3 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-3 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-3{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-3{stroke-width:5;}#mermaid-svg-GPjG8rIS8csRQMvw .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-4 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-4 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-4 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-4 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-4 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-4{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-4{stroke-width:2;}#mermaid-svg-GPjG8rIS8csRQMvw .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-5 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-5 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-5 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-5 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-5 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-5{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-5{stroke-width:-1;}#mermaid-svg-GPjG8rIS8csRQMvw .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-6 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-6 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-6 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-6 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-6 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-6{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-6{stroke-width:-4;}#mermaid-svg-GPjG8rIS8csRQMvw .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-7 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-7 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-7 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-7 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-7 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-7{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-7{stroke-width:-7;}#mermaid-svg-GPjG8rIS8csRQMvw .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-8 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-8 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-8 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-8 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-8 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-8{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-8{stroke-width:-10;}#mermaid-svg-GPjG8rIS8csRQMvw .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-9 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-9 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-9 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-9 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-9 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-9{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-9{stroke-width:-13;}#mermaid-svg-GPjG8rIS8csRQMvw .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-10 rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-10 path,#mermaid-svg-GPjG8rIS8csRQMvw .section-10 circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-10 polygon,#mermaid-svg-GPjG8rIS8csRQMvw .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-10 text{fill:black;}#mermaid-svg-GPjG8rIS8csRQMvw .node-icon-10{font-size:40px;color:black;}#mermaid-svg-GPjG8rIS8csRQMvw .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .edge-depth-10{stroke-width:-16;}#mermaid-svg-GPjG8rIS8csRQMvw .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled,#mermaid-svg-GPjG8rIS8csRQMvw .disabled circle,#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:lightgray;}#mermaid-svg-GPjG8rIS8csRQMvw .disabled text{fill:#efefef;}#mermaid-svg-GPjG8rIS8csRQMvw .section-root rect,#mermaid-svg-GPjG8rIS8csRQMvw .section-root path,#mermaid-svg-GPjG8rIS8csRQMvw .section-root circle,#mermaid-svg-GPjG8rIS8csRQMvw .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-GPjG8rIS8csRQMvw .section-root text{fill:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .section-root span{color:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .section-2 span{color:#ffffff;}#mermaid-svg-GPjG8rIS8csRQMvw .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-GPjG8rIS8csRQMvw .edge{fill:none;}#mermaid-svg-GPjG8rIS8csRQMvw .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-GPjG8rIS8csRQMvw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Union FS 实现方案对比Overlay2 现代标准内核原生支持最多128层叠加Node-based 索引机制SELinux 兼容性好AUFS 历史先驱非内核主线代码早期 Docker 唯一选择内存占用较大适合老旧内核系统DeviceMapper 块级方案基于 LVM 技术使用稀疏文件需预分配空间大小thin-pool 模式成熟Btrfs_ZFS 高级特性原生写时复制支持压缩与去重支持快照与克隆运维复杂度较高

七、面试高阶考点速查表

考察点深度问题示例理论回答要点层数限制与性能Overlay2 的 128 层限制意味着什么?如何优化?镜像构建扁平化:过多的 RUN 指令会产生大量无用中间层。通过多阶段构建和合并 RUN 命令减少层数,既能规避 128 层硬限制,也能降低元数据读写开销。写操作性能瓶颈容器内大量随机写为什么会变慢?COW 的首次写惩罚:首次写入会触发文件从 LowerDir 到 UpperDir 的全量拷贝,大文件(如数据库数据文件)的首次写开销极高。解决思路是使用 Volume 绕过文件系统栈直接挂载宿主机目录。跨文件系统兼容Overlay2 要求 UpperDir 和 WorkDir 在同一文件系统,为什么?原子 rename 保证:WorkDir 通过在同一文件系统内的 rename 操作实现原子性切换(Linux 要求跨文件系统不能使用 rename)。如果分在不同挂载点,Overlay2 将无法启动。磁盘空间泄漏删除容器后,磁盘空间没有完全释放?镜像层共享机制:基础镜像层可能还被其他容器或镜像引用。Docker 使用引用计数机制,只有引用计数归零的层才会在 docker system prune 时真正释放。镜像瘦身原理为什么基础镜像选择 Alpine 而非 CentOS?层体积传递效应:联合文件系统的总大小是所有层体积之和。基础层尺寸直接影响最终镜像体积。Alpine(约 5MB)比 CentOS(约 200MB)在不包含业务代码的情况下已经节约了 195MB。

八、总结

联合文件系统的理论核心可浓缩为三点:

分层叠加:多个只读目录与一个可写目录合并为统一视图,上层覆下层。写时复制:共享读取,私密写入。未修改的文件所有容器共享一份底层数据;修改发生时才将目标文件拷贝到私有层。实现多样:Overlay2 是当下的最佳实践,AUFS 是历史的铺路石,DeviceMapper 与 Btrfs/ZFS 在特定场景仍有独特价值。

理解 Union FS 的本质,是解释 Docker 为何“启动快、镜像小、部署灵活”的关键理论依据。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧