Managing Data in Containers

简介:

Managing Data in Containers

So far we've been introduced to some basic Docker concepts, seen how to work with Docker images as well as learned about networking and links between containers. In this section we're going to discuss how you can manage data inside and between your Docker containers.

这一章介绍怎样在docker容器之间管理数据

We're going to look at the two primary ways you can manage data in Docker.

在容器中管理数据的2个主要方法

  • Data volumes, and
  • Data volume containers.

Data volumes

数据卷

data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data:

数据卷是一个专门UFS的专门文件夹,它能够提供非常多实用的特性或者共享数据

  • Data volumes can be shared and reused between containers  数据卷能够在容器之间共享和重用
  • Changes to a data volume are made directly 对数据卷的改变是立刻生效
  • Changes to a data volume will not be included when you update an image 当你更新数据的时候。不会被包括到image中
  • Volumes persist until no containers use them 卷会一直存在直到没有容器使用他们

Adding a data volume

加入一个数据卷

You can add a data volume to a container using the -v flag with the docker run command. You can use the -v multiple times in a single docker run to mount multiple data volumes. Let's mount a single volume now in our web application container.

在用docker run命令的时候,使用-v标记来加入一个数据卷。在一次run中多次使用能够挂载多个数据卷,以下载入一个卷到web容器上。

$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py

This will create a new volume inside a container at /webapp.

这创建一个新的卷到容器的/webapp

Note: You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image.

注意:也能够在dockerfile中使用volume来加入一个或者多个新的卷到随意容器

Mount a Host Directory as a Data Volume

挂载一个主机文件夹作为数据卷

In addition to creating a volume using the -v flag you can also mount a directory from your own host into a container.

使用-v标记也能够挂载一个主机的文件夹到容器中去

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

This will mount the local directory, /src/webapp, into the container as the /opt/webapp directory. This is very useful for testing, for example we can mount our source code inside the container and see our application at work as we change the source code. The directory on the host must be specified as an absolute path and if the directory doesn't exist Docker will automatically create it for you.

上面的命令载入主机的/src/webapp到容器的/opt/webapp文件夹。

这个測试的时候非常实用,比方我们能够载入我们的源代码到容器中。来查看他们是否工作良好。文件夹的路径必须是主机上的绝对路径。假设文件夹不存在docker会自己主动为你创建它。

Note: This is not available from a Dockerfile due to the portability and sharing purpose of it. As the host directory is, by its nature, host-dependent, a host directory specified in a Dockerfile probably wouldn't work on all hosts.

注意:dockerfile 中不能用,各种操作系统的文件路径格式不一样。所以不一定适合全部的主机。


Docker defaults to a read-write volume but we can also mount a directory read-only.

docker mount默认是读写权限,但我们能够把它载入为仅仅读。

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

Here we've mounted the same /src/webapp directory but we've added the ro option to specify that the mount should be read-only.

加了ro之后,就挂载为仅仅读了。

Mount a Host File as a Data Volume

The -v flag can also be used to mount a single file - instead of just directories - from the host machine.

-v标记也能够从主机挂载单个文件到容器中

$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

This will drop you into a bash shell in a new container, you will have your bash history from the host and when you exit the container, the host will have the history of the commands typed while in the container.

这样就能够记录在容器输入过的命令了。

Note: Many tools used to edit files including vi and sed --in-place may result in an inode change. Since Docker v1.1.0, this will produce an error such as "sed: cannot rename ./sedKdJ9Dy: Device or resource busy". In the case where you want to edit the mounted file, it is often easiest to instead mount the parent directory.

注意:非常多工具子在使用vi或者sed --in-place的时候会导致inode的改变。从docker 1.1.0起。它会报错,所以最简单的办法就直接mount父文件夹。

Creating and mounting a Data Volume Container

创建并载入一个容器数据卷

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it's best to create a named Data Volume Container, and then to mount the data from it.

假设你有一些持续更新的数据须要在容器之间共享,最好创建一个命名容器卷。然后载入它。

Let's create a new named container with a volume to share.

如今就来创建一个命名容器卷并共享它

$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres

You can then use the --volumes-from flag to mount the /dbdata volume in another container.

你能够在其它容器中使用--volumes-from 来挂载/dbdata卷

$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres

And another:

$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres


You can use multiple --volumes-from parameters to bring together multiple data volumes from multiple containers.

还能够使用多个--volumes-from 參数来从多个容器挂载多个数据卷

You can also extend the chain by mounting the volume that came from the dbdata container in yet another container via the db1 or db2 containers.

也能够从其它已经挂载了容器卷的容器来挂载数据卷

$ sudo docker run -d --name db3 --volumes-from db1 training/postgres

If you remove containers that mount volumes, including the initial dbdata container, or the subsequent containers db1 and db2, the volumes will not be deleted until there are no containers still referencing those volumes. This allows you to upgrade, or effectively migrate data volumes between containers.

假设你移除了挂载的容器,包含初始容器,或者后来的db1 db2,这些卷在有容器使用它的时候不会被删除。这能够让我们在容器之间升级和移动数据。

Backup, restore, or migrate data volumes

备份、恢复、移动数据卷

Another useful function we can perform with volumes is use them for backups, restores or migrations. We do this by using the --volumes-from flag to create a new container that mounts that volume, like so:

另外一个实用功能是使用他们来备份、恢复、移动数据卷。使用--volume标记来创建一个载入了卷的新的容器。命令例如以下:

$ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

