Data backup and recovery configuration for Docker data management

Source: Internet
Author: User
Tags db2 docker run

Docker Data Management

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

To mount a local directory or file in a data volume within a container

A data volume is a special directory that provides a container to use, bypassing the file system and providing a variety of features

Data volumes can be shared and reused directly in a container
Changes to the data volume will take effect immediately
Data updates do not affect mirroring
The volume always exists, regardless of whether the container is used

1, Mount Directory

Data volumes are similar to mount Mount operations in Linux

When using the Docker Run command, use the-v tag to create a data volume within the container, and multiple use-v cousin can create multiple

can also be invoked in Docker-compose.

such as: Docker run d-p 80:80--name name1-v/nginx:/usr/local/nginx
As shown above, the/nginx directory is mounted to the container volume/usr/lcoal/nginx, and if the directory does not exist, it is created automatically, his default permission is read/write (rw), the user can use RO to specify read-only, etc.
such as: Docker run d-p 80:80--name name1-v/nginx:/usr/local/nginx:ro
If RO is added, data on the data volume that is mounted inside the container cannot be modified

2. mount a local file as a data volume

The-V tag can also mount a single file from a host to a container as a data volume
such as: Docker run d-p 80:80--name name1-v/nginx.comf:/usr/local/nginx/nginx.conf:ro
However, we do not recommend this approach, because when the container is mounted, if the container using VI modification or SED changes will manipulate the file's inode change, which in the current version of Docker may operate the error

3, Data volume container

Usually in some continuously updated data, we can use a data volume container, such as:

[Root@localhost ~]# Docker run-it-v/mydata1--name MyData Debian
root@d3bc579080cb:/# echo ' www.linuxea.com ' > Mydata/data.txt
root@d3bc579080cb:/# echo ' Date +%f-%t ' > Mydata1/data.txt
root@d3bc579080cb:/# Cat Mydata1/data.txt
2016-09-01-04:14:35
root@d3bc579080cb:/#
Now, use the other container and use--volumes-from to mount the data volume in the MyData container, such as:
DB1 Mount

[Root@localhost ~]# Docker run-it--volumes-from mydata--name db1 Debian
root@9a42647f5a2d:/# Cat Mydata1/data.txt
2016-09-01-04:14:35
root@9a42647f5a2d:/#

DB2 Mount

[Root@localhost ~]# Docker run-it--volumes-from mydata--name DB2 Debian
root@8f7738f2f159:/# Cat Mydata1/data.txt
2016-09-01-04:14:35
root@8f7738f2f159:/#
At this point, no matter where the modification will be synchronized, of course, you can also use the--volumes-from parameter multiple times to mount multiple data volumes from multiple containers

If deleted, you need to use Docker rm-v to specify that the associated container be deleted at the same time, and if only the mounted container is deleted, the data volume will not be automatically deleted
# #4, container volume backup and recovery:

1. Backup Container Volume

We follow the example above, using the MyData container volume that is mounted above

Before backing up, we first append a row to the data file in the MyData1 directory in the MyData container that was previously mounted

[Root@1002797adf78 mydata1]# Echo ' is I ' a backup ' date +%c-%p ' ">> data.txt
[Root@1002797adf78 mydata1]# Cat Data.txt
2016-09-01-16:08:24
Is I am a backupthu Sep 1 16:32:40 2016-pm
[Root@1002797adf78 mydata1]#

Now we back up this directory in the container to the local
Docker run--volumes-from mydata-v $ (PWD):/backup--name back Kriation/centos7 tar cf/backup/beifen.tar/mydata1

This command first creates a container, uses--volumes-from to mount the MyData container's data volume, which is the MyData1 directory, to the backup directory, and the container starts with the tar command to back up the/mydata1 to the host's current directory: Tar CF/ Backup/benfen.tar/mydata1

As shown below:

[Root@gitlab dockerhub]# Docker run--volumes-from mydata-v $ (PWD):/backup--name back Kriation/centos7 tar cf/backup/be Ifen.tar/mydata1
Tar:removing leading '/' from member names
[Root@gitlab dockerhub]# ls
Beifen.tar file123
[Root@gitlab dockerhub]#

We unzip it, check it out, backup is complete.

[Root@gitlab dockerhub]# Tar XF Beifen.tar
[Root@gitlab dockerhub]# Cat Mydata1/data.txt
2016-09-01-16:08:24
Is I am a backupthu Sep 1 16:32:40 2016-pm
[Root@gitlab dockerhub]#

2. Recover Container Volume

1, if you want to restore a container, first create a container with a data volume NGX

[Root@gitlab dockerhub]# Docker run-it-v/mydata1--name ngx Kriation/centos7/bin/bash
[Root@400835c3ce88/]#

2, create a new container, mount the NGX container, and extract it into the mounted directory using the Tar XF

[Root@gitlab dockerhub]# Docker run--volumes-from ngx-v $ (PWD):/backup Kriation/centos7 tar Xf/backup/beifen.tar
3, when you return to NGX, you can see that the data has been restored

[Root@400835c3ce88/]# CD mydata1/
[Root@400835c3ce88 mydata1]# ls
Data.txt

Import and container everywhere

1, Import

[Root@gitlab ~]# Docker export b889bfd7a6ee > Nginx.tar
[Root@gitlab ~]# ll Nginx.tar
-rw-r--r--. 1 root 235801600 September 2 02:08 Nginx.tar
[Root@gitlab ~]#

2, Export

[Root@gitlab ~]# cat Nginx.tar |docker import-marksugar/nginx:123
sha256: D236F62AE8C4ED106E00BA073877F90F19B21DF0C8B3A67CA744BAEF9D7495AF
[Root@gitlab ~]# docker Images
REPOSITORY                   TAG                  IMAGE id             created              SIZE
marksugar/nginx              123                  d236f62ae8c4        6 seconds ago        MB

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.