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
A 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 aDockerfile
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 aDockerfile
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
andsed
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
--in-place
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.
Managing Data in Containers的更多相关文章
- Docker数据管理-数据卷 data volumes和数据卷容器data volumes containers的使用详解
此文来源于:https://yq.aliyun.com/ziliao/43471 参考原文件之外,做了些修改. Volume数据卷是Docker的一个重要概念.数据卷是可供一个或多个容器使用的特殊目录 ...
- Linking Containers Together
Linking Containers Together In the Using Docker section we touched on connecting to a service runnin ...
- 【新技术】Docker 学习笔记
原文地址 一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- ...
- Docker相关文档
网上找到的一个入门级Docker学习笔记,写的不错,值得一看. 转自:http://www.open-open.com/lib/view/open1423703640748.html#articleH ...
- 【云计算】docker run详解
Docker学习总结之Run命令介绍 时间 2015-01-21 17:06:00 博客园精华区 ...
- docker nexus oss
docker login/search x.x.x.x:8081 sonatype/docker-nexus Docker images for Sonatype Nexus with the Ora ...
- 非常详细的docker学习笔记
http://www.open-open.com/lib/view/open1423703640748.html 一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化 ...
- 非常详细的 Docker 学习笔记
一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docke ...
- Docker学习总结之Run命令介绍
Docker学习总结之Run命令介绍 本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 在使用Docker时,执行最多的命令某 ...
随机推荐
- BZOJ 1257: [CQOI2007]余数之和sum( 数论 )
n >= k 部分对答案的贡献为 k * (n - k) n < k 部分贡献为 ∑ (k - ⌊k / i⌋ * i) = ∑ , ⌊k / i⌋ 相等的数是连续的一段, 此时这段连 ...
- 正确处理Windows电源事件
简介为系统挂起与恢复而进行的应用准备步骤 曾几何时,当您正要通过应用提交或发布一些重要数据时,突然遇到一些急事需要处理,而且会耽误很长时间.当您完成任务回到电脑前时,发现电脑已经自动进入 了挂起状态, ...
- HTML文档类型DTD与浏览器怪异模式
虽然在兼容IE6时候经常会注意到两个模式的区别,但是系统的理解起来,还没有认真总结过.看了一些网上的资料.结合自己的理解汇总了一下,放在这里备忘并分享给大家. 浏览器从服务端获取网页后会根据文档的DO ...
- Android ListView条目全选功能,不用checkbox实现!
大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...
- asp.net2.0安全性(4)--Login系列控件--转载来自车老师
前面主要说了与安全相关的一系列的类,现在我们使用这些类就可以做出我们自己的安全系统了.其实微软的目的远不至于此,下面我们就来看一下微软为我们提供的Login系列控件. Login系列控件是微软为了简化 ...
- EasyUI - 一般处理程序 返回 Json值
使用 一般处理程序(ashx)返回Json值. 原始Json数组的格式: [ { ", "name":"张三", "sex":&q ...
- MSSQL - SqlDataAdapter连接数据库提高性能用法
SqlDataAdapter 与 SqlConnection 和 SqlCommand 一起使用,以便在连接到 SQL Server 数据库时提高性能. SqlDataAdapter 的这一实现自动打 ...
- 转:python idle 清屏问题的解决
http://www.cnblogs.com/maybego/p/3234055.html python idle 清屏问题的解决 在学习和使用python的过程中,少不了要与python idle打 ...
- 程序中非action获得spring容器
java类: public class MySpringContext implements ApplicationContextAware{ private static ApplicationCo ...
- 一些窗口API函数,比如SetForegroundWindow,SwitchToThisWindow
SetForegroundWindowSwitchToThisWindow procedure TApplication.BringToFront;varTopWindow: HWnd;beginif ...