欢迎光临
我们一直在努力

高级java每日一道面试题-2026年02月08日-实战篇[Docker]-如何实现容器的快照和恢复?

容器快照与恢复是高级运维和弹性伸缩中的关键技术,它既能保存容器的文件系统状态,也能在更高级的场景下冻结并恢复进程内存。对 Java 应用而言,合理的快照策略可以加速调试、实现快速回滚,甚至在跨节点迁移时保证会话不丢失。下面从概念、机制、流程到面试要点,进行全方位解析。

一、快照与恢复的实现方式

Docker 提供了两种截然不同的“快照”手段:

#mermaid-svg-rLCqbTRNLLftzAXR{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-rLCqbTRNLLftzAXR .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rLCqbTRNLLftzAXR .error-icon{fill:#552222;}#mermaid-svg-rLCqbTRNLLftzAXR .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rLCqbTRNLLftzAXR .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rLCqbTRNLLftzAXR .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rLCqbTRNLLftzAXR .marker.cross{stroke:#333333;}#mermaid-svg-rLCqbTRNLLftzAXR svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rLCqbTRNLLftzAXR p{margin:0;}#mermaid-svg-rLCqbTRNLLftzAXR .edge{stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .section–1 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section–1 path,#mermaid-svg-rLCqbTRNLLftzAXR .section–1 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section–1 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section–1 text{fill:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth–1{stroke-width:17;}#mermaid-svg-rLCqbTRNLLftzAXR .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-0 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-0 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-0 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-0 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-0 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-0{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-0{stroke-width:14;}#mermaid-svg-rLCqbTRNLLftzAXR .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-1 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-1 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-1 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-1 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-1 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-1{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-1{stroke-width:11;}#mermaid-svg-rLCqbTRNLLftzAXR .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-2 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-2 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-2 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-2 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-2 text{fill:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-2{stroke-width:8;}#mermaid-svg-rLCqbTRNLLftzAXR .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-3 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-3 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-3 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-3 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-3 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-3{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-3{stroke-width:5;}#mermaid-svg-rLCqbTRNLLftzAXR .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-4 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-4 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-4 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-4 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-4 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-4{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-4{stroke-width:2;}#mermaid-svg-rLCqbTRNLLftzAXR .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-5 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-5 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-5 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-5 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-5 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-5{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-5{stroke-width:-1;}#mermaid-svg-rLCqbTRNLLftzAXR .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-6 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-6 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-6 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-6 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-6 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-6{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-6{stroke-width:-4;}#mermaid-svg-rLCqbTRNLLftzAXR .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-7 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-7 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-7 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-7 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-7 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-7{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-7{stroke-width:-7;}#mermaid-svg-rLCqbTRNLLftzAXR .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-8 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-8 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-8 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-8 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-8 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-8{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-8{stroke-width:-10;}#mermaid-svg-rLCqbTRNLLftzAXR .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-9 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-9 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-9 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-9 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-9 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-9{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-9{stroke-width:-13;}#mermaid-svg-rLCqbTRNLLftzAXR .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-10 rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-10 path,#mermaid-svg-rLCqbTRNLLftzAXR .section-10 circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-10 polygon,#mermaid-svg-rLCqbTRNLLftzAXR .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-10 text{fill:black;}#mermaid-svg-rLCqbTRNLLftzAXR .node-icon-10{font-size:40px;color:black;}#mermaid-svg-rLCqbTRNLLftzAXR .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .edge-depth-10{stroke-width:-16;}#mermaid-svg-rLCqbTRNLLftzAXR .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled,#mermaid-svg-rLCqbTRNLLftzAXR .disabled circle,#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:lightgray;}#mermaid-svg-rLCqbTRNLLftzAXR .disabled text{fill:#efefef;}#mermaid-svg-rLCqbTRNLLftzAXR .section-root rect,#mermaid-svg-rLCqbTRNLLftzAXR .section-root path,#mermaid-svg-rLCqbTRNLLftzAXR .section-root circle,#mermaid-svg-rLCqbTRNLLftzAXR .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-rLCqbTRNLLftzAXR .section-root text{fill:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .section-root span{color:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .section-2 span{color:#ffffff;}#mermaid-svg-rLCqbTRNLLftzAXR .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-rLCqbTRNLLftzAXR .edge{fill:none;}#mermaid-svg-rLCqbTRNLLftzAXR .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-rLCqbTRNLLftzAXR :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

