容器间共享数据是实现微服务协作、Sidecar 模式、日志集中采集等需求的基础。Docker 提供了多种机制让多个容器访问同一份数据,需根据共享范围、生命周期、性能要求和耦合程度选择合适方案。
一、容器共享数据的核心方式
#mermaid-svg-Hh8gzlPXjEattvfQ{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-Hh8gzlPXjEattvfQ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Hh8gzlPXjEattvfQ .error-icon{fill:#552222;}#mermaid-svg-Hh8gzlPXjEattvfQ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Hh8gzlPXjEattvfQ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Hh8gzlPXjEattvfQ .marker.cross{stroke:#333333;}#mermaid-svg-Hh8gzlPXjEattvfQ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Hh8gzlPXjEattvfQ p{margin:0;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge{stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 text{fill:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth--1{stroke-width:17;}#mermaid-svg-Hh8gzlPXjEattvfQ .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-0{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-0{stroke-width:14;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-1{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-1{stroke-width:11;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 text{fill:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-2{stroke-width:8;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-3{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-3{stroke-width:5;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-4{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-4{stroke-width:2;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-5{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-5{stroke-width:-1;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-6{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-6{stroke-width:-4;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-7{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-7{stroke-width:-7;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-8{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-8{stroke-width:-10;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-9{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-9{stroke-width:-13;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 polygon,#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 text{fill:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .node-icon-10{font-size:40px;color:black;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .edge-depth-10{stroke-width:-16;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled circle,#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:lightgray;}#mermaid-svg-Hh8gzlPXjEattvfQ .disabled text{fill:#efefef;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-root rect,#mermaid-svg-Hh8gzlPXjEattvfQ .section-root path,#mermaid-svg-Hh8gzlPXjEattvfQ .section-root circle,#mermaid-svg-Hh8gzlPXjEattvfQ .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-Hh8gzlPXjEattvfQ .section-root text{fill:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-root span{color:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .section-2 span{color:#ffffff;}#mermaid-svg-Hh8gzlPXjEattvfQ .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-Hh8gzlPXjEattvfQ .edge{fill:none;}#mermaid-svg-Hh8gzlPXjEattvfQ .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-Hh8gzlPXjEattvfQ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 容器间共享数据
方式一:共享 Volume
多个容器挂载同一个命名 Volume
数据由 Docker 管理
生命周期独立于容器
方式二:共享 Bind Mount
多个容器挂载宿主机同一目录
直接依赖宿主文件路径
方式三:数据卷容器
一个专用容器创建并持有 Volume
其他容器通过 --volumes-from 引用
方式四:网络文件系统
通过 NFS / Ceph / GlusterFS 挂载
跨主机共享
方式五:Sidecar 模式
一个业务容器 + 一个辅助容器
共享同一个 Volume 或网络命名空间
二、各种共享方式的原理与架构
1. 共享命名 Volume(推荐)
多容器启动时均引用同一个命名 Volume,Docker 将它们指向宿主机相同的物理目录。
#mermaid-svg-EwAwMXqfOTJpjNGe{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-EwAwMXqfOTJpjNGe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EwAwMXqfOTJpjNGe .error-icon{fill:#552222;}#mermaid-svg-EwAwMXqfOTJpjNGe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EwAwMXqfOTJpjNGe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EwAwMXqfOTJpjNGe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EwAwMXqfOTJpjNGe .marker.cross{stroke:#333333;}#mermaid-svg-EwAwMXqfOTJpjNGe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EwAwMXqfOTJpjNGe p{margin:0;}#mermaid-svg-EwAwMXqfOTJpjNGe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster-label text{fill:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster-label span{color:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster-label span p{background-color:transparent;}#mermaid-svg-EwAwMXqfOTJpjNGe .label text,#mermaid-svg-EwAwMXqfOTJpjNGe span{fill:#333;color:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe .node rect,#mermaid-svg-EwAwMXqfOTJpjNGe .node circle,#mermaid-svg-EwAwMXqfOTJpjNGe .node ellipse,#mermaid-svg-EwAwMXqfOTJpjNGe .node polygon,#mermaid-svg-EwAwMXqfOTJpjNGe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EwAwMXqfOTJpjNGe .rough-node .label text,#mermaid-svg-EwAwMXqfOTJpjNGe .node .label text,#mermaid-svg-EwAwMXqfOTJpjNGe .image-shape .label,#mermaid-svg-EwAwMXqfOTJpjNGe .icon-shape .label{text-anchor:middle;}#mermaid-svg-EwAwMXqfOTJpjNGe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EwAwMXqfOTJpjNGe .rough-node .label,#mermaid-svg-EwAwMXqfOTJpjNGe .node .label,#mermaid-svg-EwAwMXqfOTJpjNGe .image-shape .label,#mermaid-svg-EwAwMXqfOTJpjNGe .icon-shape .label{text-align:center;}#mermaid-svg-EwAwMXqfOTJpjNGe .node.clickable{cursor:pointer;}#mermaid-svg-EwAwMXqfOTJpjNGe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EwAwMXqfOTJpjNGe .arrowheadPath{fill:#333333;}#mermaid-svg-EwAwMXqfOTJpjNGe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EwAwMXqfOTJpjNGe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EwAwMXqfOTJpjNGe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EwAwMXqfOTJpjNGe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EwAwMXqfOTJpjNGe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EwAwMXqfOTJpjNGe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster text{fill:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe .cluster span{color:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe 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-EwAwMXqfOTJpjNGe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EwAwMXqfOTJpjNGe rect.text{fill:none;stroke-width:0;}#mermaid-svg-EwAwMXqfOTJpjNGe .icon-shape,#mermaid-svg-EwAwMXqfOTJpjNGe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EwAwMXqfOTJpjNGe .icon-shape p,#mermaid-svg-EwAwMXqfOTJpjNGe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EwAwMXqfOTJpjNGe .icon-shape .label rect,#mermaid-svg-EwAwMXqfOTJpjNGe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EwAwMXqfOTJpjNGe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EwAwMXqfOTJpjNGe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EwAwMXqfOTJpjNGe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 容器B
容器A
宿主机
命名 Volume
/var/lib/docker/volumes/shared/_data
挂载点 /app/data
挂载点 /app/data
特点:数据完全由 Docker 管理,容器之间权限统一,生命周期独立。
2. 共享 Bind Mount
所有容器将宿主机的同一绝对路径挂载到内部。
#mermaid-svg-pojj7NQg45OeiEZz{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-pojj7NQg45OeiEZz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-pojj7NQg45OeiEZz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-pojj7NQg45OeiEZz .error-icon{fill:#552222;}#mermaid-svg-pojj7NQg45OeiEZz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pojj7NQg45OeiEZz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-pojj7NQg45OeiEZz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pojj7NQg45OeiEZz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pojj7NQg45OeiEZz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-pojj7NQg45OeiEZz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pojj7NQg45OeiEZz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pojj7NQg45OeiEZz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pojj7NQg45OeiEZz .marker.cross{stroke:#333333;}#mermaid-svg-pojj7NQg45OeiEZz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pojj7NQg45OeiEZz p{margin:0;}#mermaid-svg-pojj7NQg45OeiEZz .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-pojj7NQg45OeiEZz .cluster-label text{fill:#333;}#mermaid-svg-pojj7NQg45OeiEZz .cluster-label span{color:#333;}#mermaid-svg-pojj7NQg45OeiEZz .cluster-label span p{background-color:transparent;}#mermaid-svg-pojj7NQg45OeiEZz .label text,#mermaid-svg-pojj7NQg45OeiEZz span{fill:#333;color:#333;}#mermaid-svg-pojj7NQg45OeiEZz .node rect,#mermaid-svg-pojj7NQg45OeiEZz .node circle,#mermaid-svg-pojj7NQg45OeiEZz .node ellipse,#mermaid-svg-pojj7NQg45OeiEZz .node polygon,#mermaid-svg-pojj7NQg45OeiEZz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-pojj7NQg45OeiEZz .rough-node .label text,#mermaid-svg-pojj7NQg45OeiEZz .node .label text,#mermaid-svg-pojj7NQg45OeiEZz .image-shape .label,#mermaid-svg-pojj7NQg45OeiEZz .icon-shape .label{text-anchor:middle;}#mermaid-svg-pojj7NQg45OeiEZz .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-pojj7NQg45OeiEZz .rough-node .label,#mermaid-svg-pojj7NQg45OeiEZz .node .label,#mermaid-svg-pojj7NQg45OeiEZz .image-shape .label,#mermaid-svg-pojj7NQg45OeiEZz .icon-shape .label{text-align:center;}#mermaid-svg-pojj7NQg45OeiEZz .node.clickable{cursor:pointer;}#mermaid-svg-pojj7NQg45OeiEZz .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-pojj7NQg45OeiEZz .arrowheadPath{fill:#333333;}#mermaid-svg-pojj7NQg45OeiEZz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-pojj7NQg45OeiEZz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-pojj7NQg45OeiEZz .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pojj7NQg45OeiEZz .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-pojj7NQg45OeiEZz .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pojj7NQg45OeiEZz .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-pojj7NQg45OeiEZz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-pojj7NQg45OeiEZz .cluster text{fill:#333;}#mermaid-svg-pojj7NQg45OeiEZz .cluster span{color:#333;}#mermaid-svg-pojj7NQg45OeiEZz 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-pojj7NQg45OeiEZz .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-pojj7NQg45OeiEZz rect.text{fill:none;stroke-width:0;}#mermaid-svg-pojj7NQg45OeiEZz .icon-shape,#mermaid-svg-pojj7NQg45OeiEZz .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pojj7NQg45OeiEZz .icon-shape p,#mermaid-svg-pojj7NQg45OeiEZz .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-pojj7NQg45OeiEZz .icon-shape .label rect,#mermaid-svg-pojj7NQg45OeiEZz .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pojj7NQg45OeiEZz .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-pojj7NQg45OeiEZz .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-pojj7NQg45OeiEZz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 容器B
容器A
宿主机
宿主机目录
/mnt/shared
挂载点 /data
挂载点 /data
特点:直接映射,适合开发环境热加载代码。但生产环境移植性差,且宿主机文件权限会影响容器。
3. 数据卷容器(Data Volume Container)
创建一个"数据卷容器",它声明并挂载一个或多个 Volume,其他容器通过 --volumes-from 参数继承这些挂载点,而不必显式声明 Volume。
应用容器 2 应用容器 1 数据卷容器 Docker Admin 应用容器 2 应用容器 1 数据卷容器 Docker Admin #mermaid-svg-2p1ygdpb8cNG3zqg{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-2p1ygdpb8cNG3zqg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2p1ygdpb8cNG3zqg .error-icon{fill:#552222;}#mermaid-svg-2p1ygdpb8cNG3zqg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2p1ygdpb8cNG3zqg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2p1ygdpb8cNG3zqg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2p1ygdpb8cNG3zqg .marker.cross{stroke:#333333;}#mermaid-svg-2p1ygdpb8cNG3zqg svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2p1ygdpb8cNG3zqg p{margin:0;}#mermaid-svg-2p1ygdpb8cNG3zqg .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-2p1ygdpb8cNG3zqg text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-2p1ygdpb8cNG3zqg .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-2p1ygdpb8cNG3zqg .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-2p1ygdpb8cNG3zqg #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-2p1ygdpb8cNG3zqg .sequenceNumber{fill:white;}#mermaid-svg-2p1ygdpb8cNG3zqg #sequencenumber{fill:#333;}#mermaid-svg-2p1ygdpb8cNG3zqg #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-2p1ygdpb8cNG3zqg .messageText{fill:#333;stroke:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-2p1ygdpb8cNG3zqg .labelText,#mermaid-svg-2p1ygdpb8cNG3zqg .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .loopText,#mermaid-svg-2p1ygdpb8cNG3zqg .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .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-2p1ygdpb8cNG3zqg .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-2p1ygdpb8cNG3zqg .noteText,#mermaid-svg-2p1ygdpb8cNG3zqg .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-2p1ygdpb8cNG3zqg .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-2p1ygdpb8cNG3zqg .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-2p1ygdpb8cNG3zqg .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-2p1ygdpb8cNG3zqg .actorPopupMenu{position:absolute;}#mermaid-svg-2p1ygdpb8cNG3zqg .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-2p1ygdpb8cNG3zqg .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-2p1ygdpb8cNG3zqg .actor-man circle,#mermaid-svg-2p1ygdpb8cNG3zqg line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-2p1ygdpb8cNG3zqg :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 容器仅声明 Volume,可以不运行 两者共享同一个 Volume 创建数据卷容器docker create -v /data --name data-store启动应用容器1docker run --volumes-from data-store ...启动应用容器2docker run --volumes-from data-store ...
特点:一种便捷的共享模式,尤其适合备份、迁移场景。数据卷容器可以负责数据的导入导出、备份任务。
4. 网络文件系统(NFS 等)共享
使用 Docker Volume 驱动,挂载远程 NFS 共享或 CephFS,容器可在不同宿主机上访问同一份数据。
#mermaid-svg-pzi2IoPkXfhA8MA8{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-pzi2IoPkXfhA8MA8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-pzi2IoPkXfhA8MA8 .error-icon{fill:#552222;}#mermaid-svg-pzi2IoPkXfhA8MA8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pzi2IoPkXfhA8MA8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .marker.cross{stroke:#333333;}#mermaid-svg-pzi2IoPkXfhA8MA8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pzi2IoPkXfhA8MA8 p{margin:0;}#mermaid-svg-pzi2IoPkXfhA8MA8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster-label text{fill:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster-label span{color:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster-label span p{background-color:transparent;}#mermaid-svg-pzi2IoPkXfhA8MA8 .label text,#mermaid-svg-pzi2IoPkXfhA8MA8 span{fill:#333;color:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .node rect,#mermaid-svg-pzi2IoPkXfhA8MA8 .node circle,#mermaid-svg-pzi2IoPkXfhA8MA8 .node ellipse,#mermaid-svg-pzi2IoPkXfhA8MA8 .node polygon,#mermaid-svg-pzi2IoPkXfhA8MA8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .rough-node .label text,#mermaid-svg-pzi2IoPkXfhA8MA8 .node .label text,#mermaid-svg-pzi2IoPkXfhA8MA8 .image-shape .label,#mermaid-svg-pzi2IoPkXfhA8MA8 .icon-shape .label{text-anchor:middle;}#mermaid-svg-pzi2IoPkXfhA8MA8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .rough-node .label,#mermaid-svg-pzi2IoPkXfhA8MA8 .node .label,#mermaid-svg-pzi2IoPkXfhA8MA8 .image-shape .label,#mermaid-svg-pzi2IoPkXfhA8MA8 .icon-shape .label{text-align:center;}#mermaid-svg-pzi2IoPkXfhA8MA8 .node.clickable{cursor:pointer;}#mermaid-svg-pzi2IoPkXfhA8MA8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .arrowheadPath{fill:#333333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pzi2IoPkXfhA8MA8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-pzi2IoPkXfhA8MA8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pzi2IoPkXfhA8MA8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster text{fill:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 .cluster span{color:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 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-pzi2IoPkXfhA8MA8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-pzi2IoPkXfhA8MA8 rect.text{fill:none;stroke-width:0;}#mermaid-svg-pzi2IoPkXfhA8MA8 .icon-shape,#mermaid-svg-pzi2IoPkXfhA8MA8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-pzi2IoPkXfhA8MA8 .icon-shape p,#mermaid-svg-pzi2IoPkXfhA8MA8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-pzi2IoPkXfhA8MA8 .icon-shape .label rect,#mermaid-svg-pzi2IoPkXfhA8MA8 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-pzi2IoPkXfhA8MA8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-pzi2IoPkXfhA8MA8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-pzi2IoPkXfhA8MA8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Host2
Host1
读/写
读/写
容器 A
NFS Volume 插件
NFS 服务器
容器 B
NFS Volume 插件
特点:跨主机共享的唯一方案,但需要考虑网络延迟、锁定机制和性能影响。
三、不同共享方式的对比
| 共享方式 | 跨主机 | 数据持久性 | 管理复杂度 | 性能 | 适用场景 |
|---|---|---|---|---|---|
| 命名 Volume | 否(单机) | 持久,容器删除保留 | 低 | 高(本地文件系统) | 同主机多容器协作,如应用+日志采集侧车。 |
| Bind Mount | 否 | 持久,依赖宿主机 | 低(但路径耦合) | 高 | 开发环境共享源码、配置文件。 |
| 数据卷容器 | 否 | 持久,但依赖卷容器 | 中 | 高 | 容器间传递备份、迁移数据;简化 Volume 管理。 |
| NFS Volume 驱动 | 是 | 持久,由远端存储保证 | 高(需 NFS 基础设施) | 中(网络 I/O) | 多节点微服务需要共享文件存储(如上传图片)。 |
| tmpfs | 否 | 否(内存临时) | 低 | 极高(内存) | 进程间高速缓存共享(需共享网络命名空间),不常见。 |
四、Sidecar 模式中的数据共享
在微服务架构中,Sidecar 是最典型的容器共享数据场景。例如,业务容器将日志写入文件,一个专用的日志采集 Agent 容器共享该文件目录,将日志推送到中心。
共享架构示意:
#mermaid-svg-c6C51FAhMOuHmf5H{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-c6C51FAhMOuHmf5H .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-c6C51FAhMOuHmf5H .error-icon{fill:#552222;}#mermaid-svg-c6C51FAhMOuHmf5H .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-c6C51FAhMOuHmf5H .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-c6C51FAhMOuHmf5H .marker{fill:#333333;stroke:#333333;}#mermaid-svg-c6C51FAhMOuHmf5H .marker.cross{stroke:#333333;}#mermaid-svg-c6C51FAhMOuHmf5H svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-c6C51FAhMOuHmf5H p{margin:0;}#mermaid-svg-c6C51FAhMOuHmf5H .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-c6C51FAhMOuHmf5H .cluster-label text{fill:#333;}#mermaid-svg-c6C51FAhMOuHmf5H .cluster-label span{color:#333;}#mermaid-svg-c6C51FAhMOuHmf5H .cluster-label span p{background-color:transparent;}#mermaid-svg-c6C51FAhMOuHmf5H .label text,#mermaid-svg-c6C51FAhMOuHmf5H span{fill:#333;color:#333;}#mermaid-svg-c6C51FAhMOuHmf5H .node rect,#mermaid-svg-c6C51FAhMOuHmf5H .node circle,#mermaid-svg-c6C51FAhMOuHmf5H .node ellipse,#mermaid-svg-c6C51FAhMOuHmf5H .node polygon,#mermaid-svg-c6C51FAhMOuHmf5H .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-c6C51FAhMOuHmf5H .rough-node .label text,#mermaid-svg-c6C51FAhMOuHmf5H .node .label text,#mermaid-svg-c6C51FAhMOuHmf5H .image-shape .label,#mermaid-svg-c6C51FAhMOuHmf5H .icon-shape .label{text-anchor:middle;}#mermaid-svg-c6C51FAhMOuHmf5H .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-c6C51FAhMOuHmf5H .rough-node .label,#mermaid-svg-c6C51FAhMOuHmf5H .node .label,#mermaid-svg-c6C51FAhMOuHmf5H .image-shape .label,#mermaid-svg-c6C51FAhMOuHmf5H .icon-shape .label{text-align:center;}#mermaid-svg-c6C51FAhMOuHmf5H .node.clickable{cursor:pointer;}#mermaid-svg-c6C51FAhMOuHmf5H .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-c6C51FAhMOuHmf5H .arrowheadPath{fill:#333333;}#mermaid-svg-c6C51FAhMOuHmf5H .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-c6C51FAhMOuHmf5H .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-c6C51FAhMOuHmf5H .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-c6C51FAhMOuHmf5H .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-c6C51FAhMOuHmf5H .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-c6C51FAhMOuHmf5H .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-c6C51FAhMOuHmf5H .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-c6C51FAhMOuHmf5H .cluster text{fill:#333;}#mermaid-svg-c6C51FAhMOuHmf5H .cluster span{color:#333;}#mermaid-svg-c6C51FAhMOuHmf5H 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-c6C51FAhMOuHmf5H .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-c6C51FAhMOuHmf5H rect.text{fill:none;stroke-width:0;}#mermaid-svg-c6C51FAhMOuHmf5H .icon-shape,#mermaid-svg-c6C51FAhMOuHmf5H .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-c6C51FAhMOuHmf5H .icon-shape p,#mermaid-svg-c6C51FAhMOuHmf5H .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-c6C51FAhMOuHmf5H .icon-shape .label rect,#mermaid-svg-c6C51FAhMOuHmf5H .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-c6C51FAhMOuHmf5H .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-c6C51FAhMOuHmf5H .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-c6C51FAhMOuHmf5H :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Pod/Compose
写入日志
读取日志
推送
业务容器 Java App
日志采集 Agent
Fluentd/Filebeat
共享 Volume
/var/log/app
ELK/日志中心
Docker Compose 设计理念:两个服务在 Compose 文件中声明挂载同一个命名 Volume,即可实现 Sidecar 共享。无需网络调用,文件系统成为它们之间的通信通道。
五、Java 面试追问与答案
1. 为什么推荐 Volume 而不是 Bind Mount 来共享数据?
答:Volume 由 Docker 完全管理,具有更好的可移植性和权限隔离;Bind Mount 暴露了宿主文件结构,不同机器路径可能不同,且容器可能意外修改宿主机敏感文件。生产环境应优先使用 Volume,仅开发调试用 Bind Mount。
2. 如果多个容器同时写入同一个 Volume 文件,会发生什么?
答:依赖文件系统的并发控制机制,可能导致文件损坏或写入冲突。应该设计好避免多个进程写同一个文件,或使用文件锁;更好的做法是每个容器写自己的子目录或文件,由另一容器聚合处理。
3. 数据卷容器看起来像一个笨重的额外容器,为什么还要用它?
答:数据卷容器主要用于封装 Volume 的元数据,方便数据备份、恢复和迁移。比如可以将数据卷容器打包镜像,用于预置初始数据。在 Docker Compose 中,它也被用来确保 Volume 生命周期的明确归属。
4. 跨主机共享数据时,如何保证数据一致性?
答:使用 NFS 等网络存储时,需要考虑多个容器并发读写可能产生的竞争。解决方案包括:使用支持文件锁的分布式文件系统(如 CephFS),或在应用层设计避免冲突(如唯一写入者)。对于数据库类场景,不建议多实例共享同一数据目录,应使用数据库自身的复制机制。
5. Kubernetes 中如何实现类似的数据共享?
答:Kubernetes 中通过 PersistentVolume、PersistentVolumeClaim 和 Pod 内的多个容器共享同一个 Volume 来实现。Sidecar 模式在 K8s 中非常普遍,与 Docker Compose 的共享 Volume 原理一致。
六、思维导图总结
#mermaid-svg-14duTacmXKOEeHns{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-14duTacmXKOEeHns .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-14duTacmXKOEeHns .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-14duTacmXKOEeHns .error-icon{fill:#552222;}#mermaid-svg-14duTacmXKOEeHns .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-14duTacmXKOEeHns .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-14duTacmXKOEeHns .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-14duTacmXKOEeHns .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-14duTacmXKOEeHns .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-14duTacmXKOEeHns .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-14duTacmXKOEeHns .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-14duTacmXKOEeHns .marker{fill:#333333;stroke:#333333;}#mermaid-svg-14duTacmXKOEeHns .marker.cross{stroke:#333333;}#mermaid-svg-14duTacmXKOEeHns svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-14duTacmXKOEeHns p{margin:0;}#mermaid-svg-14duTacmXKOEeHns .edge{stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .section--1 rect,#mermaid-svg-14duTacmXKOEeHns .section--1 path,#mermaid-svg-14duTacmXKOEeHns .section--1 circle,#mermaid-svg-14duTacmXKOEeHns .section--1 polygon,#mermaid-svg-14duTacmXKOEeHns .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section--1 text{fill:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth--1{stroke-width:17;}#mermaid-svg-14duTacmXKOEeHns .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-0 rect,#mermaid-svg-14duTacmXKOEeHns .section-0 path,#mermaid-svg-14duTacmXKOEeHns .section-0 circle,#mermaid-svg-14duTacmXKOEeHns .section-0 polygon,#mermaid-svg-14duTacmXKOEeHns .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-14duTacmXKOEeHns .section-0 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-0{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-0{stroke-width:14;}#mermaid-svg-14duTacmXKOEeHns .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-1 rect,#mermaid-svg-14duTacmXKOEeHns .section-1 path,#mermaid-svg-14duTacmXKOEeHns .section-1 circle,#mermaid-svg-14duTacmXKOEeHns .section-1 polygon,#mermaid-svg-14duTacmXKOEeHns .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-1 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-1{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-1{stroke-width:11;}#mermaid-svg-14duTacmXKOEeHns .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-2 rect,#mermaid-svg-14duTacmXKOEeHns .section-2 path,#mermaid-svg-14duTacmXKOEeHns .section-2 circle,#mermaid-svg-14duTacmXKOEeHns .section-2 polygon,#mermaid-svg-14duTacmXKOEeHns .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-2 text{fill:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-2{stroke-width:8;}#mermaid-svg-14duTacmXKOEeHns .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-3 rect,#mermaid-svg-14duTacmXKOEeHns .section-3 path,#mermaid-svg-14duTacmXKOEeHns .section-3 circle,#mermaid-svg-14duTacmXKOEeHns .section-3 polygon,#mermaid-svg-14duTacmXKOEeHns .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-3 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-3{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-3{stroke-width:5;}#mermaid-svg-14duTacmXKOEeHns .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-4 rect,#mermaid-svg-14duTacmXKOEeHns .section-4 path,#mermaid-svg-14duTacmXKOEeHns .section-4 circle,#mermaid-svg-14duTacmXKOEeHns .section-4 polygon,#mermaid-svg-14duTacmXKOEeHns .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-4 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-4{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-4{stroke-width:2;}#mermaid-svg-14duTacmXKOEeHns .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-5 rect,#mermaid-svg-14duTacmXKOEeHns .section-5 path,#mermaid-svg-14duTacmXKOEeHns .section-5 circle,#mermaid-svg-14duTacmXKOEeHns .section-5 polygon,#mermaid-svg-14duTacmXKOEeHns .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-5 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-5{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-5{stroke-width:-1;}#mermaid-svg-14duTacmXKOEeHns .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-6 rect,#mermaid-svg-14duTacmXKOEeHns .section-6 path,#mermaid-svg-14duTacmXKOEeHns .section-6 circle,#mermaid-svg-14duTacmXKOEeHns .section-6 polygon,#mermaid-svg-14duTacmXKOEeHns .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-6 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-6{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-6{stroke-width:-4;}#mermaid-svg-14duTacmXKOEeHns .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-7 rect,#mermaid-svg-14duTacmXKOEeHns .section-7 path,#mermaid-svg-14duTacmXKOEeHns .section-7 circle,#mermaid-svg-14duTacmXKOEeHns .section-7 polygon,#mermaid-svg-14duTacmXKOEeHns .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-7 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-7{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-7{stroke-width:-7;}#mermaid-svg-14duTacmXKOEeHns .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-8 rect,#mermaid-svg-14duTacmXKOEeHns .section-8 path,#mermaid-svg-14duTacmXKOEeHns .section-8 circle,#mermaid-svg-14duTacmXKOEeHns .section-8 polygon,#mermaid-svg-14duTacmXKOEeHns .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-8 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-8{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-8{stroke-width:-10;}#mermaid-svg-14duTacmXKOEeHns .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-9 rect,#mermaid-svg-14duTacmXKOEeHns .section-9 path,#mermaid-svg-14duTacmXKOEeHns .section-9 circle,#mermaid-svg-14duTacmXKOEeHns .section-9 polygon,#mermaid-svg-14duTacmXKOEeHns .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-9 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-9{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-9{stroke-width:-13;}#mermaid-svg-14duTacmXKOEeHns .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-10 rect,#mermaid-svg-14duTacmXKOEeHns .section-10 path,#mermaid-svg-14duTacmXKOEeHns .section-10 circle,#mermaid-svg-14duTacmXKOEeHns .section-10 polygon,#mermaid-svg-14duTacmXKOEeHns .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-10 text{fill:black;}#mermaid-svg-14duTacmXKOEeHns .node-icon-10{font-size:40px;color:black;}#mermaid-svg-14duTacmXKOEeHns .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .edge-depth-10{stroke-width:-16;}#mermaid-svg-14duTacmXKOEeHns .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-14duTacmXKOEeHns .disabled,#mermaid-svg-14duTacmXKOEeHns .disabled circle,#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:lightgray;}#mermaid-svg-14duTacmXKOEeHns .disabled text{fill:#efefef;}#mermaid-svg-14duTacmXKOEeHns .section-root rect,#mermaid-svg-14duTacmXKOEeHns .section-root path,#mermaid-svg-14duTacmXKOEeHns .section-root circle,#mermaid-svg-14duTacmXKOEeHns .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-14duTacmXKOEeHns .section-root text{fill:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .section-root span{color:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .section-2 span{color:#ffffff;}#mermaid-svg-14duTacmXKOEeHns .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-14duTacmXKOEeHns .edge{fill:none;}#mermaid-svg-14duTacmXKOEeHns .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-14duTacmXKOEeHns :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 容器数据共享方法
Volume 共享
命名 Volume
NFS/Ceph
生命周期独立
Sidecar 模式
Bind Mount 共享
宿主机目录映射
开发环境热加载
路径耦合
数据卷容器
封装 Volume 引用
便于备份/迁移
通过 volumes-from 继承
跨主机方案
NFS Volume 驱动
CephFS
S3 挂载
安全考虑
最小权限原则
避免共享敏感数据
Volume 加密
七、总结
容器间共享数据的本质是将文件系统的一部分从单个容器的可写层抽离,映射到多个容器的联合挂载点。推荐使用命名 Volume 和 Sidecar 模式,它们与容器生命周期解耦,易于管理和移植。对于跨主机需求,可引入 NFS 等网络存储驱动。在 Java 微服务中,合理运用这些共享机制能够实现日志聚合、文件处理等协作流程,而无需侵入应用代码。