Docker Quick Start Series (4): Data volume, data volume container concept and related operations

Source: Internet
Author: User
Tags docker run

Introduction

In some cases, it is necessary for our service to run to generate some logs, or we need to back up the data in the container, or even share data between multiple containers, which inevitably involves the data management operations of the container.

There are two main ways in which you manage data in a container:

    • Data volumes
    • Data Volume container

A data volume is a special directory that can be used by a container, bypassing the file system and providing many useful features:
-Data volumes can be shared and reused between containers
-Changes to the data volume will take effect immediately
-Updates to the data volume do not affect mirroring
-the volume will persist until no container is used

#(类似linux下的挂载(mount)) 
Create a data volume

When using the Docker Run command, use the-v parameter tag to create a data volume within the container, multiple data volumes can be created using the-V flag multiple times

docker run -dp --name web -v /webapp ubuntu:14.04#这里我们没有-p后,并没有制定端口,如果我们不制定容器与宿主机之间映射的端口关系的话,Docker会随意映射

Mount a host directory as a data volume

Using the-V flag can also specify that a local existing directory be attached to the container as a data volume

docker run -dp --name web -v /src/webapp:/opt/webapp ubuntu:1404

The above command loads the host/src/webapp directory into the/opt/webapp directory of the container:
This feature is handy for testing, such as allowing users to place programs or data into a local directory, and then run and use them inside the container. Also, the path to the local directory must be an absolute path, and if the directory does not exist, Docker will create it automatically.
The default permission for Docker mount data volumes is read-write, and the user can also pass, RO to specify read-only:

docker run -dp --name web -v /src/webapp:/opt/webapp:ro ubuntu:14.04# 加了:ro之后,容器内挂载的数据卷的数据就无法修改。
Mount the native file as a data volume

The-V flag can also mount a single file from the host to the container as a data volume:

Docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu:14.04# 这样就可以记录在容器输入过的命令历史(不同shell版本有所不同)
Data Volume container

If users need to share some continuously updated data between containers, the simplest way is to use a data volume container, which is actually a common container that is dedicated to providing data volumes for other containers to mount.
First, create a data volume container dbdata, and in which you create a data volume to mount to/dbdata:

docker run -ti -v /dbdata --name dbdata ubuntu:14.04

We can then use –volumes-form in other containers to mount the data volumes in the Dbdata container, such as creating DB1 and DB2 two containers, and mounting the data volumes from the Dbdata container:

docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04docker run -ti --volumes-from dbdata --name db2 ubuntu:14.04

At this point the containers DB1 and DB2 both mount the same data volume to the same/dbdata directory. Three containers can be seen by any of the other containers that are written in that directory.

For example, create a test file in the Dbdata container:

root@df392e32f0p6:/# touch test1root@df392e32f0p6:/#lstest

We look at it in the DB1 container:

docker run -ti --volumes-from dbdata --name db1 ubuntu:14.04root@92597e32f0p6:/# ls dbdata/test

We can use the –volumes-from parameter multiple times to mount multiple data volumes from multiple containers. You can also mount a data volume from another container that already has a container volume attached:

docker run -d --name db3 --volumes-from db1 ubuntu:14.04#注意:使用--volumes-from参数所挂载数据卷的容器自身并不需要保持在运行状态

If you delete a mounted container (including DBDATA,DB1 and DB2), the data volume is not automatically deleted. If you want to delete a data volume, you must explicitly use the Docker rm-v command to delete the associated container at the same time when you delete the last container that also mounts it.

The use of data volume containers allows users to freely upgrade and move data volumes between containers.

Migrating data with a data volume container

A data volume container can be used to back up and reply to data volumes to enable data migration.

Backup
docker run --volumes-from dbdata -v $(pwd):/backup --name worker ubuntu:14.04 tar cvf /backup/backup.tar /dbdata

This command is a little complicated, so let's see what this command does.
1. First create a container worker with Ubuntu image.
2. Use the –volumes-from dbdata parameter to have the worker container mount the data volume of the Dbdata container (that is, the dbdata data volume);
3. Use the-V $ (PWD):/backup parameter to mount the local current directory to the worker container's/backup directory

After the worker container is started, the tar cvf/backup/backup.tar/dbdata command is used to back up the contents of/dbdata to/backup/backup.tar within the container, that is, the Backup.tar under the current directory of the host host.

Recovery

If you want to restore data to a container, you can do this by first creating a container with a data volume DBDATA2:

docker run -v /dbdata --name dbdata2 ubuntu:14.04

Then create another new container, mount the DBDATA2 container, and use Ubtar to unzip the backup file onto the mounted container volume:

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

Docker Quick Start Series (4): Data volume, data volume container concept and related operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.