容器快照与恢复

方式一:docker commit

仅文件系统快照

生成新镜像

不保存进程状态

适合环境保存、调试

方式二:Checkpoint/Restore

完整进程快照

使用 CRIU

保存内存、CPU 状态

适合迁移、快速启动

二、文件系统快照 (docker commit)

docker commit 会将容器的可写层冻结成一个新的只读镜像层,叠加在原镜像之上,不包含任何运行时的内存信息、网络连接等。它是一种轻量级的“保存现场”。

原理架构:

#mermaid-svg-dG9JRQhcpPCWA1ME{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-dG9JRQhcpPCWA1ME .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dG9JRQhcpPCWA1ME .error-icon{fill:#552222;}#mermaid-svg-dG9JRQhcpPCWA1ME .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dG9JRQhcpPCWA1ME .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dG9JRQhcpPCWA1ME .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dG9JRQhcpPCWA1ME .marker.cross{stroke:#333333;}#mermaid-svg-dG9JRQhcpPCWA1ME svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dG9JRQhcpPCWA1ME p{margin:0;}#mermaid-svg-dG9JRQhcpPCWA1ME .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster-label text{fill:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster-label span{color:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster-label span p{background-color:transparent;}#mermaid-svg-dG9JRQhcpPCWA1ME .label text,#mermaid-svg-dG9JRQhcpPCWA1ME span{fill:#333;color:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME .node rect,#mermaid-svg-dG9JRQhcpPCWA1ME .node circle,#mermaid-svg-dG9JRQhcpPCWA1ME .node ellipse,#mermaid-svg-dG9JRQhcpPCWA1ME .node polygon,#mermaid-svg-dG9JRQhcpPCWA1ME .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dG9JRQhcpPCWA1ME .rough-node .label text,#mermaid-svg-dG9JRQhcpPCWA1ME .node .label text,#mermaid-svg-dG9JRQhcpPCWA1ME .image-shape .label,#mermaid-svg-dG9JRQhcpPCWA1ME .icon-shape .label{text-anchor:middle;}#mermaid-svg-dG9JRQhcpPCWA1ME .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dG9JRQhcpPCWA1ME .rough-node .label,#mermaid-svg-dG9JRQhcpPCWA1ME .node .label,#mermaid-svg-dG9JRQhcpPCWA1ME .image-shape .label,#mermaid-svg-dG9JRQhcpPCWA1ME .icon-shape .label{text-align:center;}#mermaid-svg-dG9JRQhcpPCWA1ME .node.clickable{cursor:pointer;}#mermaid-svg-dG9JRQhcpPCWA1ME .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dG9JRQhcpPCWA1ME .arrowheadPath{fill:#333333;}#mermaid-svg-dG9JRQhcpPCWA1ME .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dG9JRQhcpPCWA1ME .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dG9JRQhcpPCWA1ME .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dG9JRQhcpPCWA1ME .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dG9JRQhcpPCWA1ME .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dG9JRQhcpPCWA1ME .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster text{fill:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME .cluster span{color:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME 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-dG9JRQhcpPCWA1ME .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dG9JRQhcpPCWA1ME rect.text{fill:none;stroke-width:0;}#mermaid-svg-dG9JRQhcpPCWA1ME .icon-shape,#mermaid-svg-dG9JRQhcpPCWA1ME .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dG9JRQhcpPCWA1ME .icon-shape p,#mermaid-svg-dG9JRQhcpPCWA1ME .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dG9JRQhcpPCWA1ME .icon-shape .label rect,#mermaid-svg-dG9JRQhcpPCWA1ME .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dG9JRQhcpPCWA1ME .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dG9JRQhcpPCWA1ME .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dG9JRQhcpPCWA1ME :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

