Docker data volume, data volume container detailed introduction _docker

Source: Internet
Author: User
Tags db2 docker run

Docker data volume, data volume container detailed introduction

Introduction

Sometimes, when our service is running, it will produce some logs, or we need to back up the data in the container, or even data sharing between the containers, which necessarily involves the data management operation of the container.

There are two main ways of managing data in a container:

Data volume
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 that do not affect mirroring
-The volume will always exist until no container is used

# (Mount) (similar to Linux mount)

Create a data volume

When using the Docker Run command, you can create a data volume within the container using the-v parameter marker, and multiple data volumes can be created with the-V tag multiple times

Docker RUN-DP--name web-v/webapp ubuntu:14.04
After #这里我们没有-p, there is no port, and if we do not set the port relationship between the container and the host, Docker will randomly map

Mount a host directory as a data volume
You can also use the-v tag to specify that a local existing directory be mounted 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 to the container's/opt/webapp directory:

This feature is very handy for testing, such as a user can place programs or data into a local directory, and then run and use them in a container. In addition, the

The path to the directory must be an absolute path, and Docker will be created automatically if the directory does not exist.

Docker the default permission to mount a data volume is read-write, and the user can also pass, RO specify read only:

Docker RUN-DP--name web-v/src/webapp:/opt/webapp:ro ubuntu:14.04
# added: RO, the data volume of the container mounted in the data can not be modified.

mount a native file as a data volume

The-V tag 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
# This allows you to record the history of the command entered in the container (different shell versions vary)

Data Volume container

If the user needs to share some continuously updated data between the containers, the easiest way is to use the data volume container, which is actually a common container that is designed to provide data volumes for other containers to mount.
First, create a data volume container dbdata, and in which you create a data volume 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
Docker Run-ti--volumes-from dbdata--name DB2

The container DB1 and DB2 both mount the same data volume to the same/dbdata directory. Three containers are written by either party in the directory and can be seen by other containers.

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

root@df392e32f0p6:/ # Touch Test1
root@df392e32f0p6:/#ls
Test

We view it in the DB1 container:

Docker Run-ti--volumes-from dbdata--name db1
root@92597e32f0p6:/# ls dbdata/
Test

We can use the –volumes-from parameter more than once to mount multiple data volumes from multiple containers. You can also mount a data volume from another container on which the container volume is mounted:

Docker run-d--name db3--volumes-from db1
#注意: The container on which the data volume is mounted using the--volumes-from parameter does not need to remain in the running state

If you delete the mounted containers (including DBDATA,DB1 and DB2), the data volumes are not automatically deleted. If you want to delete a data volume, you must explicitly use the Docker rm-v command to specify that the associated container be deleted at the same time you delete the last container on which it was mounted.

Using a data volume container allows users to freely upgrade and move data volumes between containers.

Migrating data with Data volume containers

A data volume container can be used to back up and reply to data volumes in order 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 bit complicated, so let's see what this command does.

1. First, a container worker is created using the Ubuntu image.
2. Use the –volumes-from dbdata parameter to let 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/backup directory of the Worker container

After the worker container starts, the tar cvf/backup/backup.tar/dbdata command is used to back up the content under/dbdata as a/backup/backup.tar in the container, the Backup.tar in the host host's current directory.

Recovery

If you want to recover data to a container, you can follow these steps to create a container dbdata2 with a data volume first:

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

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

Docker run--volumes-from dbdata2-v $ (PWD):/backup busybox tar Xvf/backup/backup.tar

Thank you for reading, I hope to help you, thank you for your support for this site!

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.