Managing Data in Containers,managingcontainers
Managing Data in Containers,managingcontainers
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 --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.
1、10.7.3以下的版本:
QQ所有的用户信息都保存在~/Library/Application Support/QQ/;
若您需要删除登录的历史记录,打来并编辑:~/Library/Application Support/QQ/Defaults/accounts.xml ;
聊天记录文件:~/Library/Application Support/QQ/Defaults/QQ号码文件夹/Msg2.0.db。 2、10.7.3及其以上的版本:
QQ所有的用户信息都保存在~/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/
若您需要删除登录的历史记录,打来并编辑~/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/ Defaults/accounts.xml ;
聊天记录文件:~/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/QQ号码文件夹/Msg2.0.db。
温馨提示:打开QQ信息文件的方法
1、在terminal下,输入cd ~/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/
2、在finder的菜单项中----前往---文件夹,输入~/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/
楼下复制我帖者,没有后代~!
The use of containers in export shipments makes the transport and handling easier and faster. The crane and gantry are commonly used in handling containers. The forklift is also used at the docks and container terminals to move the 20' and shorter dry cargo containers, which are equipped with forklift pockets---fork pockets or tine pockets.
The ports worldwide handle over 100 million TEUs annually. The unit TEU (twenty-foot equivalent unit) is used to express the relative number of containers based on the equivalent length of a 20' container. For example, 100 containers of 20' is 100 TEUs, while 100 containers of 40' is 200 TEUs.
The container ships used in the international traffic are designed with the cells (compartments with cell guides) resembling a honeycomb wherein the containers are placed, thus named cellular container ships.
The ships are bigger and faster nowadays, especially those used in the deep-sea voyage (long haul). Those rated below 20 knots are common in the short-sea voyage (short haul). The knot is a unit of ship's speed, being one nautical mile per hour. One nautical mile is 1.852 kilometers. A ship that steams at 20 knots is moving at a speed of about 37 kilometers per hour.
Some cellular container ships in the 20 to 23 knot range can accommodate 2,000 to 3,000 TEUs. Some rated 24 knots have a carrying capacity of 4,000 to 4,900 TEUs and load of 56,000 to 75,000 metric tons. The length of the vessel can be about 90......余下全文>>
评论暂时关闭