docker commit

新镜像

新只读层 = 原可写层

原容器

只读层 1

只读层 2

可写层

时序图:commit 保存容器现场

镜像仓库运行容器Docker Daemon开发者镜像仓库运行容器Docker Daemon开发者#mermaid-svg-Og7PFoYTt5pzqyuw{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-Og7PFoYTt5pzqyuw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Og7PFoYTt5pzqyuw .error-icon{fill:#552222;}#mermaid-svg-Og7PFoYTt5pzqyuw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Og7PFoYTt5pzqyuw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Og7PFoYTt5pzqyuw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Og7PFoYTt5pzqyuw .marker.cross{stroke:#333333;}#mermaid-svg-Og7PFoYTt5pzqyuw svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Og7PFoYTt5pzqyuw p{margin:0;}#mermaid-svg-Og7PFoYTt5pzqyuw .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Og7PFoYTt5pzqyuw text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-Og7PFoYTt5pzqyuw .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-Og7PFoYTt5pzqyuw .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-Og7PFoYTt5pzqyuw #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-Og7PFoYTt5pzqyuw .sequenceNumber{fill:white;}#mermaid-svg-Og7PFoYTt5pzqyuw #sequencenumber{fill:#333;}#mermaid-svg-Og7PFoYTt5pzqyuw #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-Og7PFoYTt5pzqyuw .messageText{fill:#333;stroke:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Og7PFoYTt5pzqyuw .labelText,#mermaid-svg-Og7PFoYTt5pzqyuw .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .loopText,#mermaid-svg-Og7PFoYTt5pzqyuw .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .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-Og7PFoYTt5pzqyuw .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-Og7PFoYTt5pzqyuw .noteText,#mermaid-svg-Og7PFoYTt5pzqyuw .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-Og7PFoYTt5pzqyuw .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Og7PFoYTt5pzqyuw .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Og7PFoYTt5pzqyuw .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Og7PFoYTt5pzqyuw .actorPopupMenu{position:absolute;}#mermaid-svg-Og7PFoYTt5pzqyuw .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-Og7PFoYTt5pzqyuw .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Og7PFoYTt5pzqyuw .actor-man circle,#mermaid-svg-Og7PFoYTt5pzqyuw line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-Og7PFoYTt5pzqyuw :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}手动修改配置文件,未修复基于新镜像重启容器继续调试正在调试一个出错的 Java 应用docker commit <容器ID> debug:v1冻结可写层,生成新镜像层新镜像 ID可选:推送 debug:v1 镜像

特点与局限:

  • 优点:操作简单,能快速保存文件系统的任何修改(配置、日志、临时文件)。
  • 缺点:无内存状态,无法保留 Java 进程的堆栈、锁、网络连接等;镜像体积会因可写层膨胀而变大;不可重现。
  • Java 场景:适合保存一次性的调试现场,如 JVM 参数调整、配置文件临时修改;但不适合生产环境的状态保存。

三、进程级快照 (Checkpoint/Restore)

Docker 实验性支持Checkpoint/Restore,底层使用 CRIU (Checkpoint/Restore In Userspace) 技术。它能冻结容器内所有进程,将进程树、内存页、打开的文件、网络 socket 等信息转储为一组文件,之后可以基于这些文件完全恢复容器,就像它从未被停止过一样。

核心原理流程:

