2026-10-14 22:42:38 +08:00
<!DOCTYPE html>
< html lang = "zh-CN" data-theme = "light" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > 第 3 章 镜像:分层、标识与分发格式 · Docker 专题知识库< / title >
< meta name = "description" content = "讲清 manifest/config/layer 三件套与 OCI 媒体类型、overlay2 写时复制的代价、tag 与 digest 在生产中的用法、多架构构建、pull/push/save/load 选择,以及镜像来源可信度判断。" >
< link rel = "stylesheet" href = "assets/doc.css" >
< / head >
< body >
< header class = "topbar" >
< div class = "tb-in" >
< a class = "brand" href = "index.html" > < span class = "logo" > DKR< / span > < div > Docker 专题< small > 开发到生产的容器知识库< / small > < / div > < / a >
< div class = "crumbs" >
< a href = "index.html" > 总目录< / a > < span class = "sep" > /< / span > < b > 第 3 章< / b >
< / div >
< div class = "spacer" > < / div >
< button class = "btn" id = "tocToggle" > ☰ 导航< / button >
< button class = "btn" id = "themeBtn" title = "切换到深色主题" > 深色< / button >
< / div >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
< nav id = "chapters" > < a class = "toc-a" href = "01-what.html" > < i class = "cn" > 1< / i > 容器与 Docker 全景:它到底解决了什么问题< / a > < a class = "toc-a" href = "02-install.html" > < i class = "cn" > 2< / i > 安装、配置与命令心智模型< / a > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 3< / i > 镜像:分层、标识与分发格式< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-3-1" > 3.1 镜像是什么: manifest / config / layer< / a > < a class = "toc-a lv3" href = "#sec-3-2" > 3.2 分层与写时复制< / a > < a class = "toc-a lv3" href = "#sec-3-3" > 3.3 tag、image ID 与 digest< / a > < a class = "toc-a lv3" href = "#sec-3-4" > 3.4 多架构镜像与 manifest list< / a > < a class = "toc-a lv3" href = "#sec-3-5" > 3.5 pull / push / save / load / tag / rmi< / a > < a class = "toc-a lv3" href = "#sec-3-6" > 3.6 读懂镜像: inspect / history / 体积归因< / a > < a class = "toc-a lv3" href = "#sec-3-7" > 3.7 镜像来源可信度< / a > < / div > < a class = "toc-a" href = "04-dockerfile.html" > < i class = "cn" > 4< / i > Dockerfile 全解:从一条指令到可复现的镜像< / a > < a class = "toc-a" href = "05-container.html" > < i class = "cn" > 5< / i > 容器生命周期与日常操作< / a > < a class = "toc-a" href = "06-storage.html" > < i class = "cn" > 6< / i > 存储与数据持久化< / a > < a class = "toc-a" href = "07-network.html" > < i class = "cn" > 7< / i > 网络:容器之间与外界如何通信< / a > < a class = "toc-a" href = "08-compose.html" > < i class = "cn" > 8< / i > Docker Compose: 多容器场景的开发标配< / a > < a class = "toc-a" href = "09-registry.html" > < i class = "cn" > 9< / i > 镜像仓库与分发:把镜像安全、可复现地送到每一台机器< / a > < a class = "toc-a" href = "10-security.html" > < i class = "cn" > 10< / i > 安全与镜像治理:把「被攻破」的后果关进容器< / a > < a class = "toc-a" href = "11-devtools.html" > < i class = "cn" > 11< / i > 开发工作流实战:把 Docker 用进日常< / a > < a class = "toc-a" href = "12-ops.html" > < i class = "cn" > 12< / i > 生产运维、监控与排障< / a > < a class = "toc-a" href = "13-cheat.html" > < i class = "cn" > 13< / i > 速查表与 20 个高频坑< / a > < a class = "toc-a" href = "14-checklist.html" > < i class = "cn" > 14< / i > 能力自测、面试问答与继续深入< / a > < / nav >
< div class = "side-foot" >
< a class = "btn" href = "index.html" > ← 总目录< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< p class = "chapnav" >
< a href = "index.html" > Docker 专题< / a >
< span > ·< / span >
< span > 第 < b class = "pos" > 3< / b > / 14 章< / span >
< / p >
< h2 id = "ch3" > < span class = "n" > 3< / span > 镜像:分层、标识与分发格式< / h2 >
2026-10-25 23:26:58 +08:00
< div class = "box ok" >
< span class = "bt" > 面试 / 实战直答 · 30 秒版< / span >
< p > 镜像不是一个大文件,而是< b > 几份 JSON 描述加一串只读层< / b > ,每样东西都按自己的 sha256 存放。由此能推出三条:同名 tag 可以指向不同内容;层只读,写进去的东西删不掉;三个容器可以共享同一份只读层。< / p >
< ul >
< li > < b > 标识:< / b > tag 是能移动的指针, image ID 是本机报的身份, digest 是清单的 sha256——要证明「是同一份」, 只认 digest。< / li >
< li > < b > 体积:< / b > 「镜像多大」至少有四个口径,谈优化之前先说清按哪个算。< / li >
< li > < b > 分发:< / b > 内容寻址让重复的层只存一份、只传一次,所以统一基础镜像的收益是量级上的。< / li >
< / ul >
< / div >
2026-10-14 22:42:38 +08:00
< div class = "lead" >
2026-10-25 23:26:58 +08:00
< b > 这一章解决什么:< / b > 镜像是容器里唯一需要长期保存、跨机器搬运的东西,所以「它到底是什么」和「怎么证明是同一份」都是生产问题。学完你要能当场回答三句话:这个镜像为什么这么大、这两个 tag 是不是同一个东西、改了 tag 为什么没生效。
< / div >
< h4 > 读之前:六个词< / h4 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 术语< / th > < th > 一句话解释< / th > < th > 在哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > index( 索引) < / td > < td > 多架构时才有:一张「平台 → 清单」的表< / td > < td > 3.1 / 3.4< / td > < / tr >
< tr > < td > manifest( 清单) < / td > < td > 一个平台的描述:指向哪份配置、由哪几层组成< / td > < td > 3.1< / td > < / tr >
< tr > < td > config( 配置) < / td > < td > 运行参数与层顺序的 JSON, classic 存储下 image ID 就是它的 sha256< / td > < td > 3.1< / td > < / tr >
< tr > < td > layer( 层) < / td > < td > 只读的 tar, 解压后依次叠成根文件系统< / td > < td > 3.1 / 3.2< / td > < / tr >
< tr > < td > digest( 摘要) < / td > < td > 清单或索引的 sha256, 内容变它才变< / td > < td > 3.3< / td > < / tr >
< tr > < td > tag( 标签) < / td > < td > 仓库名后面那一段,指向某个 digest, 但会移动< / td > < td > 3.3< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box tip" >
< span class = "bt" > 怎么读这一章< / span >
< p > 每节都是同一个形状:< b > 场景< / b > → < b > 怎么做< / b > → < b > 代价< / b > ,最后一句可以直接背走;时间紧就只读最后那句。< / p >
< p > 标了 < span class = "tag s" > 可跳过< / span > 的段落是 OCI 规范与引擎实现细节,第一遍可以整块跳过,不影响后面任何一节。想先建立形状感,就看< b > 图 1< / b > (一个镜像由哪几样东西组成)与< b > 图 2< / b > (三个容器怎么共享一份层)。< / p >
2026-10-14 22:42:38 +08:00
< / div >
< h3 id = "sec-3-1" > 3.1 镜像是什么: manifest / config / layer< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 有人说「镜像不就是个 tar 包」,你手上正好有个 21 MB 的 < code > nginx:1.27-alpine< / code > ,需要讲清它由哪几样东西组成、每样各管什么。< / p >
< p > < span class = "tag" > 怎么做< / span > 把它拆成三层看:最外面是< b > 索引< / b > (多架构时才存在),中间是< b > 清单< / b > (描述一个平台),最里面是< b > 配置< / b > 加一串< b > 层< / b > 。每一样都是独立的 JSON 或 tar, 有自己的 sha256。< / p >
< pre data-lang = "bash" data-name = "一个多架构镜像的真实骨架( digest 已用占位符)" > $ docker buildx imagetools inspect nginx:1.27-alpine
2026-10-14 22:42:38 +08:00
Name: docker.io/library/nginx:1.27-alpine
MediaType: application/vnd.oci.image.index.v1+json
Digest: sha256:< 64 位十六进制,索引 digest>
Manifests:
Name: docker.io/library/nginx:1.27-alpine@sha256:< 单平台清单 digest>
MediaType: application/vnd.oci.image.manifest.v1+json
Platform: linux/amd64
Name: docker.io/library/nginx:1.27-alpine@sha256:< 单平台清单 digest>
MediaType: application/vnd.oci.image.manifest.v1+json
Platform: linux/arm64< / pre >
2026-10-25 23:26:58 +08:00
< div class = "box note" >
< span class = "bt" > 预期输出< / span >
< p > 本机没有外网,上面这段是 < code > buildx imagetools inspect< / code > 的预期形状, digest 一律用占位符。判断方法比数字重要:凡是要确认「线上跑的是哪一份」,都去看 digest, 而不是看 tag。< / p >
< / div >
< figure class = "figure" >
< svg viewBox = "0 0 720 366" role = "img" aria-label = "一个镜像由索引、清单、配置与若干只读层组成,并标出 docker inspect 各字段落在哪一块" >
< defs >
< marker id = "ar03a" viewBox = "0 0 10 10" refX = "9" refY = "5" markerWidth = "7" markerHeight = "7" orient = "auto-start-reverse" >
< path d = "M0,0 L10,5 L0,10 z" style = "fill:var(--brand)" / >
< / marker >
< marker id = "ar03b" viewBox = "0 0 10 10" refX = "9" refY = "5" markerWidth = "7" markerHeight = "7" orient = "auto-start-reverse" >
< path d = "M0,0 L10,5 L0,10 z" style = "fill:var(--muted)" / >
< / marker >
< / defs >
< text x = "24" y = "30" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > ① tag 先落到索引< / text >
< rect x = "24" y = "40" width = "176" height = "124" rx = "12" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "112" y = "66" text-anchor = "middle" font-size = "14" font-weight = "700" style = "fill:var(--brand-ink)" > index< / text >
< text x = "112" y = "88" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 多架构清单< / text >
< text x = "112" y = "112" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > linux/amd64< / text >
< text x = "112" y = "130" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > linux/arm64< / text >
< text x = "112" y = "152" text-anchor = "middle" font-size = "11" style = "fill:var(--brand-ink)" > .RepoDigests[0]< / text >
< text x = "232" y = "30" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > ② 按平台挑一份< / text >
< rect x = "232" y = "40" width = "196" height = "124" rx = "12" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "330" y = "66" text-anchor = "middle" font-size = "14" font-weight = "700" style = "fill:var(--ink)" > manifest< / text >
< text x = "330" y = "88" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 单平台描述< / text >
< text x = "330" y = "112" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > config 的 digest< / text >
< text x = "330" y = "130" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > layers: digest + size< / text >
< text x = "330" y = "152" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > .Architecture / .Os< / text >
< text x = "470" y = "30" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > ③ 运行参数与层顺序< / text >
< rect x = "470" y = "40" width = "226" height = "92" rx = "12" style = "fill:var(--ok-bg);stroke:var(--ok-line)" / >
< text x = "583" y = "62" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > config( JSON) < / text >
< text x = "583" y = "82" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 运行参数: Env / Cmd / Labels< / text >
< text x = "583" y = "100" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 层顺序: rootfs.diff_ids< / text >
< text x = "583" y = "120" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > .RootFS.Layers 就是它< / text >
< text x = "470" y = "152" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > ④ 若干只读层(只画 3 层)< / text >
< rect x = "470" y = "160" width = "226" height = "32" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "482" y = "181" font-size = "12" font-weight = "700" style = "fill:var(--ink)" > layer 3< / text >
< text x = "684" y = "181" text-anchor = "end" font-size = "11.5" style = "fill:var(--muted)" > 15.5 MB< / text >
< rect x = "470" y = "198" width = "226" height = "32" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "482" y = "219" font-size = "12" font-weight = "700" style = "fill:var(--ink)" > layer 2< / text >
< text x = "684" y = "219" text-anchor = "end" font-size = "11.5" style = "fill:var(--muted)" > 1.8 MB< / text >
< rect x = "470" y = "236" width = "226" height = "32" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "482" y = "257" font-size = "12" font-weight = "700" style = "fill:var(--ink)" > layer 1( base) < / text >
< text x = "684" y = "257" text-anchor = "end" font-size = "11.5" style = "fill:var(--muted)" > 3.6 MB< / text >
< path d = "M204 102 H228" style = "stroke:var(--brand);fill:none" marker-end = "url(#ar03a)" / >
< path d = "M432 76 H466" style = "stroke:var(--brand);fill:none" marker-end = "url(#ar03a)" / >
< path d = "M432 140 C450 140 452 168 466 172" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar03b)" / >
< text x = "24" y = "298" font-size = "12.5" style = "fill:var(--muted)" > 五处 sha256 各管一件事:索引、清单、配置、层的压缩摘要、层的解压摘要,少一个字节都不是同一份。< / text >
< text x = "24" y = "320" font-size = "12.5" style = "fill:var(--muted)" > inspect 的对应关系:.RepoDigests[0] 落到索引,.RootFS.Layers 就是 diff_ids。< / text >
< text x = "24" y = "342" font-size = "12.5" style = "fill:var(--muted)" > 图里没有 tag 与 image ID: 前者是索引的别名, 后者是本机给的身份, 两者都不进仓库。< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 一个镜像由哪几样东西组成: tag 先落到索引,索引按平台挑清单,清单指向配置和层< / div >
< / figure >
< p > 见图 1, 读法从左到右: tag 先落到索引,索引按平台挑一份清单,清单再指向配置和若干层。同一个 tag 在 amd64 与 arm64 上是两份不同的清单,但共享同一个索引。< / p >
< p > < span class = "tag" > 机制< / span > 配置里装的是运行参数与层顺序:< code > Env< / code > 、< code > Cmd< / code > 、< code > Entrypoint< / code > 、< code > Labels< / code > ,以及 < code > rootfs.diff_ids< / code > 。它和层一起被清单引用,配置本身不含任何文件内容。< / p >
< pre data-lang = "bash" data-name = "从本地视角看同一件事:架构、层数、体积" > $ docker image inspect -f '{{.Os}}/{{.Architecture}} layers={{len .RootFS.Layers}} size={{.Size}}' nginx:1.27-alpine
2026-10-14 22:42:38 +08:00
linux/amd64 layers=8 size=20984244 # 本机 Docker 29.6.1 + containerd 镜像存储实测;体积口径见 3.6
$ docker image inspect -f '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' nginx:1.27-alpine
["/docker-entrypoint.sh"] ["nginx" "-g" "daemon off;"]< / pre >
2026-10-25 23:26:58 +08:00
< p > 拿真实镜像核对一遍最清楚。< code > alpine:3.20< / code > 一个镜像身上有五处 sha256, 全不相同, 各回答一个不同的问题: < / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 你看到的< / th > < th > 它是谁的 sha256< / th > < th > 实测( alpine:3.20) < / th > < th > 什么时候会变< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > RepoDigests[0]< / code > 、< code > images --digests< / code > < / td > < td > 索引(多架构清单)< / td > < td > < code > sha256:d9e853e8…< / code > < / td > < td > 上游重新构建并推送< / td > < / tr >
< tr > < td > < code > .Id< / code > (本机 containerd 存储)< / td > < td > 索引; classic 存储下换成配置的 sha256< / td > < td > < code > sha256:d9e853e8…< / code > < / td > < td > 同上< / td > < / tr >
< tr > < td > 单平台清单 digest< / td > < td > 某个平台的清单< / td > < td > < code > sha256:c64c687c…< / code > < / td > < td > 该平台重建< / td > < / tr >
< tr > < td > config digest< / td > < td > 配置 JSON< / td > < td > < code > sha256:bf8527eb…< / code > < / td > < td > 运行参数或层顺序变< / td > < / tr >
< tr > < td > 层 digest< / td > < td > 压缩后的层< / td > < td > < code > sha256:25f1d6b1…< / code > < / td > < td > 层内容变< / td > < / tr >
< tr > < td > < code > RootFS.Layers< / code > ( diff_id) < / td > < td > 解压后的层< / td > < td > < code > sha256:08bc4e53…< / code > < / td > < td > 同上,但与上一行永不相等< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 最容易混的是最后两行:清单里记的是< b > 压缩后< / b > 的层摘要,配置的 < code > diff_ids< / code > 是< b > 解压后< / b > 的层摘要。同一层两个哈希,所以别拿清单的层 digest 去比 < code > RootFS.Layers< / code > 。< / p >
< p > < span class = "tag" > 代价< / span > 层一旦推进仓库就不能修改,只能往上加新层。两个后果:构建时多写的文件永远留在镜像里;同一个镜像也不能靠改层内容打补丁,只能重建。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box note" >
2026-10-25 23:26:58 +08:00
< span class = "bt" > < span class = "tag s" > 可跳过< / span > OCI 规范的字段全集< / span >
< p > 媒体类型记两个就够:清单是 < code > application/vnd.oci.image.manifest.v1+json< / code > ,多架构索引是 < code > ...image.index.v1+json< / code > 。老 Docker 格式对应 < code > application/vnd.docker.distribution.manifest.v2+json< / code > ,两者可以互推,但老工具只认自己那一种。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 镜像 = 几份 JSON 描述加一串按内容命名的只读层,写完再删等于没删。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-2" > 3.2 分层与写时复制< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 容器里改了个配置、装了个包,你担心把镜像弄坏;两个容器改了同一个文件,你担心互相覆盖。这两件事由同一条机制决定。< / p >
< p > < span class = "tag" > 怎么做< / span > 每个层只读,容器启动时在最上面加一个< b > 可写层< / b > :读文件时从上往下找,找到就用;写文件时若目标在下层,内核先把整份文件复制上来,这叫< mark > 写时复制( copy-up) < / mark > 。< / p >
< pre data-lang = "bash" data-name = "overlay2 的 lower/upper/merged 与可写层" > $ docker run -d --name cow nginx:1.27-alpine
2026-10-14 22:42:38 +08:00
$ docker inspect -f '{{json .GraphDriver.Data}}' cow | python3 -m json.tool
{
"LowerDir": "/var/lib/docker/overlay2/< id> /diff:< id> /diff",
"MergedDir": "/var/lib/docker/overlay2/< id> /merged",
"UpperDir": "/var/lib/docker/overlay2/< id> /diff",
"WorkDir": "/var/lib/docker/overlay2/< id> /work"
}
# 可写层( UpperDir) 随容器写入而增长; 容器一删, 这一层就没了
$ sudo du -sh /var/lib/docker/overlay2/< id> /diff
1.2M /var/lib/docker/overlay2/< id> /diff
# 若引擎启用了 containerd 镜像存储,上面第一条 inspect 会输出 null( Docker 29.6.1 实测)
$ docker rm -f cow< / pre >
2026-10-25 23:26:58 +08:00
< div class = "box note" >
< span class = "bt" > < span class = "tag s" > 可跳过< / span > overlayfs 的四个目录< / span >
< p > 上面这段要引擎使用 classic( graphdriver) 存储才看得到 < code > GraphDriver< / code > 字段。本机 Docker 29.6.1 用 containerd 镜像存储,实测该字段返回 < code > null< / code > ,要核对层就改用 < code > .RootFS.Layers< / code > 与清单。< / p >
< / div >
< figure class = "figure" >
< svg viewBox = "0 0 720 340" role = "img" aria-label = "三个容器共享同一份只读层,每个容器顶层各有一层可写层,读命中、写入复制、删除留白障" >
< defs >
< marker id = "ar03c" viewBox = "0 0 10 10" refX = "9" refY = "5" markerWidth = "7" markerHeight = "7" orient = "auto-start-reverse" >
< path d = "M0,0 L10,5 L0,10 z" style = "fill:var(--brand)" / >
< / marker >
< marker id = "ar03d" viewBox = "0 0 10 10" refX = "9" refY = "5" markerWidth = "7" markerHeight = "7" orient = "auto-start-reverse" >
< path d = "M0,0 L10,5 L0,10 z" style = "fill:var(--muted)" / >
< / marker >
< / defs >
< text x = "24" y = "28" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > 镜像的只读层:仓库里只存一份< / text >
< text x = "304" y = "28" font-size = "12.5" font-weight = "700" style = "fill:var(--muted)" > 三个容器各有一层可写层< / text >
< rect x = "24" y = "38" width = "250" height = "34" rx = "8" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "38" y = "60" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > layer 4< / text >
< text x = "262" y = "60" text-anchor = "end" font-size = "11" style = "fill:var(--muted)" > COPY entrypoint.sh< / text >
< rect x = "24" y = "80" width = "250" height = "34" rx = "8" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "38" y = "102" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > layer 3< / text >
< text x = "262" y = "102" text-anchor = "end" font-size = "11" style = "fill:var(--muted)" > /usr/share/nginx< / text >
< rect x = "24" y = "122" width = "250" height = "34" rx = "8" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "38" y = "144" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > layer 2< / text >
< text x = "262" y = "144" text-anchor = "end" font-size = "11" style = "fill:var(--muted)" > apk add nginx< / text >
< rect x = "24" y = "164" width = "250" height = "34" rx = "8" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "38" y = "186" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > layer 1< / text >
< text x = "262" y = "186" text-anchor = "end" font-size = "11" style = "fill:var(--muted)" > alpine 根文件系统< / text >
< text x = "24" y = "214" font-size = "12" style = "fill:var(--muted)" > 四层叠起来 = 根文件系统(只读)< / text >
< text x = "360" y = "64" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > 容器 A< / text >
< text x = "504" y = "64" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > 容器 B< / text >
< text x = "648" y = "64" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > 容器 C< / text >
< rect x = "304" y = "72" width = "112" height = "52" rx = "10" style = "fill:var(--warn-bg);stroke:var(--warn);stroke-dasharray:5 4" / >
< text x = "360" y = "94" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--warn)" > 可写层< / text >
< text x = "360" y = "112" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > 改 1 · 删 1< / text >
< rect x = "448" y = "72" width = "112" height = "52" rx = "10" style = "fill:var(--warn-bg);stroke:var(--warn);stroke-dasharray:5 4" / >
< text x = "504" y = "94" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--warn)" > 可写层< / text >
< text x = "504" y = "112" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > 空< / text >
< rect x = "592" y = "72" width = "112" height = "52" rx = "10" style = "fill:var(--warn-bg);stroke:var(--warn);stroke-dasharray:5 4" / >
< text x = "648" y = "94" text-anchor = "middle" font-size = "12.5" font-weight = "700" style = "fill:var(--warn)" > 可写层< / text >
< text x = "648" y = "112" text-anchor = "middle" font-size = "11" style = "fill:var(--muted)" > 空< / text >
< path d = "M360 126 V146" style = "stroke:var(--brand);fill:none" marker-end = "url(#ar03c)" / >
< path d = "M504 126 V146" style = "stroke:var(--brand);fill:none" marker-end = "url(#ar03c)" / >
< path d = "M648 126 V146" style = "stroke:var(--brand);fill:none" marker-end = "url(#ar03c)" / >
< rect x = "304" y = "150" width = "400" height = "34" rx = "8" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "504" y = "172" text-anchor = "middle" font-size = "12.5" style = "fill:var(--brand-ink)" > 共享的只读层:同一份,三个容器不复制< / text >
< path d = "M276 172 H300" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar03d)" / >
< text x = "24" y = "240" font-size = "12.5" style = "fill:var(--muted)" > 读:从可写层往下找,第一个命中的版本就是容器看到的文件——同一个镜像,三个容器读到同一份。< / text >
< text x = "24" y = "264" font-size = "12.5" style = "fill:var(--muted)" > 写: 改下层文件时整份先复制进可写层( copy-up) , 所以改一个字节也要付整个文件的代价。< / text >
< text x = "24" y = "288" font-size = "12.5" style = "fill:var(--muted)" > 删: 下层文件不消失, 可写层只记一个白障( whiteout) , 磁盘一个字节都不释放。< / text >
< text x = "24" y = "312" font-size = "12.5" style = "fill:var(--muted)" > 容器 A 改了 conf、删了 index.html: 镜像四层原封不动, 新容器看到的还是原文件。< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 分层与写时复制:三个容器共享一份只读层,读命中、写复制、删留白障< / div >
< / figure >
< p > 见图 2: 三个容器各有自己的可写层, 底下四层只读层只有一份。所以「容器之间互相看不见改动」与「同一份镜像省磁盘」是同一件事的两面, 并不矛盾。< / p >
< p > 把这三个动作在本机跑一遍,数字比描述可信:< / p >
< pre data-lang = "bash" data-name = "可写层实测: copy-up 与白障( Docker 29.6.1 实测)" > $ docker run -d --name cow nginx:1.27-alpine
$ docker ps -s --filter name=cow --format '{{.Names}} {{.Size}}'
cow 81.9kB (virtual 52.7MB)
# 只往一个 4.5 MB 的库文件末尾追加 1 个字节
$ docker exec cow sh -c 'printf x >> /usr/lib/libcrypto.so.3'
$ docker ps -s --filter name=cow --format '{{.Names}} {{.Size}}'
cow 4.62MB (virtual 57.2MB) # 可写层涨了 4.5 MB: 整份文件被复制上来
# 删掉镜像里的一个文件:可写层不会变小
$ docker exec cow rm /usr/share/nginx/html/index.html
$ docker diff cow | grep index.html
D /usr/share/nginx/html/index.html # D 就是白障
$ docker ps -s --filter name=cow --format '{{.Names}} {{.Size}}'
cow 4.62MB (virtual 57.2MB) # 一个字节都没释放
$ docker run --rm nginx:1.27-alpine wc -c /usr/share/nginx/html/index.html
615 /usr/share/nginx/html/index.html # 镜像里那份文件还在< / pre >
< p > 两次 < code > ps -s< / code > 的差值就是结论:改一个 4.5 MB 的文件只用追加一个字节,可写层涨了约 4.5 MB——被复制的是整份文件。删文件时体积一个字节都不降, 因为它只在上层留了个白障。< / p >
< p > < span class = "tag" > 机制< / span > 构建期同理: Dockerfile 每条 < code > RUN< / code > 、< code > COPY< / code > 提交一层,先在层 A 写、再到层 B 删,文件仍完整存在于层 A。把下载、解包、清理写进同一个 < code > RUN< / code > 才有效。< / p >
< pre data-lang = "dockerfile" data-name = "同一件事,写法不同体积差几十 MB" > # 反例:下载在层 A, 删除在层 B —— /tmp/tool.tgz 永远留在镜像里
2026-10-14 22:42:38 +08:00
FROM debian:12-slim
RUN curl -fsSL https://example.com/tool.tgz -o /tmp/tool.tgz
RUN tar -xzf /tmp/tool.tgz -C /usr/local & & rm -f /tmp/tool.tgz
# 正解:下载、解包、清理在同一个 RUN( 同一个层) , 文件从未进入任何层
FROM debian:12-slim
RUN curl -fsSL https://example.com/tool.tgz -o /tmp/tool.tgz & & \
tar -xzf /tmp/tool.tgz -C /usr/local & & \
rm -f /tmp/tool.tgz< / pre >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 代价< / span > 写时复制换来共享与秒级启动,代价是「写」比裸机贵:大文件改一个字节要整份复制,日志、数据库这类高频写目录必须挂卷,不能让它们落在可写层。挂载边界见 < a href = "06-storage.html" > 第 6 章< / a > 。< / p >
< p class = "take" > < b > 一句话:< / b > 改一个字节要复制整份文件,删一个文件只留一个白障——可写层只涨不跌。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-3" > 3.3 tag、image ID 与 digest< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 「这两个 tag 是不是同一个东西」「改了 tag 重新部署,怎么跑的仍是旧代码」——两个问题都出在把三种标识当成了一种。< / p >
< p > < span class = "tag" > 怎么做< / span > 先按稳定性排序: tag 会移动, image ID 随平台与存储实现变化,只有 digest 是内容算出来的、说不变就不变。用途随之分开:本地试玩用 tag, 跨环境对齐用 digest。< / p >
2026-10-14 22:42:38 +08:00
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 标识< / th > < th > 是什么< / th > < th > 会变吗< / th > < th > 该用在哪< / th > < / tr > < / thead >
< tbody >
2026-10-25 23:26:58 +08:00
< tr > < td > < code > ubuntu:24.04< / code > < / td > < td > 仓库名加 tag, 人写的指针< / td > < td > 会:上游重建后指向新 digest< / td > < td > 本地试玩、写文档示例< / td > < / tr >
< tr > < td > image ID < code > sha256:…< / code > < / td > < td > classic 存储下是 config 的 sha256< / td > < td > 内容变才变,但< strong > 与平台和存储实现相关< / strong > < / td > < td > 本地排查、< code > docker run< / code > 引用< / td > < / tr >
< tr > < td > repo digest < code > name@sha256:…< / code > < / td > < td > registry 里清单或索引的 sha256< / td > < td > 不变(内容寻址)< / td > < td > 生产部署、跨机器对齐、审计< / td > < / tr >
2026-10-14 22:42:38 +08:00
< / tbody >
< / table >
< / div >
2026-10-25 23:26:58 +08:00
< pre data-lang = "bash" data-name = "三个标识的实测关系" > $ docker inspect -f '{{.Id}}' nginx:1.27-alpine
2026-10-14 22:42:38 +08:00
sha256:< config digest> # 这就是 docker images 里的 IMAGE ID
$ docker inspect -f '{{index .RepoDigests 0}}' nginx:1.27-alpine
nginx@sha256:< index digest> # 这才是「这一份 nginx:1.27-alpine」的全球唯一标识
$ docker images --digests nginx
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
nginx 1.27-alpine sha256:< index digest> < 短 ID> 18 months ago 74.5MB
# 用 digest 拉取/运行:指定到内容,任何 tag 被覆盖都影响不到你
$ docker run --rm nginx@sha256:< index digest> nginx -v
nginx version: nginx/1.27.5< / pre >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 机制< / span > image ID 与 digest 不是同一个东西: classic 存储下 ID 是配置 JSON 的 sha256, digest 是清单(或索引)的 sha256, 两者永远不同。containerd 存储下 Docker 报的 ID 更接近索引 digest——本机实测 < code > .Id< / code > 、< code > .RepoDigests[0]< / code > 、< code > images --digests< / code > 三列完全相同。< / p >
< p > 所以「ID 一样」证明不了是同一份镜像, 「digest 一样」才能。多架构镜像的索引 digest 又与任一单平台清单的 digest 不同,跨平台比较时先看清在哪一层。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · latest 不是最新, tag 也不是版本号< / span >
< p > < code > latest< / code > 只是「没写 tag 时的默认值」,与版本新旧无关:它可以指向 2021 年的构建,也可以被推成任何内容。把 < code > latest< / code > 当「最新稳定版」用,等于把一个默认参数当成契约。< / p >
< p > < strong > 验证:< / strong > < code > docker image inspect -f '{{index .RepoDigests 0}}' 镜像:latest< / code > , 隔一周再比一次, digest 变了说明指针被移动过。< strong > 解决:< / strong > tag 用「日期 + git 短 SHA」, 仓库开启不可变 tag 策略,部署清单写 digest。< / p >
< / div >
< p > 顺带一个常被忽略的点:< code > docker tag< / code > 不复制任何数据,只是给同一份内容再加一个名字。所以「两个 tag 的 ID 一样」是正常的,它们本来就指向同一份配置。< / p >
< p > < span class = "tag" > 代价< / span > 选错对齐层面比不对齐更贵。用 ID 对齐会随存储实现失效,用 tag 对齐会随上游重建失效;只有 digest 两样都不失效,但它不好读,必须由流水线写进部署清单。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box case" >
< span class = "bt" > 真实事故的形状:回滚回不去< / span >
2026-10-25 23:26:58 +08:00
< p > 上线用 < code > myapp:latest< / code > ,出问题后想回滚,于是把「上一次的镜像」重新 < code > run< / code > 一遍——但 < code > latest< / code > 早在这次构建时被覆盖,本地旧镜像也已 < code > docker image prune -a< / code > 清掉,最后只能重新构建一份「大概和上周一样」的代码。< / p >
< p > 改用 < code > myapp:2025-01-06-a1b2c3d< / code > (日期加 git 短 SHA) 并开启不可变 tag 策略后,回滚就是把旧 tag 重新部署一次, 30 秒内完成且能对上天。指针可被移动,依赖指针就等于依赖运气。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > tag 是活的指针, digest 是死的身份证;要证明是同一份,永远比 digest。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-4" > 3.4 多架构镜像与 manifest list< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 开发机是 Apple Silicon, CI 与生产是 x86_64。同一个 tag, 本地跑得好好的镜像推到生产, 报 < code > no matching manifest for linux/amd64 in the manifest list entries< / code > 。< / p >
< p > < span class = "tag" > 怎么做< / span > 把多个平台的清单挂到同一个 tag 下( manifest list 或 OCI index) , 客户端按自己的平台自动挑。构建需要 buildx 的容器驱动与 QEMU 模拟:< / p >
< pre data-lang = "bash" data-name = "一次构建两个平台并推送" > $ docker buildx create --name multiarch --driver docker-container --use
2026-10-14 22:42:38 +08:00
$ docker buildx build \
--platform linux/amd64,linux/arm64 \
-t registry.example.com/app/api:2025-01-06-a1b2c3d \
--push .
$ docker buildx imagetools inspect registry.example.com/app/api:2025-01-06-a1b2c3d
# 输出里应能看到 linux/amd64 与 linux/arm64 两个 Platform 条目< / pre >
2026-10-25 23:26:58 +08:00
< p > 三个容易踩的点:< code > --load< / code > 只能装载当前平台,多平台必须 < code > --push< / code > ;默认生成的 attestation 清单会混进索引,老客户端可能因此报错,可加 < code > --provenance=false --sbom=false< / code > 关掉;平台相关的参数用 < code > ARG TARGETARCH< / code > ,别把架构硬编码进 Dockerfile。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 忘了 --platform 当场不报错,运行时才 exec format error< / span >
< p > 索引里没有你要的平台时, docker 先按本地平台拉,本地跑通了就以为没事;等镜像被拉到架构不对的节点上,容器启动直接 < code > exec format error< / code > ,或者更糟——在 QEMU 模拟下能跑,但慢得离谱。< / p >
< p > < strong > 验证:< / strong > 把 < code > docker image inspect -f '{{.Os}}/{{.Architecture}}' 镜像< / code > 与目标节点 < code > uname -m< / code > 对一遍。< strong > 解决:< / strong > 构建时显式 < code > --platform< / code > ,推送后用 < code > docker buildx imagetools inspect< / code > 确认各平台清单都在。< / p >
< / div >
< pre data-lang = "bash" data-name = "架构不一致时的现象与代价" > # 在 arm64 笔记本上强跑 amd64 镜像:能跑,但是模拟执行
2026-10-14 22:42:38 +08:00
$ docker run --rm --platform linux/amd64 alpine:3.20 uname -m
x86_64
$ docker run --rm alpine:3.20 uname -m
aarch64
# 反例:忘记加 --platform 的构建,推送后平台只有一个
$ docker build -t registry.example.com/app/api:2025-01-06-a1b2c3d .
$ docker image inspect -f '{{.Os}}/{{.Architecture}}' registry.example.com/app/api:2025-01-06-a1b2c3d
linux/arm64 # 生产节点是 amd64, 部署时才会失败< / pre >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 代价< / span > 模拟执行不是验证手段:它走 QEMU, 性能只有原生的几分之一, 依赖 CPU 指令或 < code > /proc/cpuinfo< / code > 的库还会行为漂移。用它做「能不能启动」的检查可以,做性能测试或上线依据不行。< / p >
< div class = "box note" >
< span class = "bt" > < span class = "tag s" > 可跳过< / span > 多架构清单的完整 JSON< / span >
< p > 索引里除了各平台清单,还常混进 < code > unknown/unknown< / code > 平台的 attestation 清单( provenance 与 SBOM) 。实测 < code > alpine:3.20< / code > 的索引里是 8 个平台清单加 8 个 attestation, 它们不参与运行, 却会让「数平台个数」的脚本多算一倍。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 多架构的正确性在构建时决定,漏了 --platform, 错误只在运行时出现。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-5" > 3.5 pull / push / save / load / tag / rmi< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 要给隔离机房搬一套镜像,或把 CI 构建好的镜像推进私有仓库。前者走文件,后者走网络,命令不同,坑也不同。< / p >
< p > < span class = "tag" > 怎么做< / span > 按两个维度记:在线分发用 < code > pull< / code > 、< code > push< / code > ,按 digest 并行传,已有的层自动跳过;离线分发用 < code > save< / code > 、< code > load< / code > ,打成 tar 走 U 盘。改名用 < code > tag< / code > ,不搬数据。< / p >
< pre data-lang = "bash" data-name = "在线分发:拉取与推送" > $ docker pull nginx:1.27-alpine
2026-10-14 22:42:38 +08:00
1.27-alpine: Pulling from library/nginx
Digest: sha256:< index digest>
Status: Downloaded newer image for nginx:1.27-alpine
$ docker push registry.example.com/app/api:2025-01-06-a1b2c3d
The push refers to repository [registry.example.com/app/api]
< 层 ID> : Layer already exists # 基础镜像的层:仓库里已有,不重复上传
< 层 ID> : Pushed # 只有业务层真正上传
2025-01-06-a1b2c3d: digest: sha256:< index digest> size: 1572< / pre >
2026-10-25 23:26:58 +08:00
< pre data-lang = "bash" data-name = "离线分发: save / load 与它的格式差异" > $ docker save nginx:1.27-alpine | gzip > nginx-1.27-alpine.tgz
2026-10-14 22:42:38 +08:00
$ ls -lh nginx-1.27-alpine.tgz
-rw-r--r-- 1 dev dev 19M Jan 6 09:41 nginx-1.27-alpine.tgz
$ gunzip -c nginx-1.27-alpine.tgz | docker load
Loaded image: nginx:1.27-alpine
$ docker save -o all.tar nginx:1.27-alpine postgres:16-alpine # 多个镜像打一个包
$ tar -tf all.tar | head -5
# classic( graphdriver) 存储: 层目录 + manifest.json + repositories
< 层 ID> /layer.tar
manifest.json
repositories
# containerd 镜像存储: OCI 布局(本机 Docker 29.6.1 实测)
$ docker save alpine:3.20 -o alpine.tar & & tar -tf alpine.tar
blobs/
blobs/sha256/
blobs/sha256/1db32a7978cab674f63e3f771e00e092017d50892822fbba4f338fefdd9ecb05
… # 各层、config、manifest 都是按 digest 命名的 blob
index.json
manifest.json # OCI 布局里也会附带一份兼容用的 manifest.json
oci-layout
# 需要指定平台时用 --platform( 官方支持 "Save only the given platform(s)")
$ docker save --platform linux/amd64 -o api-amd64.tar registry.example.com/app/api:2025-01-06-a1b2c3d< / pre >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 机制< / span > < code > save< / code > 打包的是「本地镜像存储里的东西」,产物随存储实现不同。本机实测 < code > docker save nginx:1.27-alpine< / code > 解出来同时有两套描述: OCI 布局的 < code > index.json< / code > 、< code > oci-layout< / code > 、< code > blobs/sha256/< / code > ,以及给老工具用的 < code > manifest.json< / code > ,两条路径指向同一批 blob。< / p >
< p > 所以「save 出的包能不能被目标机 load」要先验证: 跨引擎( Docker 与 Podman、containerd 之间)或跨大版本搬运前,先在一台机器上完整走一遍 save 到 load, 比到了机房才发现解不开便宜得多。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 删 tag 不等于删镜像< / span >
< p > < code > docker rmi 名字:tag< / code > 只是把标签摘掉;只有当它是最后一个引用时,镜像记录才真正消失,层还要等没有镜像再用它们才回收。有容器引用时更直接,当场拒绝:< / p >
< pre data-lang = "bash" data-name = "删 tag 与删镜像的差别(本机实测)" > $ docker rmi nginx:1.27-alpine
Error response from daemon: conflict: unable to delete nginx:1.27-alpine (must be forced) - container 84b20219d3f9 is using its referenced image 65645c7bb6a0
$ docker rmi demo:1 # 最后一个 tag 被摘掉时,镜像记录才一起消失
Untagged: demo:1
Deleted: sha256:be25bed8882f935897fe050866201fe2c629bfb2838c172da1ac4839915d512e< / pre >
< p > < strong > 解决:< / strong > 先删容器再删镜像;要批量回收,用 < code > docker image prune< / code > (只删悬空)或 < code > -a< / code > (连没有容器引用的 tag 镜像一起删,会误伤基础镜像)。< / p >
< / div >
< div class = "box warn" >
< span class = "bt" > 易错点 · pull 了新 tag, 旧镜像变成 < none> 还占着盘< / span >
< p > 同名 tag 被覆盖后,旧的一版不会消失,只是失去名字,在 < code > docker images< / code > 里显示成 < code > < none> :< none> < / code > (悬空镜像)。它仍占磁盘,而且层被共享,删它省下的空间往往比你估的少。< / p >
< p > < strong > 验证:< / strong > < code > docker images -f dangling=true< / code > 。< strong > 解决:< / strong > 定期 < code > docker image prune< / code > ,并在 CI 上保留最近 N 个 tag, 而不是无脑加 < code > -a< / code > 。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< div class = "box danger" >
< span class = "bt" > 磁盘被镜像吃满时的正确顺序< / span >
2026-10-25 23:26:58 +08:00
< p > 先 < code > docker system df -v< / code > 看清是镜像、容器、卷还是构建缓存占的;再删无用容器与已停止容器;然后 < code > docker image prune< / code > ;最后才考虑 < code > -a< / code > ,并配合「保留最近 N 个 tag」的策略。< / p >
< p > 卷里的数据 < code > prune< / code > 不会帮你判断重要性,误删就是数据丢失,参见 < a href = "06-storage.html" > 第 6 章< / a > 。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 代价< / span > 两条路都不替你管垃圾。pull 覆盖掉的旧 tag、save 留下的 tar、rmi 摘掉的名字,没有一步会自动回收;不写清理策略,盘就是慢慢被吃掉的。< / p >
< p class = "take" > < b > 一句话:< / b > pull 走网络、save 走文件,两者都不顺手删旧内容;想省盘得显式 prune。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-6" > 3.6 读懂镜像: inspect / history / 体积归因< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 镜像膨胀是慢性病:服务不会挂,只是拉取变慢、发布变长、回滚窗口变大。有人问「这个镜像为什么这么大」,四个答案都对,取决于按哪个口径算。< / p >
< p > < span class = "tag" > 怎么做< / span > 定位固定三步: 先看总量与层数, 再看每层是哪条指令产生的, 最后进容器里量目录。Dockerfile 侧的优化手段集中在 < a href = "04-dockerfile.html" > 第 4 章< / a > 。< / p >
< pre data-lang = "bash" data-name = "三步体积归因( Docker 29.6.1 + containerd 镜像存储实测)" > # ① 总量与层数
2026-10-14 22:42:38 +08:00
$ docker image ls --format '{{.Repository}}:{{.Tag}}\t{{.Size}}' | sort -k2 -h | tail -5
$ docker image inspect -f '{{len .RootFS.Layers}} layers, {{.Size}} bytes' nginx:1.27-alpine
8 layers, 20984244 bytes # ≈ 20 MB: containerd 存储报的是压缩后的内容体积
# ② 每层是哪条指令产生的、各占多大( nginx 的体积几乎全在那一条 apk add 上)
$ docker history --no-trunc --format '{{.Size}}\t{{.CreatedBy}}' nginx:1.27-alpine | head -5
38.7MB RUN /bin/sh -c set -x & & apkArch="$(cat /etc/apk/arch)" & & nginxPackages="…" & & apk add … # buildkit
0B ENV NJS_RELEASE=1
0B ENV NJS_VERSION=0.8.10
0B CMD ["nginx" "-g" "daemon off;"]
0B STOPSIGNAL SIGQUIT
# ③ 进容器里量目录(比看总大小更直接)
$ docker run --rm --entrypoint sh nginx:1.27-alpine -c 'du -sk /usr/* /var/* 2> /dev/null | sort -n | tail -5'
20 /var/cache
2508 /usr/bin
2652 /usr/sbin
4600 /usr/share
37876 /usr/lib
# 结论:体积集中在 /usr/lib、/usr/bin 这类目录时,就该考虑换更小的基础镜像或做多阶段构建< / pre >
2026-10-25 23:26:58 +08:00
< p > 同一台机器、同一个镜像,四个数字都能叫「镜像大小」:< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 这个数字< / th > < th > 本机实测( nginx:1.27-alpine) < / th > < th > 怎么来的< / th > < / tr > < / thead >
< tbody >
< tr > < td > 清单里各层压缩大小相加< / td > < td > 20,960,631 字节,约 21 MB< / td > < td > 逐层读清单的 size 字段< / td > < / tr >
< tr > < td > < code > docker image inspect .Size< / code > < / td > < td > 20,984,244 字节< / td > < td > 上面之和再加元数据< / td > < / tr >
< tr > < td > 各层解压后相加< / td > < td > 49,633,280 字节,约 49.6 MB< / td > < td > 逐层 gunzip 后统计< / td > < / tr >
< tr > < td > < code > docker history< / code > 各行相加< / td > < td > 约 52.6 MB< / td > < td > 构建器记录的每步写入量< / td > < / tr >
< tr > < td > < code > docker image ls< / code > 的 DISK USAGE< / td > < td > 74.5 MB< / td > < td > 引擎统计的实际占用,口径最宽< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 差在口径:< code > .Size< / code > 算压缩后的内容, history 每行是「这一步写入了多少」,解压落盘是另一回事,< code > DISK USAGE< / code > 又是引擎的实际统计。跨机器、跨存储比体积,几乎必然得出错的结论。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · docker history 的 SIZE 不是这一层的独占大小< / span >
< p > 它显示「这一层相对父层写入了多少」,不是「删掉这一层能省多少」。所以拿 history 各行相加去预测磁盘收益会高估; 被上层白障删掉的文件仍留在下层, history 里也看不见负数。< / p >
< p > < strong > 验证:< / strong > 把各行 SIZE 相加,再与 < code > .Size< / code > 、< code > docker ps -s< / code > 的 virtual 值分别对比,三者不相等是常态。< strong > 解决:< / strong > 用 history 定位「哪一步大」,用 < code > .Size< / code > 定预算,用 < code > du< / code > 查目录。< / p >
< / div >
< p > 想看清「每层到底装了什么」,用 < code > dive< / code > 按层浏览,或 < code > docker save< / code > 出 tar 一层层解。前者适合日常,后者适合事故复盘。< / p >
< p > < span class = "tag" > 代价< / span > 口径不统一时,优化会做在错的地方。为 < code > DISK USAGE< / code > 去删共享层,常常只省下几 MB; 真正该压的是 history 里最大的那一步。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box ok" >
< span class = "bt" > 体积预算的做法< / span >
2026-10-25 23:26:58 +08:00
< p > 给每类服务定一个体积上限(例如 API 服务 150 MB、前端静态站 60 MB) , 在 CI 里加一条 < code > docker image inspect -f '{{.Size}}'< / code > 的比较,超过就失败或告警。< / p >
< p > 体积失控通常是三种原因:把编译工具装进了运行镜像、< code > COPY< / code > 了整个仓库、基础镜像从 slim 换成了 full。靠预算比靠人盯可靠。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 四个数字都能叫镜像大小,先问口径,再谈优化。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-3-7" > 3.7 镜像来源可信度< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 镜像是你直接执行的代码,通常还以 root 启动。所以「这个镜像能不能用」不该靠感觉,而该是可逐条核对的事实。< / p >
< p > < span class = "tag" > 怎么做< / span > 按可控程度排序:官方镜像最好,其次是厂商命名的命名空间,社区镜像最差。前两类也要固定 digest 并把扫描结果纳入基线,第三类要么自己构建,要么只当参考。< / p >
2026-10-14 22:42:38 +08:00
< div class = "cards" >
< div class = "card" >
< h4 > 官方镜像( library/*) < / h4 >
< ul >
< li > < b > 依据< / b > : Docker 维护, Dockerfile 公开在 < code > docker-library< / code > 仓库,有自动化重建与扫描< / li >
< li > < b > 用法< / b > : < code > nginx:1.27-alpine< / code > 、< code > postgres:16-alpine< / code > 这类,固定到具体小版本< / li >
< / ul >
< / div >
< div class = "card" >
< h4 > Verified Publisher / 官方厂商< / h4 >
< ul >
< li > < b > 依据< / b > :厂商自己维护的命名空间,有企业背书与发布流程< / li >
< li > < b > 注意< / b > :仍要固定 digest, 并把扫描结果纳入你的基线< / li >
< / ul >
< / div >
< div class = "card" >
< h4 > 社区镜像< / h4 >
< ul >
< li > < b > 风险< / b > :可能内置挖矿、后门、过期基础镜像;也可能是别人随手推的个人项目< / li >
2026-10-25 23:26:58 +08:00
< li > < b > 替代< / b > :以其源码自己构建,或改用官方镜像加一层薄 Dockerfile< / li >
2026-10-14 22:42:38 +08:00
< / ul >
< / div >
< / div >
2026-10-25 23:26:58 +08:00
< p > 拿到陌生镜像的两分钟检查:看有没有可追溯的源码标签(< code > org.opencontainers.image.source< / code > )、层历史里有没有可疑动作(< code > curl … | sh< / code > 、base64 解码后执行、装了 < code > nc< / code > )、以及高危漏洞是否成堆。< / p >
< pre data-lang = "bash" data-name = "扫描与签名验证" > # 漏洞扫描(示例用 trivy 与 docker scout, 任选其一)
2026-10-14 22:42:38 +08:00
$ trivy image --severity HIGH,CRITICAL --ignore-unfixed nginx:1.27-alpine
$ docker scout quickview nginx:1.27-alpine
# 验证镜像确实来自你信任的 CI 流水线( keyless 签名)
$ cosign verify \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
--certificate-identity https://github.com/example/api/.github/workflows/release.yml@refs/heads/main \
registry.example.com/app/api:2025-01-06-a1b2c3d
# 看镜像里的元数据与来源标注
$ docker image inspect -f '{{json .Config.Labels}}' registry.example.com/app/api:2025-01-06-a1b2c3d< / pre >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 机制< / span > 分发链上还有两个现实约束。一是限流: Docker Hub 按 IP 或账户计拉取配额,超额返回 429, CI 里必须登录或走缓存代理。二是第三方加速器:< mark > 按 < code > name@sha256:…< / code > 拉时内容不可篡改< / mark > ,但< mark > 按 tag 拉时「tag 到清单」这一步由 mirror 决定< / mark > ,它能返回与上游不同的清单,客户端不会报错。< / p >
< p > 所以用 mirror 的前提是部署清单写死 digest; 企业环境更该用 Harbor、ECR、Artifactory 这类自己的 pull-through 缓存,源头可控且可审计。< / p >
< p > < span class = "tag" > 代价< / span > 链条每加一环都要有人维护。固定 digest 要有人升级基础镜像, 签名要有人守住身份与密钥, SBOM 要有人看;断在维护上的链条,比断在技术上更多。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box ok" >
< span class = "bt" > 上线前的最小可信链条< / span >
2026-10-25 23:26:58 +08:00
< p > 基础镜像固定到 digest, 私有仓库开启不可变 tag, CI 构建后签名并生成 SBOM, 部署清单写 digest 而不是 tag, 定期基于最新补丁重建基础镜像并重跑扫描。< / p >
< p > 漏洞治理与供应链完整做法见 < a href = "10-security.html" > 第 10 章< / a > ;仓库侧的标签策略与限流配置见 < a href = "09-registry.html" > 第 9 章< / a > 。< / p >
< / div >
< p class = "take" > < b > 一句话:< / b > 可信度是可验证的事实链:固定 digest、签名、扫描, 缺一环就只剩感觉。< / p >
< h2 id = "ch3-sum" > 全章回顾:三个问题、两张图< / h2 >
< div class = "box case" >
< span class = "bt" > 三句话说完这一章< / span >
< p > < b > 第一句:< / b > 镜像 = 索引到清单再到配置加层,每样按自己的 sha256 命名;层只读,所以「先写再删」永远删不掉体积。< / p >
< p > < b > 第二句:< / b > 写时复制让三个容器共享一份只读层:读命中最上层的可见版本,写要整份复制,删只留白障。< / p >
< p > < b > 第三句:< / b > tag 会移动、image ID 随环境变,只有 digest 能证明「是同一份」;镜像变大只发生在写入那一刻。< / p >
< / div >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 症状< / th > < th > 先查什么< / th > < th > 回哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > 镜像 21 MB, 机器上却占 74 MB< / td > < td > 先把四个体积口径对齐,别拿 < code > .Size< / code > 当磁盘占用< / td > < td > 3.6< / td > < / tr >
< tr > < td > 两个 tag 的 IMAGE ID 一样,算同一个镜像吗< / td > < td > 比 digest; ID 只代表同一份配置< / td > < td > 3.3< / td > < / tr >
< tr > < td > 改了 tag 重新部署,跑的还是旧代码< / td > < td > 部署清单写的是 tag 还是 digest, 节点上有没有旧的层< / td > < td > 3.3 / 3.5< / td > < / tr >
< tr > < td > 构建或拉取报 no matching manifest< / td > < td > 索引里有没有对应平台的清单,< code > --platform< / code > 写没写< / td > < td > 3.4< / td > < / tr >
< tr > < td > 容器一启动就 exec format error< / td > < td > 拉错架构:比对 < code > uname -m< / code > 与镜像的 < code > .Architecture< / code > < / td > < td > 3.4< / td > < / tr >
< tr > < td > < code > docker images< / code > 里一堆 < code > < none> < / code > < / td > < td > 被覆盖的旧 tag, prune 才回收< / td > < td > 3.5< / td > < / tr >
< tr > < td > 删了文件镜像也没变小< / td > < td > 文件在上层删、下层还在(白障)< / td > < td > 3.2 / 3.6< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box tip" >
< span class = "bt" > 一分钟讲完这一章< / span >
< p > 先讲骨架: tag 落到索引, 索引按平台挑清单, 清单指向配置和层, 层按内容命名。再讲两个数字: image ID 不等于 digest; history 的 SIZE 不是独占大小。< / p >
< p > 最后用两张图收口:图 1 是「有哪些东西」,图 2 是「三个容器怎么共享」——排障时先分清是标识问题还是层问题。< / p >
2026-10-14 22:42:38 +08:00
< / div >
< / article >
< nav class = "pager" >
< a class = "prev" href = "02-install.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 2 章 · 安装、配置与命令心智模型< / span > < / a >
< a class = "next" href = "04-dockerfile.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 4 章 · Dockerfile 全解:从一条指令到可复现的镜像< / span > < / a >
< / nav >
< p class = "foot" >
Docker 专题 · 第 3 章 镜像:分层、标识与分发格式< br >
2026-10-15 11:55:58 +08:00
< a href = "index.html" > 总目录< / a > · < a href = "../../SRE工程师/sre-learning-path/index.html" > SRE 学习路径< / a >
2026-10-14 22:42:38 +08:00
< / p >
< / main >
< / div >
< button id = "toTop" title = "回到顶部" > ↑< / button >
< script src = "assets/doc.js" > < / script >
< script src = "../../assets/back.js" data-portal-back = "knowledge/docker" data-portal-label = "知识库 › Docker 专题知识库" > < / script >
< / body >
< / html >