Here's we've launched a new container and mounted the volume from the dbdata container. We've then mounted a local host directory as /backup. Finally, we've passed a command that uses tar to backup the contents of the dbdata volume to a backup.tar file inside our /backup directory. When the command completes and the container stops we'll be left with a backup of our dbdata volume.

这里我们创建了一个容器。然后从dbdata容器来挂载一个数据卷。然后从本地主机挂载了一个/backup文件夹。

最后。使用tar命令来讲dbdata卷备份为back.tar到/backup文件夹。当命令运行完、容器停止之后,我们就备份了dbdata数据卷。


You could then to restore to the same container, or another that you've made elsewhere. Create a new container.

你能够使用这个备份来恢复这个容器。

$ sudo docker run -v /dbdata --name dbdata2 ubuntu /bin/bash

Then un-tar the backup file in the new container's data volume.

然后使用untar解压这个备份文件到新容器卷中。

$ sudo docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar

You can use this techniques above to automate backup, migration and restore testing using your preferred tools.

你能够用上述技术自己主动备份、移动、恢复測试。

Next steps

Now we've learned a bit more about how to use Docker we're going to see how to combine Docker with the services available on Docker Hub including Automated Builds and private repositories.

Go to Working with Docker Hub.










本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5103758.html,如需转载请自行联系原作者

相关实践学习
基于Hologres轻量实时的高性能OLAP分析
本教程基于GitHub Archive公开数据集,通过DataWorks将GitHub中的项⽬、行为等20多种事件类型数据实时采集至Hologres进行分析,同时使用DataV内置模板,快速搭建实时可视化数据大屏,从开发者、项⽬、编程语⾔等多个维度了解GitHub实时数据变化情况。
阿里云实时数仓实战 - 用户行为数仓搭建
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3 )前置知识要求:熟练掌握 SQL 语法熟悉 Linux 命令,对 Hadoop 大数据体系有一定的了解   课程大纲 第一章 了解数据仓库概念 初步了解数据仓库是干什么的 第二章 按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章 数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章 采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章 用户行为数据仓库 严格按照企业的标准开发 第六章 搭建业务数仓理论基础和对表的分类同步 第七章 业务数仓的搭建  业务行为数仓效果图  
相关文章
|
5月前
|
存储 数据采集 安全
一文深入了解RFID与条码技术的差异
RFID与条码技术都是用于物体信息识别的重要手段,但两者在工作原理、数据存储、读取方式、速度、耐用性、成本及安全性等方面存在显著差异。条码依赖光学扫描,成本低但易受环境影响,数据量有限;RFID通过射频信号传输,具备更大存储容量、非接触式读取、快速多标签处理能力以及更高安全性,适合复杂环境。根据实际需求选择合适技术,二者均为物联网时代物品识别的关键工具。
|
11月前
|
数据采集 前端开发 JavaScript
动态与静态网站抓取的区别:从抓取策略到性能优化
本文详细介绍了动态与静态网站抓取的区别、抓取策略及性能优化技巧,并提供了相关代码示例。静态网站抓取通过简单的HTTP请求和解析库实现,而动态网站则需使用Selenium等工具模拟浏览器执行JavaScript。文章还展示了如何使用代理IP、多线程和合理的请求头设置来提高抓取效率。
428 2
动态与静态网站抓取的区别:从抓取策略到性能优化
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
10月前
|
人工智能 算法 物联网
Lyra:SmartMore 联合香港多所高校推出的多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力
Lyra是由香港中文大学、SmartMore和香港科技大学联合推出的高效多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力。Lyra基于开源大型模型和多模态LoRA模块,减少训练成本和数据需求,支持多种模态理解和推理任务。
297 33
Lyra:SmartMore 联合香港多所高校推出的多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力
|
12月前
|
SQL 关系型数据库 数据库
使用 PostgreSQL 和 Python 实现数据库操作
【10月更文挑战第2天】使用 PostgreSQL 和 Python 实现数据库操作
|
9月前
|
人工智能 新能源 数据安全/隐私保护
销售易CRM 2024年市场表现分析及2025年展望
2024年,腾讯旗下销售易CRM在产品创新、行业应用和客户服务等方面取得显著进展。通过AI赋能推出智能销售助手,优化私有化部署,提升移动端用户体验,并在制造业等重点行业提供定制化解决方案。2025年,销售易将深化AI集成,扩展产品生态,加大新兴行业投入,加强国际化布局。尽管面临市场竞争,销售易将继续优化易用性、数据安全及定价策略,助力企业数字化转型。
|
存储 前端开发 数据处理
|
12月前
|
人工智能
三文带你轻松上手鸿蒙的AI语音03-文本合成声音
三文带你轻松上手鸿蒙的AI语音03-文本合成声音
293 1
三文带你轻松上手鸿蒙的AI语音03-文本合成声音
|
存储 关系型数据库 MySQL
Mysql全面总结
本文全面总结了MySQL的相关知识,涵盖思维导图、架构、存储引擎、数据类型、索引、查询、事务、锁机制、调优、分区与分表分库、主从复制及其他问题。MySQL采用插件式存储引擎架构,支持多种存储引擎,如InnoDB和MyISAM,每种引擎具备不同的特性。文章详细介绍了InnoDB和MyISAM的对比,包括事务支持、行级锁定、索引类型等。此外,还探讨了MySQL的查询优化、性能调优、主从复制等内容,适合数据库开发者和运维人员阅读。如涉及版权问题,请联系作者删除。
Mysql全面总结