#mermaid-svg-IxFi3mTb5AgctRJA{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-IxFi3mTb5AgctRJA .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IxFi3mTb5AgctRJA .error-icon{fill:#552222;}#mermaid-svg-IxFi3mTb5AgctRJA .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IxFi3mTb5AgctRJA .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IxFi3mTb5AgctRJA .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IxFi3mTb5AgctRJA .marker.cross{stroke:#333333;}#mermaid-svg-IxFi3mTb5AgctRJA svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IxFi3mTb5AgctRJA p{margin:0;}#mermaid-svg-IxFi3mTb5AgctRJA .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-IxFi3mTb5AgctRJA .cluster-label text{fill:#333;}#mermaid-svg-IxFi3mTb5AgctRJA .cluster-label span{color:#333;}#mermaid-svg-IxFi3mTb5AgctRJA .cluster-label span p{background-color:transparent;}#mermaid-svg-IxFi3mTb5AgctRJA .label text,#mermaid-svg-IxFi3mTb5AgctRJA span{fill:#333;color:#333;}#mermaid-svg-IxFi3mTb5AgctRJA .node rect,#mermaid-svg-IxFi3mTb5AgctRJA .node circle,#mermaid-svg-IxFi3mTb5AgctRJA .node ellipse,#mermaid-svg-IxFi3mTb5AgctRJA .node polygon,#mermaid-svg-IxFi3mTb5AgctRJA .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IxFi3mTb5AgctRJA .rough-node .label text,#mermaid-svg-IxFi3mTb5AgctRJA .node .label text,#mermaid-svg-IxFi3mTb5AgctRJA .image-shape .label,#mermaid-svg-IxFi3mTb5AgctRJA .icon-shape .label{text-anchor:middle;}#mermaid-svg-IxFi3mTb5AgctRJA .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IxFi3mTb5AgctRJA .rough-node .label,#mermaid-svg-IxFi3mTb5AgctRJA .node .label,#mermaid-svg-IxFi3mTb5AgctRJA .image-shape .label,#mermaid-svg-IxFi3mTb5AgctRJA .icon-shape .label{text-align:center;}#mermaid-svg-IxFi3mTb5AgctRJA .node.clickable{cursor:pointer;}#mermaid-svg-IxFi3mTb5AgctRJA .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IxFi3mTb5AgctRJA .arrowheadPath{fill:#333333;}#mermaid-svg-IxFi3mTb5AgctRJA .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IxFi3mTb5AgctRJA .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IxFi3mTb5AgctRJA .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IxFi3mTb5AgctRJA .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IxFi3mTb5AgctRJA .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IxFi3mTb5AgctRJA .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IxFi3mTb5AgctRJA .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IxFi3mTb5AgctRJA .cluster text{fill:#333;}#mermaid-svg-IxFi3mTb5AgctRJA .cluster span{color:#333;}#mermaid-svg-IxFi3mTb5AgctRJA 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-IxFi3mTb5AgctRJA .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IxFi3mTb5AgctRJA rect.text{fill:none;stroke-width:0;}#mermaid-svg-IxFi3mTb5AgctRJA .icon-shape,#mermaid-svg-IxFi3mTb5AgctRJA .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IxFi3mTb5AgctRJA .icon-shape p,#mermaid-svg-IxFi3mTb5AgctRJA .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IxFi3mTb5AgctRJA .icon-shape .label rect,#mermaid-svg-IxFi3mTb5AgctRJA .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IxFi3mTb5AgctRJA .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IxFi3mTb5AgctRJA .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IxFi3mTb5AgctRJA :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

运行中的容器

触发 Checkpoint

Docker Daemon 调用 CRIU

冻结进程

转储进程树、内存页、文件描述符、socket、信号

生成 Checkpoint 文件保存到宿主机目录

容器停止

需要恢复容器

读取 Checkpoint 文件

重建进程树、内存映射

恢复文件描述符和网络连接

容器继续运行

时序图:Checkpoint 与 Restore

