Docker data volumes and data containers
These two days began to learn Docker, found that Docker is really strong, so that the site deployment and maintenance efficiency greatly improved. So ready to be on hand to maintain a few station, all Docker. It is felt in the process of finishing that Docker can be deployed and maintained in a functional or process unit, without having to spend more time on cumbersome configuration, but the data sharing between Docker and the host and the sharing of data between Docker is still a headache and a worry.
Several basic concepts:
Docker: a container management technique, also referred to as an existing development tool chain.
Container: containers
Image: mirroring
volum: Volume [Translator: volume can be understood as a file path in the computer]
There are two main ways of managing data in a container:
Data volume (volumes)
Data volume container (Volume containers)
Data volume
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 be effective immediately;
- Updates to the data volume do not affect mirroring;
- The volume will always exist until no container is used.
The use of a data volume, similar to the mount operation of a directory or file under Linux.
To mount a local directory into a container
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE Registry latest 5c929a8b587a hours ago 33.27 MB Genesis_centos Latest 85bc3a58f134 5 days ago 277.6 MB 192.168.1.179:5000/busybox latest 9967c5ad88de days ago 1.09 3 MB busybox latest 9967c5ad88de days ago 1.093 MB centos-6-x86 latest 8fca9486a39 B days ago 341.3 MB centos_with_net latest 3e8ea8607f08 4 weeks ago 294.9 MB CentOS Latest 9BAAB0AF79C4 6 weeks ago 196.7 MB [root@localhost ~]# Ls/data/ls: Unable to access/data/: No file or directory [Root@loca Lhost ~]# mkdir/data/[root@localhost ~]# docker run-itd-v/data/:/data1 CentOS Bash 096460f831bfd72b2efc6ba6b7e7bb0601 52afa49506ef26e0fa3cb03974f8d5
- -V is used to specify the Mount directory
- /data/in front of ":" is a local directory
- ":" The/data1/in the container is the table of contents
[Root@localhost ~]# touch/data/1.txt
[root@localhost ~]# echo "Test" >/data/1.txt
[root@localhost ~]# Docker exec-it 09646 bash
[root@096460f831bf/]# df-h
filesystem Size Used avail use% mounted on
/dev/ma Pper/docker-253:0-1447735-096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5 9.8G 231M 9.0G 3%/
tmpfs 936M 0 936M 0%/dev
shm 64M 0 64M 0%/dev/shm
/dev/mapper /volgroup-lv_root 35G 6.0G 28G 18%/data1
[root@096460f831bf/]# ls/data1/
1.txt
[ ROOT@096460F831BF/]# cat/data1/1.txt
test
[ROOT@096460F831BF/]# touch/data1/2.txt
[ ROOT@096460F831BF/]# exit
exit
[root@localhost ~]# ls/data/
1.txt 2.txt
Whether the container is stopped or deleted, the data still exists.
[Root@localhost ~]# Docker stop 09646
09646
[root@localhost ~]# ls/data/
1.txt 2.txt
[root@localhost ~ ]# Docker rm 09646
09646
[root@localhost ~]# ls/data/
1.txt 2.txt
mount a data volume
[Root@localhost ~]# Docker run-itd-v/data/:/data1 CentOS Bash
E136b27a8e177d878e76c60aafade32df947a60f77b3f95dcaf0680b7ffbc6e8
[root@localhost ~]# Docker PS
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e136b27a8e17 CentOS "bash" seconds ago seconds Tender_euclid
In fact, when you mount a directory, you can specify the container name, or randomly define it if you do not specify it. For example, the above we did not specify it, we generated a name for Tender_euclid, the name can use the command Docker PS look at the rightmost column.
[Root@localhost ~]# Docker run-itd--volumes-from tender_euclid CentOS Bash
3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164
This allows us to create a new container using the CentOS image and use the Tender_euclid container's data volume.
[Root@localhost ~]# Docker PS CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 32 22c7c5c456 CentOS "bash" seconds ago up seconds Sick_albattani E136b27a8e17 CentOS
Bash 6 minutes 6 minutes Tender_euclid [root@localhost ~]# Docker exec-it 3222 Bash [root@3222c7c5c456/]# df-h filesystem Size Used avail use% mounted on/dev/m
apper/docker-253:0-1447735-3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164 9.8G 231M 9.0G 3%/ Tmpfs 936M 0 936M 0%/dev SHM 64M 0 64M 0%/dev/shm/dev/mapper/volgroup-lv_root 35G 6.0G 28G 18%/data1 [R
oot@3222c7c5c456/]# ls/data1/1.txt 2.txt [root@3222c7c5c456/]# touch/data1/3.txt [root@3222c7c5c456/]# ls-l]/data1/ Total 4-rw-r--r--. 1 root root5 Oct 05:53 1.txt-rw-r--r--. 1 root 0 Oct 05:59 2.txt-rw-r--r--.
1 root 0 Oct 06:31 3.txt [root@3222c7c5c456/]# exit Exit [root@localhost ~]# ls/data/1.txt 2.txt 3.txt
Data Volume container
To define a data volume container
Sometimes we need multiple containers to share data with each other, similar to NFS inside Linux. So you can build a specialized data volume container, and then the other container mounts the data volume directly.
First set up the data volume container
[Root@localhost ~]# Docker run-itd-v/data/--name cent_testv CentOS Bash
fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096
Note: The/data/here is the/data directory of the container, not the local/data/directory.
[Root@localhost ~]# Docker PS
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb45150dbc21 centos "bash" 8 minutes ago up 8 minutes Cent_testv
3222c7c5c456 CentOS "bash" minutes ago up minutes Sick_albattani
e136b27a8e17 CentOS "bash" minutes ago up minutes tender_euclid
[Root@localhost ~]# Docker exec-it cent_testv bash
[root@fb45150dbc21/]# df-h
filesystem Size Used Avail Us E% mounted on
/dev/mapper/docker-253:0-1447735- fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096 9.8G 231M 9.0G 3%/
tmpfs 936M 0 936M 0%/dev
shm 64M 0 64M 0%/dev/shm
/dev/mapper/volgroup-lv_root 35G 6.0G 28G 18%/data
[root@fb45150dbc21/]# ls/data/[
root@fb45150dbc21/]# exit
exit
[ Root@localhost ~]# ls/data/
1.txt 2.txt 3.txt
Other containers mount the data volume
[Root@localhost ~]# Docker run-itd--volumes-from cent_testv CentOS Bash
0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3
Note: The container on which the data volume is mounted using the –volumes-from parameter does not need to remain in the running state
[Root@localhost ~]# Docker PS CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a 80861145C9 CentOS "bash" 3 seconds ago up 2 seconds Mad_carson Fb45150dbc21 CentOS "bash" minutes ago up minutes Cent_testv 3222c7c5c456 centos "bash" minutes ago up minutes Sick_albattani e136b27a8e17 centos "bash" about a hour ago up about a h Our tender_euclid [root@localhost ~]# Docker exec-it 0a8086 bash [root@0a80861145c9/]# df-h-FileSystem Size Used Avail use% mounted on/dev/mapper/docker-253:0-1447735-0a80861145c9a26276 18a78db2b7225eba64137d4664d3706e02c1c623cde5e3 9.8G 231M 9.0G 3%/TMPFS 93 6M 0 936M 0%/dev shm 64M 0 64M 0%/dev/shm/dev/mapper/volgroup-lv _root 35G 6.0G 28G 18%/data
[Root@0a80861145c9/]# touch/data/fight.txt
[root@0a80861145c9/]# exit
exit]
[root@localhost ~]# Docker exec-it CENT_TESTV bash
[root@fb45150dbc21/]# ls/data/
fight.txt
[root@fb45150dbc21/]# exit
exit
Migrating data with Data volume containers
Backup of data volumes
[Root@localhost ~]# Docker run-itd--volumes-from cent_testv-v/vol_data_backup/:/backup CentOS Bash
4f5bf6f33f2c78197e54e5145824e98bf89d802376e83019c2913b336fbd9d20
First we need to use the CENT_TESTV data volume to open a new container, and we also need to mount the local/vol_data_backup/directory under the container/backup, so that the new file in the container/backup directory we can directly in the/vol_ data_backup/directory to see the. The files below the/data/directory are then packaged into Data.tar files and placed under the/backup directory.
[Root@localhost ~]# Docker exec-it 4f5bf bash
[root@4f5bf6f33f2c/]# df-h
filesystem Size Used avail use% Mo unted on
/DEV/MAPPER/DOCKER-253:0-1447735-4F5BF6F33F2C78197E54E5145824E98BF89D802376E83019C2913B336FBD9D20 9.8G 231M 9.0G 3%/
tmpfs 936M 0 936M 0%/dev
shm 64M 0 64M 0%/DEV/SHM
/dev/mapper/volgroup-lv_root 35G 6.0G 28G 18%/data
[root@4f5bf6f33f2c/]# ls/backup/
[ ROOT@4F5BF6F33F2C/]# ls/data/
fight.txt
[root@4f5bf6f33f2c/]# tar cvf/backup/data.tar/data/
tar: Removing leading '/' from member names
/data/
/data/fight.txt
[root@4f5bf6f33f2c/]# exit
exit
[root@localhost ~]# ls/vol_data_backup/
Data.tar
Recovery
First create a new data volume container, then build a fresh container and mount the data volume container, and then unpack the tar packets.
[Root@localhost ~]# Docker run-itd-v/data--name cent_testv2 CentOS Bash 4cd696928bbe6e0aec9bf8b6856323d7228eb65006b21 849eff9f0d41dcea90f [root@localhost ~]# Docker run-itd--volumes-from cent_testv2-v/vol_data_backup/:/backup CentOS 71 69e8be6d3e5836b626806696046195ed600a1f95b308495e90e6c7b15170d5 [root@localhost ~]# Docker exec-it 7169 Bash [ root@7169e8be6d3e/]# df-h filesystem Size Used avail use% mounted On/dev/ma Pper/docker-253:0-1447735-7169e8be6d3e5836b626806696046195ed600a1f95b308495e90e6c7b15170d5 9.8G 231M 9.0G 3%/TMPFS 936M 0 936M 0%/dev SHM 64M 0 64M 0%/dev/shm/dev/mapper/volgroup-lv_root 35G 6.0G 28G 18%/data [Root@7169e8
be6d3e/]# Ls/backup/data.tar [root@7169e8be6d3e/]# Mv/backup/data.tar. [root@7169e8be6d3e/]# ls Anaconda-post.log bin Data.tar etc Lib Lost+found mntProc Run SRV tmp var backup data dev Home lib64 media opt root sbin sys usr [root@7169e8be6d3e/]# tar xvf da Ta.tar data/data/fight.txt [root@7169e8be6d3e/]# ls/data/fight.txt [root@7169e8be6d3e/]# exit Exit [Root@localhost ~]
# ls/vol_data_backup/[Root@localhost ~]#
Thank you for reading, I hope to help you, thank you for your support for this site!