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 > 第 6 章 存储与数据持久化 · Docker 专题知识库< / title >
< meta name = "description" content = "本章讲清容器可写层为什么保不住数据, volume、bind mount、tmpfs 三种挂载怎么选,-v 与 --mount 的行为差异,命名卷的 tar 备份与数据库逻辑备份、UID/GID 权限对齐、只读根文件系统,以及 macOS/WSL2 上的挂载性能取舍。" >
< 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 > 第 6 章< / 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 > < a class = "toc-a" href = "03-image.html" > < i class = "cn" > 3< / i > 镜像:分层、标识与分发格式< / a > < 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 > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 6< / i > 存储与数据持久化< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-6-1" > 6.1 可写层与「数据会丢」的真相< / a > < a class = "toc-a lv3" href = "#sec-6-2" > 6.2 volume / bind mount / tmpfs 三选一< / a > < a class = "toc-a lv3" href = "#sec-6-3" > 6.3 -v 与 --mount 的差别< / a > < a class = "toc-a lv3" href = "#sec-6-4" > 6.4 命名卷的生命周期与备份恢复< / a > < a class = "toc-a lv3" href = "#sec-6-5" > 6.5 开发场景:源码挂载与热重载< / a > < a class = "toc-a lv3" href = "#sec-6-6" > 6.6 UID/GID 与文件权限难题< / a > < a class = "toc-a lv3" href = "#sec-6-7" > 6.7 只读挂载< / a > < a class = "toc-a lv3" href = "#sec-6-8" > 6.8 性能与跨平台文件系统< / a > < / div > < 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" > 6< / b > / 14 章< / span >
< / p >
< h2 id = "ch6" > < span class = "n" > 6< / span > 存储与数据持久化< / h2 >
2026-10-25 23:26:58 +08:00
< div class = "box ok" >
< span class = "bt" > 面试 / 实战直答 · 30 秒版< / span >
< p > 容器里的数据默认写在< strong > 可写层< / strong > ,它和容器同生共死,< code > docker rm< / code > 之后一定没有。< / p >
< p > 要留住数据只有一条路:挂载。要备份、要跨容器共享用< strong > 命名卷< / strong > ;要用宿主机编辑器直接改文件用< strong > 绑定挂载< / strong > ;不能落盘用 < strong > tmpfs< / strong > 。< / p >
< p > 两类高频事故都出在「以为挂上了」:路径写错被静默建成一个空目录,以及容器内 UID 与宿主机目录属主对不上。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< div class = "lead" >
2026-10-25 23:26:58 +08:00
< b > 这一章解决什么:< / b > 容器默认把数据写在随容器一起销毁的可写层里,这是「删了容器数据就没了」的根源。
2026-10-14 22:42:38 +08:00
学完你能判断一个目录该用命名卷、绑定挂载还是 tmpfs, 能亲手完成一次< strong > 可验证< / strong > 的备份与恢复,
能解掉「permission denied」和「数据库目录挂到宿主机目录后慢十倍」这两类高频问题。
< / div >
2026-10-25 23:26:58 +08:00
< h4 > 读之前:六个词< / h4 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 术语< / th > < th > 一句话解释< / th > < th > 在哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > 可写层< / td > < td > 启动时叠在镜像最上面的空层,写进去的东西随容器删除一起消失< / td > < td > 6.1< / td > < / tr >
< tr > < td > 命名卷< / td > < td > Docker 管理的持久目录,有名字,删容器不影响,可以被下一个容器复用< / td > < td > 6.2 / 6.4< / td > < / tr >
< tr > < td > 匿名卷< / td > < td > 同样是卷,但名字是随机哈希,每次 < code > run< / code > 新建一个,记不住也就没法复用< / td > < td > 6.2< / td > < / tr >
< tr > < td > 绑定挂载< / td > < td > 把宿主机上一个真实路径直接映射进容器,改文件立刻生效< / td > < td > 6.2 / 6.5< / td > < / tr >
< tr > < td > tmpfs< / td > < td > 只存在于内存里的挂载,容器停止即消失,容量算在内存限额里< / td > < td > 6.2< / td > < / tr >
< tr > < td > UID / GID< / td > < td > 容器内进程的身份数字,决定它能写哪些挂载目录< / td > < td > 6.6< / td > < / tr >
< tr > < td > 只读挂载< / td > < td > 单个挂载点加 < code > ro< / code > ,或整个根文件系统加 < code > --read-only< / code > < / td > < td > 6.7< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box tip" >
< span class = "bt" > 怎么读这一章< / span >
< p > 每节都是同一个形状:< b > 场景< / b > → < b > 怎么做< / b > → < b > 代价< / b > ,最后一句可以直接背走;时间紧就只读那一句。< / p >
< p > 先看两张图:< b > 图 2< / b > ( 6.1)是「数据在哪一刻消失」的时间线,< b > 图 1< / b > ( 6.2)是三种挂载的空间对照。标了小标签的段落是引擎与平台细节,第一遍整块跳过不影响后面任何一节。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-1" > 6.1 可写层与「数据会丢」的真相< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 你在容器里跑了一次数据库迁移,或者上传了几个文件;容器一删就全没了。更迷惑的是 < code > docker restart< / code > 之后数据居然还在,于是有人得出「数据是持久的」这个结论。< / p >
< p > < span class = "tag" > 怎么做< / span > 先看清结构:镜像的每一层都只读,容器启动时 Docker 在镜像最上面叠一层< strong > 可写层< / strong > ( overlay2 里叫 upperdir) 。进程写进去的一切——上传的文件、SQLite 库、日志——都落在这一层。< / p >
< p > 这一层的寿命< b > 等于容器本身< / b > : < code > stop< / code > 后还在,< code > start< / code > 回来还在,< code > rm< / code > 之后立刻消失。这解释了那个矛盾现象——重启只是换了个进程,删容器才是把文件系统一起扔掉。< / p >
< p > 写时复制( copy-on-write) 是它的第二个代价: 改镜像里的文件, 要先把整份文件复制到可写层再改。所以在容器里改一个 500MB 的文件,既慢,又让容器凭空胖 500MB。< / p >
< p > < code > docker ps -s< / code > 的 < code > SIZE< / code > 列就是每个容器可写层实际占的磁盘。它不属于任何镜像,也不会被别的容器复用。< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "bash" data-name = "数据留在可写层会发生什么" > # 写进可写层
$ docker run --rm --name t1 alpine:3.20 sh -c 'echo hello > /data.txt'
# 容器已被删除,数据随之消失
$ docker run --rm alpine:3.20 cat /data.txt
cat: can't open '/data.txt': No such file or directory
# 挂载之后,数据才「活得比容器久」
$ docker run --rm -v demo-data:/data alpine:3.20 sh -c 'echo hello > /data/a.txt'
$ docker run --rm -v demo-data:/data alpine:3.20 cat /data/a.txt
hello< / pre >
2026-10-25 23:26:58 +08:00
< figure class = "figure" >
< svg viewBox = "0 0 720 300" role = "img" aria-label = "可写层的生命周期:从 create 到 rm 一直存在, rm 时连同容器一起删除;命名卷不在容器的时间线上,删容器不受影响" >
< text x = "24" y = "26" font-size = "13" style = "fill:var(--muted)" > 可写层:从 create 到 rm, 只有最后一步会让数据消失< / text >
< rect x = "24" y = "48" width = "112" height = "44" rx = "9" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "80" y = "68" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > create< / text >
< text x = "80" y = "84" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 建容器< / text >
< rect x = "160" y = "48" width = "112" height = "44" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "216" y = "68" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--brand-ink)" > running< / text >
< text x = "216" y = "84" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 进程在跑< / text >
< rect x = "296" y = "48" width = "112" height = "44" rx = "9" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "352" y = "68" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > stop< / text >
< text x = "352" y = "84" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 进程退出< / text >
< rect x = "432" y = "48" width = "112" height = "44" rx = "9" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "488" y = "68" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > start< / text >
< text x = "488" y = "84" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 再启动< / text >
< rect x = "568" y = "48" width = "112" height = "44" rx = "9" style = "fill:none;stroke:var(--line)" stroke-dasharray = "5 4" / >
< text x = "624" y = "68" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--muted)" > rm< / text >
< text x = "624" y = "84" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 删容器< / text >
< rect x = "24" y = "104" width = "520" height = "40" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "40" y = "130" font-size = "12.5" style = "fill:var(--brand-ink)" > 可写层存在:写进去的文件都落在这一层< / text >
< rect x = "568" y = "104" width = "112" height = "40" rx = "9" style = "fill:none;stroke:var(--danger)" stroke-dasharray = "5 4" / >
< text x = "624" y = "130" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > rm 时删掉< / text >
< rect x = "24" y = "160" width = "656" height = "40" rx = "9" style = "fill:var(--card);stroke:var(--ok)" / >
< text x = "40" y = "186" font-size = "12.5" style = "fill:var(--ink)" > 命名卷 / 绑定挂载: 不在这条时间线上, rm 不影响它们< / text >
< text x = "24" y = "228" font-size = "12.5" style = "fill:var(--ink)" > stop / start 只换进程, 可写层原封不动; rm 才连可写层一起删。< / text >
< text x = "24" y = "254" font-size = "12.5" style = "fill:var(--muted)" > 数据写在可写层里,容器一删就没;写在命名卷里,删容器只是换了个壳。< / text >
< text x = "24" y = "280" font-size = "12.5" style = "fill:var(--muted)" > 先确认挂了什么再怀疑代码: docker inspect -f '{{json .Mounts}}' 容器名。< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 可写层的生命周期:哪一步之后数据还在,哪一步之后一定没了,命名卷为什么不在同一条线上< / div >
< / figure >
< p > 见图 1, 数据「什么时候会丢」摊在一条时间线上: 可写层从创建那一刻出现, 一直活到 < code > rm< / code > ; < code > stop< / code > 与 < code > start< / code > 只换进程,不碰它。< / p >
< p > 下面那条带子是同一条时间线上的命名卷——它不归容器管,删容器时毫发无损。挂载唯一要做的事,就是把数据挪到这条带子上。< / p >
< p > 还有一条与可写层直接相关的规则: Dockerfile 里写了 < code > VOLUME /data< / code > ,每次 < code > docker run< / code > 都会新建一个< strong > 匿名卷< / strong > 挂上去,而不是用可写层里的目录。< / p >
< p > 于是「构建阶段往这个目录写初始数据」就变得不可预期了: legacy 构建器会静默丢弃, BuildKit 的行为还取决于目录在 < code > VOLUME< / code > 之前是否已存在。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box warn" >
2026-10-25 23:26:58 +08:00
< span class = "bt" > 易错点 · VOLUME 之后的写入,构建阶段就不生效< / span >
< p > < b > 现象< / b > : < code > VOLUME /data< / code > 之后再 < code > RUN echo x > /data/f< / code > ,构建成功,换台机器起容器 < code > /data< / code > 却是空的;有时构建直接失败:< code > can't create /data/f: nonexistent directory< / code > 。< / p >
< p > < b > 原因< / b > : < code > VOLUME< / code > 之后的写入不再进镜像层。legacy 构建器(< code > DOCKER_BUILDKIT=0< / code > ) 静默丢弃; BuildKit( Docker 23 起默认)看目录在 < code > VOLUME< / code > 之前是否存在——先 < code > RUN mkdir -p /data< / code > 再声明,写入会保留进镜像;不先建目录,后续 < code > RUN< / code > 连 < code > ls /data< / code > 都失败。< / p >
< p > < b > 怎么验证< / b > : < code > docker inspect -f '{{json .Mounts}}' 容器名< / code > 看它到底挂了什么,再 < code > docker run --rm 镜像 ls -A /data< / code > 看卷里有什么。< / p >
< p > < b > 解决< / b > :别用 < code > VOLUME< / code > 预置数据。初始化逻辑放 entrypoint, 数据放命名卷, 镜像只管程序。< / p >
< / div >
< div class = "box danger" >
2026-10-14 22:42:38 +08:00
< span class = "bt" > 别用 commit 抢救数据< / span >
2026-10-25 23:26:58 +08:00
< p > 数据留在可写层,就会有人想用 < code > docker commit< / code > 把它打包成镜像。镜像是< b > 可复现的构建产物< / b > ,把运行期数据塞进去会让体积失控、无法回滚,也违反了「镜像不可变」的前提。< / p >
< p > 要留数据就挂载;要临时取出来用 < code > docker cp 容器:/path ./local< / code > ,或先用 < code > docker diff< / code > 看改了什么。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 可写层随容器删除而消失, stop 与 start 都不算数,只有挂载能留住数据。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-2" > 6.2 volume / bind mount / tmpfs 三选一< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 数据库容器重建之后数据没了、CI 里挂载路径在构建机上不存在、容器把内存吃满被 OOMKilled——这三类问题都源于挂载类型选错, 而不是命令写错。< / p >
< p > < span class = "tag" > 怎么做< / span > 三种挂载的差别不在「能不能写」,而在< strong > 数据落在谁的管辖范围内、能活多久< / strong > 。先见图 2, 把空间关系画清楚, 再对照细节。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 380" role = "img" aria-label = "三种挂载的空间对照:命名卷的数据在 dockerd 管理的目录里, 绑定挂载映射宿主机任意路径, tmpfs 只在内存里;三列分别标出数据在哪、删容器是否还在、能否跨容器共享、性能" >
< defs >
< marker id = "ar06a" 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 = "26" font-size = "13" style = "fill:var(--muted)" > 同一份数据,三种挂载落在哪里< / text >
< rect x = "120" y = "44" width = "184" height = "40" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "212" y = "69" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--brand-ink)" > volume 命名卷< / text >
< rect x = "312" y = "44" width = "184" height = "40" rx = "9" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "404" y = "69" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > bind mount 绑定挂载< / text >
< rect x = "504" y = "44" width = "184" height = "40" rx = "9" style = "fill:var(--soft);stroke:var(--line)" / >
< text x = "596" y = "69" text-anchor = "middle" font-size = "13.5" font-weight = "700" style = "fill:var(--ink)" > tmpfs 内存盘< / text >
< text x = "24" y = "130" font-size = "12" style = "fill:var(--muted)" > 宿主机上< / text >
< rect x = "120" y = "96" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "212" y = "118" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > dockerd 管的目录< / text >
< text x = "212" y = "136" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > /var/lib/docker/volumes< / text >
< rect x = "312" y = "96" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "404" y = "118" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 你指定的宿主机路径< / text >
< text x = "404" y = "136" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 如 /srv/app 或 $PWD< / text >
< rect x = "504" y = "96" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "596" y = "118" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 没有宿主机路径< / text >
< text x = "596" y = "136" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 数据只在内存里< / text >
< path d = "M212 154 V174" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar06a)" / >
< path d = "M404 154 V174" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar06a)" / >
< path d = "M596 154 V174" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar06a)" / >
< text x = "24" y = "212" font-size = "12" style = "fill:var(--muted)" > 容器里< / text >
< rect x = "120" y = "178" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "212" y = "200" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 一个普通目录< / text >
< text x = "212" y = "218" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 程序看不出区别< / text >
< rect x = "312" y = "178" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "404" y = "200" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 映射成同一个目录< / text >
< text x = "404" y = "218" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 宿主机改了立刻生效< / text >
< rect x = "504" y = "178" width = "184" height = "58" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "596" y = "200" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 一个内存文件系统< / text >
< text x = "596" y = "218" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 读写都走 RAM< / text >
< text x = "24" y = "268" font-size = "12" style = "fill:var(--muted)" > 删容器后< / text >
< rect x = "120" y = "248" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "212" y = "268" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ok)" > 还在,等 volume rm< / text >
< rect x = "312" y = "248" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "404" y = "268" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ok)" > 还在,人管的< / text >
< rect x = "504" y = "248" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "596" y = "268" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > 一起消失< / text >
< text x = "24" y = "306" font-size = "12" style = "fill:var(--muted)" > 跨容器< / text >
< rect x = "120" y = "286" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "212" y = "306" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 可以,按卷名挂< / text >
< rect x = "312" y = "286" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "404" y = "306" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 可以,同一路径< / text >
< rect x = "504" y = "286" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "596" y = "306" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > 不能< / text >
< text x = "24" y = "344" font-size = "12" style = "fill:var(--muted)" > 性能< / text >
< rect x = "120" y = "324" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "212" y = "344" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 近原生< / text >
< rect x = "312" y = "324" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "404" y = "344" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > Linux 原生; macOS 慢< / text >
< rect x = "504" y = "324" width = "184" height = "30" rx = "7" style = "fill:none;stroke:var(--line)" / >
< text x = "596" y = "344" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 最快,但吃内存< / text >
< text x = "24" y = "374" font-size = "12" style = "fill:var(--muted)" > 差别不在「能不能写」,而在数据落在谁的管辖范围内、能活多久。< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 三种挂载在宿主机与容器里各长什么样:数据在哪、删容器还在吗、能不能跨容器共享、性能各差在哪< / div >
< / figure >
< p > 见图 2: 三列是同一件事的三个答案——命名卷的数据在 dockerd 管的目录里, 绑定挂载的数据就在你指定的那个宿主机路径上, tmpfs 根本没有落盘。< / p >
< p > 所以「容器删了数据还在吗」不用背:只有前两种在,而前两种里只有命名卷不依赖你记住路径。< / 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 > < b > 命名卷 volume< / b > < / td > < td > < code > /var/lib/docker/volumes/< name> /_data< / code > ( Docker Desktop 上在虚拟机里,宿主机文件管理器看不到)< / td > < td > Docker 管理,容器删除后仍在,直到 < code > volume rm< / code > 或 < code > prune -a< / code > < / td > < td > 数据库数据、上传目录、一切需要备份的持久数据< / td > < / tr >
< tr > < td > < b > 匿名卷< / b > < / td > < td > 同上,卷名是随机哈希< / td > < td > 容器删除后仍在(< code > docker rm -v< / code > 除外),只能靠 < code > prune< / code > 清理< / td > < td > 临时隔离目录(如容器内的 < code > node_modules< / code > ) < / td > < / tr >
2026-10-14 22:42:38 +08:00
< tr > < td > < b > bind mount< / b > < / td > < td > 宿主机的真实路径,你自己知道在哪< / td > < td > 人管理,与宿主机目录同生共死< / td > < td > 开发时挂源码、挂宿主机 nginx 配置与证书< / td > < / tr >
< tr > < td > < b > tmpfs< / b > < / td > < td > 内存,不落盘< / td > < td > 随容器消失< / td > < td > 临时文件、密钥、不希望写进磁盘的中间产物< / td > < / tr >
< / tbody >
< / table >
< / div >
2026-10-25 23:26:58 +08:00
< p > 判断顺序只有三问:要备份、要跨容器共享 → 命名卷;要用宿主机的编辑器直接改文件 → 绑定挂载;能不能落盘 → tmpfs。数据库几乎永远选命名卷。< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "bash" data-name = "命名卷的日常操作" > $ docker volume create app-data
$ docker volume ls
$ docker volume inspect app-data -f '{{.Mountpoint}}' # Linux 原生上可直接进入该目录
$ docker run -d --name db -v app-data:/var/lib/postgresql/data postgres:16-alpine
$ docker volume rm app-data # 有容器在用时报 volume is in use
$ docker volume ls -f dangling=true # 先看清单,再决定要不要清理
$ docker volume prune # 默认只清匿名卷
$ docker volume prune -a # 连未使用的命名卷一起删,会真删数据< / pre >
2026-10-25 23:26:58 +08:00
< p > 命名卷不是「一个目录的别名」,它有自己的生命周期:创建、被挂载、被删除,三步都由 Docker 记录。所以它能被 < code > docker volume ls< / code > 列出来,也会在没人用时被批量清理。< / p >
< p > 匿名卷是它的孪生兄弟,区别只有一个:名字是随机的,你记不住,也就没法在下一次 < code > run< / code > 里复用。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box warn" >
2026-10-25 23:26:58 +08:00
< span class = "bt" > 易错点 · 匿名卷每次 run 都新建一个,删容器不写 -v 就攒一堆孤儿卷< / span >
< p > < b > 现象< / b > : < code > docker volume ls< / code > 里全是 64 位哈希名,< code > docker system df< / code > 显示卷占了几个 GB, 却说不清是哪个应用的。< / p >
< p > < b > 原因< / b > : < code > -v /app/node_modules< / code > 这种只写容器内路径的写法,以及镜像里 < code > VOLUME< / code > 声明的目录,每次 < code > run< / code > 都会新分配一个匿名卷;< code > docker rm< / code > 默认< b > 不删< / b > 它,只有 < code > docker rm -v< / code > 才会连匿名卷一起删。< / p >
< p > < b > 怎么验证< / b > : < code > docker ps -a --format '{{.Names}}'< / code > 对一下容器数量,再看 < code > docker volume ls -f dangling=true< / code > 的数量;两者同量级增长就是它。< / p >
< p > < b > 解决< / b > :一次性容器一律加 < code > --rm< / code > (它隐含删匿名卷),长期容器用命名卷,定期 < code > docker volume prune< / code > 只清匿名卷。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p > tmpfs 的定位是「不希望落盘」:密钥、临时解压目录、会话缓存。它随容器消失,也不参与任何备份,所以不要往里放任何需要活过容器的东西。< / p >
< p > 它的容量来自< strong > 容器内存限额< / strong > ,不是额外磁盘:< code > --tmpfs /tmp:size=512m< / code > 写满时报 < code > No space left on device< / code > ,真正原因却是内存被占满,容器可能紧接着被 OOMKilled。临时目录一定要显式写 < code > size=< / code > 。< / p >
< p class = "take" > < b > 一句话:< / b > 要备份用命名卷,要改文件用绑定挂载,不落盘用 tmpfs; 数据库永远选第一种。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-3" > 6.3 < code > -v< / code > 与 < code > --mount< / code > 的差别< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 同一条挂载,两种写法都能跑通;差别只在你写错的时候:< code > -v< / code > 倾向于静默补齐,< code > --mount< / code > 倾向于当场报错。< / p >
< p > < span class = "tag" > 怎么做< / span > 两种写法最终都进容器的 < code > Mounts< / code > 配置,功能等价,差别在< b > 出错时机< / b > 。前者写起来快,后者能早一步拦住事故。< / p >
2026-10-14 22:42:38 +08:00
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对比项< / th > < th > < code > -v< / code > / < code > --volume< / code > < / th > < th > < code > --mount< / code > < / th > < / tr > < / thead >
< tbody >
< tr > < td > 语法< / td > < td > 三段式 < code > -v 源:目标:选项< / code > ,位置敏感、不能省字段< / td > < td > 键值对 < code > --mount type=bind,source=...,target=...< / code > ,顺序无关< / td > < / tr >
< tr > < td > 宿主机路径不存在< / td > < td > < b > 自动创建< / b > 一个 root 属主的空目录,然后容器里「配置是空的」< / td > < td > 直接报 < code > bind source path does not exist< / code > ,不创建< / td > < / tr >
2026-10-25 23:26:58 +08:00
< tr > < td > 相对路径< / td > < td > < code > -v src:/app< / code > 里的 < code > src< / code > 会被当成< b > 卷名< / b > ,不是当前目录(高频事故)< / td > < td > < code > source=< / code > 要写绝对路径,相对路径会报错< / td > < / tr >
2026-10-14 22:42:38 +08:00
< tr > < td > 可表达的选项< / td > < td > < code > ro< / code > 、< code > rw< / code > 、< code > nocopy< / code > 、< code > z/Z< / code > ( SELinux) 、旧的 < code > cached/delegated< / code > < / td > < td > < code > readonly< / code > 、< code > volume-nocopy< / code > 、< code > volume-subpath< / code > 、< code > bind-propagation< / code > 、< code > consistency< / code > < / td > < / tr >
< tr > < td > 适合< / td > < td > 手敲调试、随手一挂< / td > < td > 脚本、CI、Compose 长语法,意图写全< / td > < / tr >
< / tbody >
< / table >
< / div >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag s" > 可跳过< / span > 最后一行的选项名不用背:< code > -v< / code > 的 < code > z< / code > / < code > Z< / code > 只在 SELinux 主机上有意义,< code > cached< / code > / < code > delegated< / code > 只在 macOS 的 gRPC-FUSE 下有效,第一遍跳过不影响任何判断。< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "bash" data-name = "-v 与 --mount 的等价写法与坑" > # 命名卷:两种写法等价
$ docker run --rm -v app-data:/data alpine:3.20 touch /data/a
$ docker run --rm --mount type=volume,source=app-data,target=/data alpine:3.20 touch /data/b
# ✗ 你以为挂的是 ./src, 实际新建了一个名叫 src 的命名卷,容器里是空的
$ docker run --rm -v src:/app alpine:3.20 ls -A /app
# ✓ 绑定当前目录要写 ./ 或绝对值
$ docker run --rm --mount type=bind,source="$PWD/src",target=/app alpine:3.20 ls /app
# ✓ 只读:两种写法的选项名不同
$ docker run --rm -v app-data:/data:ro alpine:3.20 touch /data/c
$ docker run --rm --mount type=volume,source=app-data,target=/data,readonly alpine:3.20 touch /data/c
touch: /data/c: Read-only file system< / pre >
2026-10-25 23:26:58 +08:00
< div class = "box warn" >
< span class = "bt" > 易错点 · < code > -v< / code > 会静默造出一个目录,哪怕你本意是挂一个文件< / span >
< p > < b > 现象< / b > : < code > -v /srv/app/config.yml:/app/config.yml< / code > 里宿主机文件路径拼错了一个字母,容器起来了,应用却报「配置是空的」或 < code > is a directory< / code > 。< / p >
< p > < b > 原因< / b > : < code > -v< / code > 只看源是否像路径;不存在就 < code > mkdir -p< / code > 一个 root 属主的空目录,然后把这个目录挂到容器内的文件路径上。数据于是写到了意料之外的地方。< / p >
< p > < b > 怎么验证< / b > : < code > ls -ld /srv/app/config.yml< / code > ,看到 < code > drwxr-xr-x< / code > 而不是 < code > -rw-r--r--< / code > 就是被建成了目录。< / p >
< p > < b > 解决< / b > :挂文件一律用 < code > --mount type=bind< / code > ,源不存在它会直接报错;脚本里再加一句 < code > test -f< / code > 提前拦住。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< div class = "box tip" >
< span class = "bt" > Compose 里的对应关系< / span >
< p > 短语法 < code > - ./src:/app:ro< / code > 等价于 < code > -v< / code > ;长语法 < code > type: bind< / code > 等价于 < code > --mount< / code > ,
并且多一个 < code > bind: create_host_path: false< / code > ,可以在宿主机目录不存在时让 Compose 报错而不是悄悄建空目录。
细节见 < a href = "08-compose.html#sec-8-3" > 8.3 service 字段全解< / a > 。< / p >
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > < code > -v< / code > 静默补目录、< code > --mount< / code > 当场报错;脚本与 CI 一律用后者。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-4" > 6.4 命名卷的生命周期与备份恢复< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 要升级数据库镜像了,你 < code > docker rm -f db< / code > 删掉旧容器,用同一个卷名起一个新容器,数据原样还在——这是命名卷最值钱的地方。< / p >
< p > < span class = "tag" > 怎么做< / span > 反过来,< code > docker volume prune -a< / code > 和 < code > docker compose down -v< / code > 会真删数据,且没有回收站。所以「删」这个动作要分清楚删的是容器还是卷。< / p >
2026-10-14 22:42:38 +08:00
< p > 备份不要在宿主机上直接 < code > cp< / code > 正在运行的数据库目录: 你复制到的只是「某一瞬间的文件」, WAL 和内存里的脏页没刷盘,恢复后可能直接起不来。卷里的普通文件可以用 tar, 数据库要用逻辑备份。< / p >
< pre data-lang = "bash" data-name = "任意命名卷的 tar 备份与恢复(跨平台一致)" > # 备份:起一个临时容器,把卷只读挂进去,打到宿主机目录
$ mkdir -p backup
$ docker run --rm \
--mount type=volume,source=app-data,target=/data,readonly \
--mount type=bind,source="$PWD/backup",target=/backup \
alpine:3.20 \
tar czf /backup/app-data-$(date +%F).tar.gz -C /data .
$ ls -lh backup/
# 恢复:先清空目标卷再解包,否则旧文件会残留下来
$ docker run --rm \
--mount type=volume,source=app-data,target=/data \
--mount type=bind,source="$PWD/backup",target=/backup \
alpine:3.20 \
sh -c 'rm -rf /data/.[!.]* /data/* & & tar xzf /backup/app-data-2025-01-01.tar.gz -C /data'< / pre >
< pre data-lang = "bash" data-name = "Postgres 的正确备份:逻辑导出" > # 自定义格式 -Fc 支持选择性/并行恢复,比纯 SQL 文本更实用
$ docker exec -e PGPASSWORD=app_pw db pg_dump -U app -d shop -Fc -f /tmp/shop.dump
$ docker cp db:/tmp/shop.dump "backup/shop-$(date +%F).dump"
# 恢复:--clean --if-exists 让脚本可以重复执行
$ docker exec -i -e PGPASSWORD=app_pw db \
pg_restore -U app -d shop --clean --if-exists < backup/shop-2025-01-01.dump
# 备份不验证等于没有备份:真的恢复到一个临时库,再从临时库查行数
$ docker exec -e PGPASSWORD=app_pw db sh -c \
'createdb -U app verify & & pg_restore -U app -d verify /tmp/shop.dump'
$ docker exec -e PGPASSWORD=app_pw db psql -U app -d verify -c 'select count(*) from orders;'
count
-------
10240
(1 row)
$ docker exec -e PGPASSWORD=app_pw db dropdb -U app verify # 验完删掉临时库< / pre >
2026-10-25 23:26:58 +08:00
< p > 备份的价值全在「能不能恢复」。所以恢复流程要写成脚本、每次真的恢复到一个临时库、查一次行数——上面最后三行就是这件事,别省。< / p >
< p > < span class = "tag s" > 可跳过< / span > 卷驱动( NFS / CIFS / local-persist) 能把卷放到远端存储, 挂载选项与权限参数第一遍可以跳过: 它不是这一章的主线, 用到时查驱动文档。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box danger" >
< span class = "bt" > < code > docker compose down -v< / code > 是删库按钮< / span >
< p > 为了让容器干净重建,很多人习惯 < code > docker compose down -v< / code > 。那个 < code > -v< / code > 删的是本项目的< b > 所有命名卷< / b > ,
2026-10-25 23:26:58 +08:00
包括 Postgres 的数据目录。开发库被清空、演示环境数据丢失,基本都是这一条命令造成的。< / p >
< p > 日常只用 < code > docker compose down< / code > ;必须重置时才加 < code > -v< / code > ,并且先确认卷名。< / p >
2026-10-14 22:42:38 +08:00
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 删容器不动卷,< code > down -v< / code > 与 < code > prune -a< / code > 才真删数据。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-5" > 6.5 开发场景:源码挂载与热重载< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 本地开发最常用的一招:把源码目录绑进容器,容器只提供运行时和依赖,代码始终由宿主机提供。改一行不用重建镜像,< code > git pull< / code > 之后也不用重新构建。< / p >
< p > < span class = "tag" > 怎么做< / span > 两条 < code > -v< / code > :一条把当前目录挂成工作目录,一条给依赖目录挡上一层。< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "bash" data-name = "Node 开发容器:绑定源码 + 隔离 node_modules" > $ docker run --rm -it --name api \
-v "$PWD":/app \
-v /app/node_modules \
-w /app \
-p 127.0.0.1:3000:3000 \
node:20-alpine \
npm run dev< / pre >
< p > 第二个 < code > -v /app/node_modules< / code > 只写了容器内路径, Docker 会挂一个匿名卷上去,把宿主机的
< code > node_modules< / code > < b > 挡住< / b > 。这一步不能省: macOS/Windows 上装的依赖是给本机架构编译的,进容器后
< code > esbuild< / code > 、< code > sharp< / code > 、< code > bcrypt< / code > 这类带原生二进制的包会以 < code > Invalid ELF header< / code >
2026-10-25 23:26:58 +08:00
或 < code > Cannot find module @esbuild/darwin-arm64< / code > 失败。< / p >
< p > 镜像里如果已经 < code > npm ci< / code > 过,新建的匿名卷会自动继承这份依赖;否则进容器执行一次 < code > docker exec -it api npm install< / code > 。< / p >
< p > < span class = "tag" > 代价< / span > 热重载依赖「文件变更事件能从宿主机传进容器」。Linux 原生通常没问题,项目大时 inotify 数量会不够,现象是「改了文件不重载」。< / p >
< p > 验证 < code > cat /proc/sys/fs/inotify/max_user_watches< / code > ,用
2026-10-14 22:42:38 +08:00
< code > sudo sysctl -w fs.inotify.max_user_watches=524288< / code > 解决。macOS/Windows 的绑定挂载常常传不进事件,
只能退化成轮询: nodemon 加 < code > --legacy-watch< / code > , webpack 配 < code > watchOptions.poll< / code > ,
Go 的 < code > air< / code > 打开 < code > poll = true< / code > 。更省事的方案是 < a href = "08-compose.html#sec-8-7" > 8.7 compose watch 热重载< / a > 。< / p >
< div class = "box case" >
< span class = "bt" > 真实案例:本地改了没提交,线上居然生效< / span >
< p > 有人在部署命令里加了 < code > -v /srv/app:/app< / code > ,本意是挂配置。结果容器跑的是宿主机目录里的代码,
镜像里的构建产物被完全遮住, 出现了「回滚镜像版本但行为没变」「debug 日志只在某台机器上出现」这类怪事。
记住一条:< b > 绑定挂载是开发工具,不是部署手段< / b > ,生产镜像必须自带代码。< / p >
< p > 另外要留意,绑定挂载< b > 不看< / b > < code > .dockerignore< / code > , < code > node_modules< / code > 、< code > .git< / code >
会照样进容器,既拖慢重载也污染环境。挂载范围尽量只给 < code > ./src< / code > ,而不是整个项目根目录。< / p >
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 源码挂进来、依赖挡出去;热重载不灵先查 inotify 与轮询。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-6" > 6.6 UID/GID 与文件权限难题< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 容器里进程的身份由 < b > UID/GID 数字< / b > 决定,和用户名无关;宿主机判断文件权限也只看数字。两边数字对不上就会出错。< / p >
< p > < span class = "tag" > 怎么做< / span > 典型三种表现:容器写不进挂载目录(< code > EACCES: permission denied< / code > );容器写出来的文件在宿主机上属主是 root 或 1000, 你改不动也删不掉; 卷里的文件属主显示成 < code > 100000< / code > 。< / p >
< p > 还有个常见误解:< code > node:20< / code > 镜像只< b > 预建< / b > 了 < code > node< / code > 用户( UID 1000) , Dockerfile 里并没有 < code > USER node< / code > ,默认仍以 root 运行
( < code > docker run --rm node:20-alpine id< / code > 的输出以 < code > uid=0(root) gid=0(root)< / code > 开头)。要跑成非 root 必须显式加 < code > --user node< / code > 。< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "bash" data-name = "先看数字,再改数字" > # 你这份镜像的 Dockerfile 里写了 USER node, 所以进程确实以 node 身份在跑
$ docker exec -it api id
uid=1000(node) gid=1000(node) groups=1000(node)
# -n 显示数字,因为容器里未必有同名用户
$ ls -ln ./data
drwxr-xr-x 2 0 0 4096 Jan 1 10:00 data
$ stat -c '%u:%g %a' ./data
0:0 755< / pre >
2026-10-25 23:26:58 +08:00
< div class = "box warn" >
< span class = "bt" > 易错点 · 容器里 < code > chown< / code > 报 Operation not permitted< / span >
< p > < b > 现象< / b > : entrypoint 里的 < code > chown -R app:app /data< / code > 直接失败:< code > chown: /data: Operation not permitted< / code > ,容器退出。< / p >
< p > < b > 原因< / b > :不是缺 root——挂载目录的属主在宿主机上, 而容器内进程( 尤其加了 < code > --user< / code > 、开了 rootless 或 userns-remap 时)没有对应的权限位去改它;反过来,< code > --user 1000:1000< / code > 启动官方数据库镜像时,它内部要 chown 的路径也不归这个 UID。< / p >
< p > < b > 怎么验证< / b > :宿主机 < code > ls -ln 挂载目录< / code > 与容器内 < code > id< / code > 的数字对比;数字一致就只剩权限位问题,不一致就是 UID 不匹配。< / p >
< p > < b > 解决< / b > :换命名卷让 entrypoint 自己处理属主;必须绑定挂载时在宿主机把目录属主改成容器内 UID, 而不是在容器里 chown。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< p > 最省事的对齐方式是在运行期指定:< code > docker run --user "$(id -u):$(id -g)" ...< / code > 。注意镜像里没有对应 UID 时
2026-10-25 23:26:58 +08:00
< code > $HOME< / code > 可能不可写,需要额外补 < code > -e HOME=/tmp< / code > 。< / p >
< p > < span class = "tag s" > 可跳过< / span > 开了 userns-remap 或 rootless 之后,卷里的文件属主会整体偏移到 < code > 100000< / code > 起,这是设计如此,不是 bug; 具体映射范围看 < code > /etc/subuid< / code > ,第一遍可以跳过。< / p >
< p > 要让镜像长期自带正确的身份,就在构建时把宿主 UID 作为参数传进去( Debian 系镜像用 < code > groupmod/usermod< / code > 改现成的 < code > node< / code > 用户最稳: 复用的是已存在的账号, 不会撞上「UID 已被占用」):< / p >
2026-10-14 22:42:38 +08:00
< pre data-lang = "dockerfile" data-name = "构建期对齐宿主 UID/GID" > FROM node:20-bookworm-slim
ARG UID=1000
ARG GID=1000
# 把镜像里的 node 用户改成宿主机的数字,绑挂载目录的权限自然对齐
# usermod 只改 UID, 不会改家目录里文件的属主, 必须顺手 chown,
# 否则以新 UID 运行时 npm 写 ~/.npm 会 EACCES
RUN set -eux; \
if [ "${UID}" != "1000" ] || [ "${GID}" != "1000" ]; then \
groupmod -o -g "${GID}" node; \
usermod -o -u "${UID}" node; \
chown -R node:node /home/node; \
fi; \
mkdir -p /app & & chown -R node:node /app
WORKDIR /app
COPY --chown=node:node package*.json ./
RUN npm ci
COPY --chown=node:node . .
USER node< / pre >
< pre data-lang = "dockerfile" data-name = "从零创建用户并匹配宿主 UID" > FROM debian:bookworm-slim
ARG UID=1000
ARG GID=1000
RUN set -eux; \
groupadd --gid "${GID}" app; \
useradd --uid "${UID}" --gid app --create-home --shell /bin/bash app; \
mkdir -p /app & & chown -R app:app /app
WORKDIR /app
COPY --chown=app:app . .
USER app< / pre >
< p > 这份 Dockerfile 能直接构建:< code > debian:bookworm-slim< / code > 里< b > 没有< / b > UID 1000 的账号,
< code > useradd --uid 1000< / code > 不会失败。1000 冲突只出现在复用已经占了 1000 的镜像时,
比如 < code > ubuntu< / code > ( < code > ubuntu< / code > 用户)或 < code > node< / code > ( < code > node< / code > 用户);
在那类镜像上加用户,就回到上一种思路——复用已有的普通用户,只把它的数字改成宿主的。< / p >
< pre data-lang = "bash" data-name = "按宿主身份构建; Alpine 与数据库容器的注意点" > $ docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) -t api:dev .
# Alpine 写法是 addgroup -g / adduser -D -u -G;
# 但 alpine 的 node 用户已经占了 1000, 传 1000 会直接冲突
# 数据库容器不要随手加 --user: 官方 postgres 的 entrypoint 要以 postgres 身份去 chown PGDATA,
# 而这个 UID 随镜像变体不同:实测 postgres:16-alpine 是 70, postgres:16( Debian) 是 999,
# 取实际值: docker run --rm --entrypoint id postgres:16-alpine postgres
# 用 --user 1000:1000 启动会得到:
# chown: /var/lib/postgresql/data: Operation not permitted
# 正确做法是用命名卷,让 entrypoint 自己处理属主
# 需要访问 docker.sock 时,把 socket 的组加进来,而不是用 root
$ docker run --rm --group-add "$(stat -c '%g' /var/run/docker.sock)" \
-v /var/run/docker.sock:/var/run/docker.sock docker:27-cli docker ps< / pre >
2026-10-25 23:26:58 +08:00
< p > 三种解法各有适用面,按优先级排:< code > --user< / code > 最轻,改一行就能试;< code > --group-add< / code > 只在「需要某个组权限」时用,比如 docker.sock、设备节点; 镜像内建用户最重, 但一次构建处处生效, 适合团队统一。< / p >
2026-10-14 22:42:38 +08:00
< div class = "box ok" >
< span class = "bt" > 三条优先顺序< / span >
< ol class = "steps" >
< li > 把「本来就不需要人类看见」的目录(依赖、数据库数据、缓存)放进命名卷,权限问题直接消失,代价是不能用宿主机 IDE 直接打开。< / li >
< li > 必须绑定挂载源码时,用 < code > --user $(id -u):$(id -g)< / code > 或在构建期对齐 UID/GID, 两种都写明在 README 里,避免换人换机后重新踩坑。< / li >
< li > 永远不要用 < code > chmod 777< / code > 绕过问题:它不仅让 CI 上的权限模型失真,还会在共享宿主机上放开其他用户的读写。< / li >
< / ol >
< / 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-6-7" > 6.7 只读挂载< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 只读分两个层次,可以叠加使用。单个挂载只读用 < code > -v app-config:/etc/app:ro< / code > (或 < code > --mount ...,readonly< / code > ) ,
用来挂配置、证书和上游数据,让容器里的进程即使被拿下也改不了这些文件。< / p >
< p > < span class = "tag" > 怎么做< / span > 整个根文件系统只读用 < code > --read-only< / code > ,
2026-10-14 22:42:38 +08:00
它的边界要记清楚:< b > 不影响卷和绑定挂载< / b > ,那些仍然可写,要只读就逐个加 < code > :ro< / code > 。< / p >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 代价< / span > 直接开 < code > --read-only< / code > 十有八九起不来,因为程序要写 < code > /tmp< / code > 、< code > /run< / code > 、pid 文件和缓存,
2026-10-14 22:42:38 +08:00
报错是干脆的一句 < code > Read-only file system< / code > 。所以必须配套 < code > --tmpfs< / code > ,日志则一律走 stdout 而不是写 < code > /var/log< / code > 。< / p >
< pre data-lang = "bash" data-name = "只读根 + 两个可写内存目录" > $ docker run -d --name api \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--tmpfs /run:rw,noexec,nosuid,size=16m \
-v api-data:/var/lib/api \
-p 127.0.0.1:8080:8080 \
api:1.4.0
$ docker exec -it api sh -c 'touch /x'
touch: /x: Read-only file system
$ docker exec -it api sh -c 'touch /tmp/ok' # 临时目录可写
$ docker exec -it api sh -c 'echo y > /var/lib/api/f' # 卷可写
# nginx 的经典报错:只读根之后它写不了 pid 和缓存
# nginx: [emerg] open() "/var/run/nginx.pid" failed (30: Read-only file system)
# 修法:--tmpfs /var/run --tmpfs /var/cache/nginx< / pre >
< div class = "box tip" >
< span class = "bt" > 开发期也值得用的只读挂载< / span >
< p > 把本机凭据只读挂进工具容器,既能直接用又不担心容器里的程序改坏本机文件:< / p >
< pre data-lang = "bash" data-name = "只读挂载本机凭据" > $ docker run --rm -it \
-v "$HOME/.kube:/root/.kube:ro" \
-v "$HOME/.aws:/root/.aws:ro" \
bitnami/kubectl:1.31 version --client< / pre >
< / div >
2026-10-25 23:26:58 +08:00
< p class = "take" > < b > 一句话:< / b > 只读要分两层做:挂载点加 ro, 根文件系统加 --read-only 并配 tmpfs。< / p >
2026-10-14 22:42:38 +08:00
< h3 id = "sec-6-8" > 6.8 性能与跨平台文件系统< / h3 >
2026-10-25 23:26:58 +08:00
< p > < span class = "tag" > 场景< / span > 挂载不改变文件系统实现: 挂什么, 容器里就是什么的性能。Linux 原生上绑定挂载几乎等于直连本地磁盘( overlay2 只影响可写层,
首次改写大文件有复制开销) ; macOS 和 Windows 上数据要穿过虚拟化边界,差别可以是十倍。< / p >
< p > < span class = "tag s" > 可跳过< / span > macOS 上 < code > /Users< / code > 通过 VirtioFS( 旧版是 gRPC-FUSE) 共享进 Linux 虚拟机;旧的 < code > :cached< / code > /< code > :delegated< / code >
只对 gRPC-FUSE 有意义, VirtioFS 下不生效。这类实现细节第一遍可以跳过。< / p >
< p > 要记的是后果:小文件密集操作——< code > npm install< / code > 、< code > composer install< / code > 、编译——在共享层上明显更慢,慢到几倍到几十倍都正常。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · macOS 上 bind mount 的写入延迟可能是容器的几十倍< / span >
< p > < b > 现象< / b > : 同一份代码, Linux 同事 < code > npm ci< / code > 40 秒,你的 Mac 上 6 分钟;数据库跑在绑定挂载上时,压测 QPS 只有一半。< / p >
< p > < b > 原因< / b > :每次写都要跨虚拟化边界,小文件与 fsync 尤其贵;命名卷落在虚拟机内部的 ext4 上,几乎不付这个成本。< / p >
< p > < b > 怎么验证< / b > :在挂载点与命名卷上各跑一次顺序写 + fsync 的对比(下面那段 < code > dd< / code > ),差好几倍就不要把数据目录放绑定挂载。< / p >
< p > < b > 解决< / b > :依赖与数据库放命名卷,只挂真正要编辑的源码;实在要两者兼得,用 8.7 的 compose watch 做单向同步。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< p > Windows + WSL2 的核心分界线是路径:项目放在 < code > /mnt/c/...< / code > (走 9p/drvfs) 比放在 WSL2 自己的 ext4
( < code > \\wsl$\Ubuntu\home\...< / code > )慢一个数量级,而且 < code > /mnt/c< / code > 下的文件变更事件传不到 Linux 侧,
热重载经常不触发。把仓库直接克隆进 WSL2 是根治办法。< / p >
< div class = "box danger" >
< span class = "bt" > 数据库数据目录不要用绑定挂载跨平台共享< / span >
< p > 在 macOS/Windows 上把 < code > PGDATA< / code > 、MySQL < code > datadir< / code > 绑到宿主机目录,会同时踩三个雷:
文件锁与 fsync 语义经过共享层后不可靠(官方不支持这种部署);每次 fsync 都跨虚拟化边界,吞吐可能掉到十分之一;
2026-10-25 23:26:58 +08:00
共享层对 < code > mmap< / code > 等调用支持不全,可能出现难以复现的启动失败甚至数据损坏。< / p >
< p > 正确做法是命名卷——数据落在这台机器虚拟机内的 ext4 上,性能接近原生,需要看文件时用 < code > docker exec< / code > 或 < code > docker cp< / code > 。< / p >
2026-10-14 22:42:38 +08:00
< / div >
< pre data-lang = "bash" data-name = "量一下当前挂载点的写吞吐" > # 512MB 顺序写 + fsync: 分别在工作目录挂载点和命名卷上各跑一次
$ docker run --rm -v "$PWD":/w alpine:3.20 \
sh -c 'dd if=/dev/zero of=/w/.bench bs=1M count=512 conv=fsync 2> & 1 | tail -1; rm -f /w/.bench'
536870912 bytes (512.0 MB, 512 MiB) copied, 4.1 s, 125 MB/s
$ docker volume create bench & & docker run --rm -v bench:/w alpine:3.20 \
sh -c 'dd if=/dev/zero of=/w/.bench bs=1M count=512 conv=fsync 2> & 1 | tail -1'
536870912 bytes (512.0 MB, 512 MiB) copied, 0.7 s, 731 MB/s< / pre >
2026-10-25 23:26:58 +08:00
< p > 数字随机器波动很大:上面那台是 125MB/s 对 731MB/s; 作者在 Windows 11 + WSL2( Docker 29.6)上实测是 126.9MB/s 对 1.2GB/s。方向一致——命名卷快 5 到 10 倍, macOS 上差距只会更大。< / p >
2026-10-14 22:42:38 +08:00
< p > 结论很直接:< b > 依赖和数据库放命名卷,只把真正需要编辑的源码绑进来< / b > 。真需要「宿主源码 + 容器内高速 I/O」两者兼得,
2026-10-25 23:26:58 +08:00
就用 < a href = "08-compose.html#sec-8-7" > 8.7 compose watch 热重载< / a > 做单向同步,或者把项目放进 WSL2 文件系统。< / p >
< p > 顺带一提,别在容器里跑 < code > git status< / code > 这类要遍历整个工作区的命令,跨文件系统时它会成为最慢的一步。< / p >
< p class = "take" > < b > 一句话:< / b > 共享层只挂源码,数据一律放命名卷;慢不慢用 dd 量,别靠感觉。< / p >
2026-10-14 22:42:38 +08:00
2026-10-25 23:26:58 +08:00
< h2 id = "ch6-sum" > 全章回顾:三句话 + 一张症状表< / h2 >
< div class = "box case" >
< span class = "bt" > 三句话说完这一章< / span >
< p > < b > 第一句:< / b > 可写层和容器同生共死,< code > stop< / code > 与 < code > start< / code > 不碰它,< code > rm< / code > 连它一起删——所以「数据没了」只有两种原因:写在了可写层,或者卷被 < code > prune< / code > / < code > down -v< / code > 删了。< / p >
< p > < b > 第二句:< / b > 三种挂载的差别是数据落在谁的管辖范围内:命名卷归 Docker、绑定挂载归你、tmpfs 只在内存里,只有前两种活得比容器久。< / p >
< p > < b > 第三句:< / b > 两类高频事故都是「以为挂上了」:< code > -v< / code > 把不存在的路径静默建成空目录,容器内 UID 与宿主机目录属主对不上。< / p >
< / div >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 症状< / th > < th > 先查什么< / th > < th > 回哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > 容器删了,数据也没了< / td > < td > < code > docker inspect< / code > 看 < code > Mounts< / code > :数据到底写在可写层还是卷里< / td > < td > 6.1< / td > < / tr >
< tr > < td > 卷里有数据,换台机器就没了< / td > < td > 挂的是匿名卷还是命名卷;绑定挂载是不是指到了本机路径< / td > < td > 6.2< / td > < / tr >
< tr > < td > 容器写不进挂载目录( permission denied) < / td > < td > 容器内 < code > id< / code > 的数字与宿主机 < code > ls -ln< / code > 的属主< / td > < td > 6.6< / td > < / tr >
< tr > < td > 挂载路径「明明是文件,容器里变成目录」< / td > < td > < code > ls -ld< / code > 看宿主机路径是不是被 < code > -v< / code > 建成了目录< / td > < td > 6.3< / td > < / tr >
< tr > < td > < code > docker volume ls< / code > 里一堆哈希名< / td > < td > 匿名卷:< code > docker rm -v< / code > 没写,或镜像里声明了 < code > VOLUME< / code > < / td > < td > 6.2< / td > < / tr >
< tr > < td > 数据库容器起不来或慢十倍< / td > < td > 数据目录是否放在 macOS / Windows 的绑定挂载上< / td > < td > 6.8< / td > < / tr >
< tr > < td > 加了 < code > --read-only< / code > 之后容器起不来< / td > < td > 缺 < code > --tmpfs /tmp /run< / code > ,或日志写进了文件< / td > < td > 6.7< / td > < / tr >
< tr > < td > 备份恢复了,数据却不对< / td > < td > 是否对运行中的库直接 < code > cp< / code > ,而不是逻辑备份< / td > < td > 6.4< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box tip" >
< span class = "bt" > 一分钟讲完这一章< / span >
< p > 先讲骨架:可写层随容器消失,挂载是唯一的出路,三种挂载的差别在「谁管、活多久」。再讲三个坑:< code > -v< / code > 静默建目录、匿名卷越攒越多、< code > chown< / code > 报 Operation not permitted。< / p >
< p > 最后用 6.8 收口:跨平台共享层的性能差 5 到 10 倍,数据库目录永远放命名卷。< / p >
< / div >
2026-10-14 22:42:38 +08:00
< / article >
< nav class = "pager" >
< a class = "prev" href = "05-container.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 5 章 · 容器生命周期与日常操作< / span > < / a >
< a class = "next" href = "07-network.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 7 章 · 网络:容器之间与外界如何通信< / span > < / a >
< / nav >
< p class = "foot" >
Docker 专题 · 第 6 章 存储与数据持久化< 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 >