宿主机磁盘Java 容器CRIUDocker Daemon管理员宿主机磁盘Java 容器CRIUDocker Daemon管理员#mermaid-svg-OpiojMoBKRLN1h62{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-OpiojMoBKRLN1h62 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-OpiojMoBKRLN1h62 .error-icon{fill:#552222;}#mermaid-svg-OpiojMoBKRLN1h62 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OpiojMoBKRLN1h62 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OpiojMoBKRLN1h62 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OpiojMoBKRLN1h62 .marker.cross{stroke:#333333;}#mermaid-svg-OpiojMoBKRLN1h62 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OpiojMoBKRLN1h62 p{margin:0;}#mermaid-svg-OpiojMoBKRLN1h62 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-OpiojMoBKRLN1h62 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-OpiojMoBKRLN1h62 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-OpiojMoBKRLN1h62 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-OpiojMoBKRLN1h62 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-OpiojMoBKRLN1h62 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-OpiojMoBKRLN1h62 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-OpiojMoBKRLN1h62 .sequenceNumber{fill:white;}#mermaid-svg-OpiojMoBKRLN1h62 #sequencenumber{fill:#333;}#mermaid-svg-OpiojMoBKRLN1h62 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-OpiojMoBKRLN1h62 .messageText{fill:#333;stroke:none;}#mermaid-svg-OpiojMoBKRLN1h62 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-OpiojMoBKRLN1h62 .labelText,#mermaid-svg-OpiojMoBKRLN1h62 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-OpiojMoBKRLN1h62 .loopText,#mermaid-svg-OpiojMoBKRLN1h62 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-OpiojMoBKRLN1h62 .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-OpiojMoBKRLN1h62 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-OpiojMoBKRLN1h62 .noteText,#mermaid-svg-OpiojMoBKRLN1h62 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-OpiojMoBKRLN1h62 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-OpiojMoBKRLN1h62 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-OpiojMoBKRLN1h62 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-OpiojMoBKRLN1h62 .actorPopupMenu{position:absolute;}#mermaid-svg-OpiojMoBKRLN1h62 .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-OpiojMoBKRLN1h62 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-OpiojMoBKRLN1h62 .actor-man circle,#mermaid-svg-OpiojMoBKRLN1h62 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-OpiojMoBKRLN1h62 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}可将 checkpoint 文件迁移到其他节点docker checkpoint create myapp myapp-checkpoint转储容器进程状态冻结进程(SIGSTOP)收集 /proc/pid/maps, mem, fd…写入 checkpoint 文件集转储完成停止容器docker start –checkpoint myapp-checkpoint myapp加载 checkpoint 文件重建进程树、内存空间恢复文件锁、TCP 连接(需配合)恢复成功继续运行(进程被唤醒)

四、两种方式的对比

特性docker commit (文件系统快照)CRIU Checkpoint/Restore (进程快照)
保存内容 容器可写层(文件系统增量) 进程树、内存、CPU 寄存器、文件描述符、socket 等
运行状态保留 否(进程上下文丢失) 是(恢复后进程继续运行)
网络连接保留 部分支持(TCP 连接需要特殊处理)
体积 等于镜像新层大小(可能大) checkpoint 文件集大小近似容器内存使用量
启动速度 正常启动(重新运行 CMD) 极快(免去初始化,直接从冻结点恢复)
适用场景 保存文件修改、调试现场、制作定制镜像 快速启动、无状态服务弹性伸缩、跨节点迁移、低延迟恢复
Java 应用关键点 无法恢复会话、缓存、锁状态 可能因 Java 线程模型、时钟、网络中断导致异常,需要应用配合

