使用 Docker 卷持久化数据
学习 Docker 卷和绑定挂载如何让数据在容器生命周期结束后继续保留,并允许您在主机与容器之间共享文件
使用 Docker 卷持久化数据 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Containers Are Ephemeral
When a container is removed, everything written inside its writable layer is gone. For databases, uploads, or logs you need storage that survives the container.
Two Storage Options
Docker offers two main mechanisms:
- Volumes: managed by Docker, stored in a dedicated area
- Bind mounts: map a specific host folder into the container
Volumes are the recommended default.
Creating a Volume
Create a named volume with docker volume create. Docker manages where it physically lives.
docker volume create app_dataMounting a Volume
Attach the volume to a container path with -v. Anything written to that path persists in the volume.
docker run -d -v app_data:/var/lib/data my_appBind Mounts for Development
A bind mount maps a host directory into the container, great for live-editing source code during development.
docker run -v /home/me/project:/app node:20Listing and Inspecting Volumes
See all volumes with docker volume ls, and view details, including the mount point, with docker volume inspect.
docker volume ls
docker volume inspect app_dataVolumes in Compose
In docker-compose.yml you declare volumes once and reference them in services. Use single quotes and indentation, never tabs.
services:
db:
image: postgres:16
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:Sharing a Volume Between Containers
Multiple containers can mount the same volume to share files, for example an app writing files and a backup container reading them.
docker run -v shared:/data writer
docker run -v shared:/data readerRead-Only Mounts
Append :ro to mount a volume read-only when a container should never modify the data, adding a safety guard.
docker run -v config:/etc/app:ro my_appBacking Up a Volume
Back up a volume by running a throwaway container that tars its contents to the host.
docker run --rm -v app_data:/data -v $(pwd):/backup \
alpine tar czf /backup/data.tar.gz /dataCleaning Up Unused Volumes
Orphaned volumes waste disk. Remove a named one with docker volume rm, or clear all unused volumes with docker volume prune, carefully.
docker volume pruneQuick Check
Test your understanding of Docker storage.
Recap
You learned to persist data in Docker:
- Containers are ephemeral; use volumes or bind mounts for durable data
- Mount with
-v name:/path; use bind mounts for live dev - Declare volumes in Compose and share them between containers
- Back up, mount read-only, and prune unused volumes
Volumes let your databases and files outlive any single container.
常见问题解答
「使用 Docker 卷持久化数据」课时是免费的吗?
是的 — 「使用 Docker 卷持久化数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「使用 Docker 卷持久化数据」这节课中我会学到什么?
学习 Docker 卷和绑定挂载如何让数据在容器生命周期结束后继续保留,并允许您在主机与容器之间共享文件 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 Docker 卷持久化数据」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 编写用于构建镜像的 Dockerfile
- 构建自定义 Docker 镜像
- 使用 Compose 构建多容器应用
- 使用 Docker 卷持久化数据