When using docker, we need to view the data generated in the container, and share and back up the data between the container and the container, and before the container and the host, here we need to go to the container data management. Currently, the following two methods are provided for data management:
# Data volumes
# Data volume container data volumes containers
I. Data volumes
To put it bluntly, a data volume is a special directory, similar to mounting a directory or file in linux, but it bypasses the file system. It has the following features:
(1) data volumes can be shared and reused between containers.
(2) changes to the data volume will take effect immediately
(3) updating data volumes does not affect the image (Image read-only)
(4) The volume will always exist, knowing that no container is used
You can set the data volume through the-v parameter to keep up with the directory. Example:
1. Create a data volume/homedata to the container os123:
[Root @ docker5 home] # docker run-d-ti -- name os123-v/homedata centos
[Root @ docker5 home] # docker exex-ti os123/bin/bash
[Root @ d1a05a7d5efe/] # ll
Total 40
-Rw-r --. 1 root 18301 Jun 2 anaconda-post.log
Lrwxrwxrwx. 1 root 7 Jun 2 13:25 bin & gt; usr/bin
Drwxr-xr-x. 5 root 380 Jun 23 02: 42 dev
Drwxr-xr-x. 48 root 4096 Jun 23 etc
Drwxr-xr-x. 2 root 6 Aug 12 2015 home
Drwxr-xr-x. 2 root 6 Jun 23 02: 42 homedata
..
[Root @ d1a05a7d5efe/] # cd homedata/
[Root @ d1a05a7d5efe homedata] # ll
Total 0
[Root @ d1a05a7d5efe homedata] # touch 21yunwei.txt; echo 123 & gt; 21yunwei.txt
[Root @ d1a05a7d5efe homedata] # cat 21yunwei.txt
123
2. Mount a directory/home/data on the local server to the os456 directory/homedata of the container:
Home/datafirst create a file 1.txt with the content hello world
[Root @ docker5 home] # docker run-d-ti -- name os456-v/home/data:/homedata centos
9379d5ef84ffc532000df2637ff3280da001573029e8da-27d3b71cc88aa4703
[Root @ docker5 home] # docker exec-ti os456/bin/bash
[Root @ 9425d5ef84ff homedata] # cd/homedata; cat 1.txt
Hello world
Through the two containers os123 and os456 above, I learned how to create a data volume and how to mount a local directory to the data volume. Note: If the two containers are independently attached data volumes (that is, the same data volume container is not mounted), the data is independent of each other, enter/homedata in the same directory of different data volumes. The content may be different.
Note: When you delete a container, the data volume is not deleted. If you want to delete a data volume when deleting a container, you must add the-v parameter. For example: docker rm os456-v/homedata
II. Data volume container
The created containers are not uniform in many cases. Data sharing between containers is required for data synchronization and update. In this way, you need to create a data volume container.
A data volume container is a common container with a set data volume. It is dedicated for mounting other containers. Use the-volumes-from data volume container name.
I have a website program that is stored in the/home/webdata directory on the server, and a data volume container webdata is created below, at the same time, Mount/home/webdata on my server to the/web Directory of the data volume container:
[Root @ docker5 home] # docker run-d-ti -- name webdata-v/home/webdata:/home/web centos
Enter the container and view the data
[Root @ docker5 home] # docker exec-ti webdata/bin/bash
[Root @ 289598d6e24d/] # cd/home/web/
[Root @ 289598d6e24d web] # ll
Total 7872
Drwxr-xr-x. 3 root 54 Mar 27 2013 META-INF
Drwxr-xr-x. 6 root 4096 Dec 25 2014 WEB-INF
Drwxr-xr-x. 3 root 63 Mar 27 2013 css
Drwxr-xr-x. 2 root 8192 Mar 27 2013 flags
-Rw-r --. 1 root 97 Mar 27 2013 index. jsp
Drwxr-xr-x. 2 root 4096 Mar 27 2013 js
Drwxr-xr-x. 2 root 6 Jun 23 0:43 probe
You can see that the/home/webdata data on the server is synchronized by setting up 1.txt and inserting the content here. It can be seen that the mounting of containers and directories is normal.
[Root @ docker5 home] # docker run-MHD -- volumes-from webdata -- name os147 centos
[Root @ docker5 home] # docker run-MHD -- volumes-from webdata -- name os258 centos
Two containers are created respectively, and the same data volume container is mounted through-volumes-from webdata. You can go to os147 and os258 to view the/home/web visible data respectively, as a result, data is shared and synchronized.
[Root @ docker5 home] # docker exec-ti os147/bin/bash
[Root @ b4cfa4c4e11c/] # cd/home/web/
[Root @ b4cfa4c4e11c web] # ll
Total 7876
-Rw-r --. 1 root 11 Jun 23 :46 1.txt
Drwxr-xr-x. 3 root 54 Mar 27 2013 META-INF
Drwxr-xr-x. 6 root 4096 Dec 25 2014 WEB-INF
Drwxr-xr-x. 3 root 63 Mar 27 2013 css
Drwxr-xr-x. 2 root 8192 Mar 27 2013 flags
-Rw-r --. 1 root 97 Mar 27 2013 index. jsp
Drwxr-xr-x. 2 root 4096 Mar 27 2013 js
Drwxr-xr-x. 2 root 6 Jun 23 0:43 probe
Note:
1. You can use the-volume-from parameter multiple times to Mount multiple directories from multiple containers. You can also mount the data volume from other containers that have mounted the data volume (similar to transfer ).
2. Emphasize that if the mounted container is deleted, the data volume will not be automatically deleted. If you want to delete a data volume when deleting a container, you must add the-v parameter.
3. Data backup, data recovery, and data migration through data volume containers
1. Backup: we create a container dedicated to backing up probe: probebak to back up the data in the data volume container. The command is as follows:
Docker run-ASD -- volumes-from webdata -- name probebak-v/home/web_probebak:/backup centos tar zcvf/backup/web_probe.tar.gz/home/web
Command to create a dedicated backup container probebak, mount the data volume container webdata, and mount the local server directory/home/web_probebak to the/backup directory container on the backup container after it is started, tar zcvf/backup/web_proce.tar.gz/home/web will be executed to complete/home/web backup on the server and package it into/backup/web_proce.tar.gz, that is, the data is backed up in/home/web_probebak/web_probe.tar.gz.
2. Restore
Create a container named os999 and mount it with data volumes/testdata
[Root @ docker5 home] # docker run-v/testdata -- name os999 centos/bin/bash
Create another container, mount the data volume you just set through-volumes-from os999, and decompress the data:
[Root @ docker5 home] # docker run -- volumes-from os999-v/home/web_probebak:/backup busybox tar zxvf/backup/web_probe.tar.gz
Usage of loopback devices is stronugly discouraged for production use. Either use '-- storage-opt dm. thinpooldev' or use '-- storage-opt dm. no_warn_on_loop_devices = true' to suppress this warning.
Home/web/
Home/web/probe.zip
Home/web/probe/
Home/web/css/
Home/web/css/classic/
Home/web/css/classic/datasourcetest.css.