五、Java 视角下的特殊考量

  • 文件系统快照 (commit) 的坑
    若容器内 Java 进程正在写文件(如日志、H2 数据库文件),docker commit 抓取的可写层可能包含不完整的文件,恢复后的容器可能因文件损坏而启动失败。建议在 commit 前停止应用或确保文件一致性。

  • CRIU 对 Java 的兼容性
    CRIU 能转储 JVM 进程,但 Java 的线程模型、System.currentTimeMillis()、网络 I/O 等可能导致恢复后出现:

    • 时间跳跃(应用认为时间突然过了很久)
    • 网络超时(TCP 保活计时器过期)
    • 文件锁丢失
    • 随机数生成器状态不一致

    因此,基于 Checkpoint/Restore 的快速启动(如 OpenJDK 的 CRaC 项目)需要 Java 应用围绕 beforeCheckpoint / afterRestore 生命周期事件进行适配,关闭并重建网络连接、重置时钟敏感变量等。

  • 应用场景推荐

    • 如果仅需保存故障现场用于离线分析,用 docker commit。
    • 如果要实现毫秒级弹性扩容(如 AWS Lambda、Kubernetes 无服务器容器),需要进程级快照,那么就要结合 CRIU 和 Java 框架(如 Quarkus、Spring Boot CRaC 扩展)。
  • 六、选型决策流程

    #mermaid-svg-dAWSE2ezmJ5yvDdq{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-dAWSE2ezmJ5yvDdq .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dAWSE2ezmJ5yvDdq .error-icon{fill:#552222;}#mermaid-svg-dAWSE2ezmJ5yvDdq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dAWSE2ezmJ5yvDdq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .marker.cross{stroke:#333333;}#mermaid-svg-dAWSE2ezmJ5yvDdq svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dAWSE2ezmJ5yvDdq p{margin:0;}#mermaid-svg-dAWSE2ezmJ5yvDdq .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster-label text{fill:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster-label span{color:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster-label span p{background-color:transparent;}#mermaid-svg-dAWSE2ezmJ5yvDdq .label text,#mermaid-svg-dAWSE2ezmJ5yvDdq span{fill:#333;color:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .node rect,#mermaid-svg-dAWSE2ezmJ5yvDdq .node circle,#mermaid-svg-dAWSE2ezmJ5yvDdq .node ellipse,#mermaid-svg-dAWSE2ezmJ5yvDdq .node polygon,#mermaid-svg-dAWSE2ezmJ5yvDdq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .rough-node .label text,#mermaid-svg-dAWSE2ezmJ5yvDdq .node .label text,#mermaid-svg-dAWSE2ezmJ5yvDdq .image-shape .label,#mermaid-svg-dAWSE2ezmJ5yvDdq .icon-shape .label{text-anchor:middle;}#mermaid-svg-dAWSE2ezmJ5yvDdq .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .rough-node .label,#mermaid-svg-dAWSE2ezmJ5yvDdq .node .label,#mermaid-svg-dAWSE2ezmJ5yvDdq .image-shape .label,#mermaid-svg-dAWSE2ezmJ5yvDdq .icon-shape .label{text-align:center;}#mermaid-svg-dAWSE2ezmJ5yvDdq .node.clickable{cursor:pointer;}#mermaid-svg-dAWSE2ezmJ5yvDdq .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .arrowheadPath{fill:#333333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dAWSE2ezmJ5yvDdq .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dAWSE2ezmJ5yvDdq .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dAWSE2ezmJ5yvDdq .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster text{fill:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq .cluster span{color:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq 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-dAWSE2ezmJ5yvDdq .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dAWSE2ezmJ5yvDdq rect.text{fill:none;stroke-width:0;}#mermaid-svg-dAWSE2ezmJ5yvDdq .icon-shape,#mermaid-svg-dAWSE2ezmJ5yvDdq .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dAWSE2ezmJ5yvDdq .icon-shape p,#mermaid-svg-dAWSE2ezmJ5yvDdq .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dAWSE2ezmJ5yvDdq .icon-shape .label rect,#mermaid-svg-dAWSE2ezmJ5yvDdq .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dAWSE2ezmJ5yvDdq .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dAWSE2ezmJ5yvDdq .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dAWSE2ezmJ5yvDdq :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    不需要

    需要

    Java 应用有适配

    无适配

    需要保存容器状态

    需要保存内存/进程状态吗?

    使用 docker commit或导出文件系统 tar

    应用是否支持 Checkpoint ?

    使用 Checkpoint/Restore或 OpenJDK CRaC

    考虑传统持久化策略如数据库、消息队列

    七、总结

    容器快照与恢复提供了不同粒度的“时光机”:文件系统级别的 commit 像给容器拍一张“磁盘照片”,简单但丢失运行态;进程级的 Checkpoint 像保存整个计算机的休眠镜像,能无缝还原,但需要应用配合。对于 Java 高级工程师,理解这两种机制的原理与边界,在面试中能清晰阐述如何为微服务选择合适的状态保存方案,是架构设计成熟度的体现。

    赞(0)
    未经允许不得转载:171主机测评 » 高级java每日一道面试题-2026年02月08日-实战篇[Docker]-如何实现容器的快照和恢复?
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址