Objective
There are two main ways to manage data in a Docker container (data volumes) and volume container (data Volume containers), below we introduce the data management in the Docker, the need to learn to learn it.
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 data volumes, similar to Linux for directory or file mount
operations.
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.093 MB
busybox latest 9967c5ad88de days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 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@localhost ~]# mkdir/data/[root@localhost ~]# Docker run-itd-v/
D ATA/:/DATA1 CentOS Bash
096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5
-v
used to specify the Mount directory
“:”
the previous/data/is the local directory
The/data1/in the back is the “:”
directory in the container.
[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< C19/>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, we did not specify it above, we generated a name of Tender_euclid, the name can use the command to Docker ps
see 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 3222c7c5c456-CentOS "bash" seconds ago up seconds Sick_albattani e136b27a8e17 CentOS "bash" 6 minutes ago up 6 Minu TES tender_euclid [root@localhost ~]# Docker exec-it 3222 bash [root@3222c7c5c456/]# df-h-FileSystem Size Used Avail use% mounted on/dev/mapper/docker-253:0-1447735-3222c7c5c45687e0650b699a9291bc50ecc85030acf8f38 8c1c6a50b0dc67164 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@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 5 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 POR TS NAMES 0a80861145c9 CentOS "bash" 3 seconds ago up 2 seconds Mad_carson fb45150dbc21 centos "Bash" Minutes ago up minutes Cent_testv 3222c7c5c456 centos "bash" the Minutes ago up minutes si Ck_albattani e136b27a8e17 CentOS "bash" about a hour ago up about a hour tender_euclid [root@localhost ~]# D Ocker exec-it 0a8086 Bash [root@0a80861145c9/]# df-h filesystem Size Used avail use% mounted on/ Dev/mapper/docker-253:0-1447735-0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3 9.8G 231M 9.0G 3% /tmpfs 936M 0 936M 0%/dev shm 64M 0 64M 0%/dev/shm/dev/mapper/volgrou P-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/mapper/docker-253:0-1447 735-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@7169e8be6d3e/]# ls/backup/data.tar root@7169e8be6d3e [/]# Mv/backup/data.tar. [root@7169e8be6d3e/]# ls Anaconda-post.log bin Data.tar etc Lib Lost+found mnt proc run SRV tmp var backup data dev H ome lib64 Media opt root sbin sys usr [root@7169e8be6d3e/]# tar xvf data.tar data/data/fight.txt [root@7169e8be6d3e/]# ls/data/fight.txt [root@7169e8be6d3e/]# exit Exit [ Root@localhost ~]# ls/vol_data_backup/[root@localhost ~]#
Summarize
Docker provides adequate support for data management, and it is a good choice to use a data volume container. The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.