Docker数据卷(Data Volume)学习

引言

在Docker中,容器的数据读写默认发生在容器的存储层,当容器被删除时其上的数据将会丢失。要想实现数据的持久化,需要将数据从宿主机挂载到容器中。目前Docker提供了三种方式将数据从宿主机挂载到容器中。

  • Volume

普通数据卷,映射到/var/lib/docekr/volumes目录下。

  • bind mounts

绑定数据卷,映射到宿主机指定路径下

  • tmpfs mounts

临时卷,只存在于宿主机内存中

三种方式的示意图如下所示:

[图片上传失败...(image-84e533-1683787778313)]

一、Volume的基本使用

1、创建数据卷

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1775" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1、创建一个Volume
[root@qll251 ~]# docker volume create for_nginx
for_nginx

2、查看所有数据卷
[root@qll251 ~]# docker volume ls
DRIVER VOLUME NAME
local for_nginx

3、 查看指定数据卷详细信息
[root@qll251 ~]# docker volume inspect for_nginx</pre>

[图片上传失败...(image-dbafcd-1683787778313)]

可以看到我们创建的for_nginx卷,保存在宿主机/var/lib/docekr/volumes/下;以后针对该Volume的写操作都会保存在/var/lib/docekr/volumes/for_nginx/_data下。

2、挂载数据卷到容器

创建nginx容器,并挂载 for_nginx 数据卷

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1780" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker run -itd -p 88:80 --mount type=volume,source=for_nginx,target=/usr/share/nginx/html nginx
c2124dd81a36e7a798b95276215a59a63d4a5f7d34ccb2bc31889494304ade56</pre>

mount参数后面跟的是键值对。source指定宿主机源文件位置。target指定数据卷在容器中的挂载位置。

使用docker inspect查看容器挂载信息:

[图片上传失败...(image-9760da-1683787778313)]

红框标出的内容,相信您基本都能看懂,这里扩展一个小知识点:RW: true,表示读写。如果希望数据卷对某个容器来说是只读的,可以通过添加readonly选项来实现,代码示例如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1787" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -itd --mount source=for_nginx,target=/test,readonly nginx</pre>

3、修改nginx主页内容

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1789" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# cd /var/lib/docker/volumes/
[root@qll251 volumes]# cd for_nginx/_data/
[root@qll251 _data]# ls
50x.html index.html
[root@qll251 _data]# echo "Hello world" > index.html</pre>

4、测试访问nginx主页

[图片上传失败...(image-c68bf9-1683787778313)]

5、验证数据卷的持久化

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1793" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1、强制删除nginx容器[root@qll251 ~]# docker rm -f c2
c2
2、容器被删除,数据卷仍然存在[root@qll251 ~]# cd /var/lib/docker/volumes/for_nginx/
[root@qll251 for_nginx]# more _data/index.html
Hello world</pre>

由此可以验证,容器卷与容器的生命周期无关,在删除容器时,docker不会自动删除卷,即使用Volume可以实现数据的持久化保存。

6、清理卷

如果不再使用本地容器卷,那么可以手动清理掉

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1798" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker volume rm for_nginx` </pre>

二、bind mounts 的基本使用

1、 使用卷创建容器

运行一个nginx容器,并将宿主机/webapp目录挂载至容器/usr/share/nginx/html目录:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1802" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker run -itd -p 81:80 --mount type=bind,source=/webapp,target=/usr/share/nginx/html nginx
690e779843933f735cf9044d7fd159a72ce6dcb833da61a010df58b0122b074e</pre>

上述命令等同于使用旧的-v参数,代码如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1804" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">docker run -itd -p 81:80 -v /webapp:/usr/share/nginx/html nginx</pre>

2、验证挂载情况

[图片上传失败...(image-dc544-1683787778313)]

3、进入容器

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n1808" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker exec -it 690 /bin/bash
root@690e77984393:/# cd /usr/share/nginx/html/
root@690e77984393:/usr/share/nginx/html# ls</pre>

进入该容器,会发现容器该目录本身存在的文件消失不见了。原因如下:

如果你使用Bind mounts挂载宿主机目录到一个容器中的非空目录,那么此容器中的非空目录中的文件会被隐藏,容器访问这个目录时能够访问到的文件均来自于宿主机目录。这也是Bind mounts模式和Volumes模式最大的不同

三、tmpfs mounts 的使用

tmpfs mountfs,顾名思义 是一种非持久化的数据存储。它仅仅将数据保存在宿主机的内存中,一旦容器停止运行,tmpfs mounts会被移除,从而造成数据丢失

1、运行一个容器,并指定临时卷

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n1815" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker run -itd --mount type=tmpfs,target=/test ubuntu:18.04
fad68fbcdab580fad03db7fa1e4f40c4e694fb8a30dc292af38dc3efd556b140
[root@qll251 ~]#</pre>

2、在容器中写入数据

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1817" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker exec -it fa /bin/bash
root@fad68fbcdab5:/# cd /test
root@fad68fbcdab5:/test# echo 123123 > tmp.txt
root@fad68fbcdab5:/test# cat tmp.txt
123123</pre>

3、重启容器,发现数据丢失

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n1819" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@qll251 ~]# docker restart fa
fa
[root@qll251 ~]# docker exec -it fa /bin/bash
root@fad68fbcdab5:/# cd /test
root@fad68fbcdab5:/test# cat tmp.txt
cat: tmp.txt: No such file or directory</pre>

4、tmpfs mounts的使用场景

当因为安全或其他原因,不希望将数据持久化到容器或宿主机上,那么可以使用tmpfs mounts模式。

文末 您的点赞收藏就是对我最大的鼓励! 欢迎关注我的简书,分享Android干货,交流Android技术。 对文章有何见解,或者有何技术问题,欢迎在评论区一起留言讨论!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在生产环境中使用Docker时,经常需要对数据进行持久化,这就有点像Redis里面的持久性一样的,或者需要在多个容...
    82a7fe2508f4阅读 3,258评论 0 0
  • 一、容器数据卷 1、什么是容器卷 docker是要将应用和环境打包成一个镜像这样,数据就不应该在容器中,否则容器删...
    一乐乐阅读 3,685评论 0 0
  • 为什么用数据卷 宿主机无法直接访问容器中的文件 容器中的文件没有持久化,导致容器删除后,文件数据也随之消失 容器之...
    butters001阅读 1,621评论 0 0
  • 为什么使用数据卷,那先来了解一下Docker的联合文件系统。 0.数据卷基础 0.1 Docker联合文件系统 D...
    董小贱阅读 10,352评论 0 4
  • 安装 官方文档 Docker提供了以下Linux发行版和体系结构中的.deb和.rpm软件包: 这里我们以cent...
    Martain阅读 3,499评论 0 3

友情链接更多精彩内容