Details and examples of Docker data volumes and data containers
- 2020-05-27 07:42:57
- OfStack
Docker data volumes and data containers
I started to learn docker in these two days, and found that docker is really powerful, which greatly improves the efficiency of website deployment and maintenance. Therefore, I am going to docker all the several small stations I maintain at hand. During the process of sorting out, I felt that docker could be deployed and maintained by function or process without spending time on tedious configuration, but the data sharing between docker and the host and between docker were still a headache.
A few basic concepts:
docker: a container management technique, also referred to as the existing development tool chain.
container: container
image: mirror
volum: volum: volum: volum: volum: volum: volum: volum:
There are two main ways to manage data in a container:
Data volume (Data Volumes)
Data volume container (Data Volume Containers)
Data volume
The data volume is a special directory that can be used by the container. It bypasses the file system and provides many useful features:
Data volumes can be Shared and reused between containers; Changes to the data volume are immediately effective; Updates to data volumes do not affect mirroring; Volume 1 will continue to exist until no container is used.The use of data volumes, similar to mount operations on directories or files under Linux.
Mount the local directory into the container
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry latest 5c929a8b587a 29 hours ago 33.27 MB
genesis_centos latest 85bc3a58f134 5 days ago 277.6 MB
192.168.1.179:5000/busybox latest 9967c5ad88de 12 days ago 1.093 MB
busybox latest 9967c5ad88de 12 days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 13 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: Don't have access to /data/: There is no file or directory
[root@localhost ~]# mkdir /data/
[root@localhost ~]# docker run -itd -v /data/:/data1 centos bash
096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5
-v is used to specify the mount directory
The /data/ before ":" is the local directory
The /data1/ after ":" 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/mapper/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 you stop the container or delete it, 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 the 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" 14 seconds ago Up 13 seconds tender_euclid
You can specify the container name when you mount the directory, or you can randomly define it if you don't. For example, if we didn't specify it above, we generated a name tender_euclid, which we can use the command Docker ps to see the right-most column 1.
[root@localhost ~]# docker run -itd --volumes-from tender_euclid centos bash
3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164
So we created the new container using the centos image and used the data volume of the tender_euclid container.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3222c7c5c456 centos "bash" 26 seconds ago Up 25 seconds sick_albattani
e136b27a8e17 centos "bash" 6 minutes ago Up 6 minutes 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-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
[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 root 5 Oct 20 05:53 1.txt
-rw-r--r--. 1 root root 0 Oct 20 05:59 2.txt
-rw-r--r--. 1 root root 0 Oct 20 06:31 3.txt
[root@3222c7c5c456 /]# exit
exit
[root@localhost ~]# ls /data/
1.txt 2.txt 3.txt
Data volume container
Define the data volume container
Sometimes, we need multiple containers to share data with each other, like NFS in linux. So you can set up a dedicated data volume container, and then the other containers mount the data volume directly.
First, create the data volume container
[root@localhost ~]# docker run -itd -v /data/ --name cent_testv centos bash
fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096
Note: the /data/ here is the container's /data directory, 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" 52 minutes ago Up 52 minutes sick_albattani
e136b27a8e17 centos "bash" 58 minutes ago Up 58 minutes tender_euclid
[root@localhost ~]# docker exec -it cent_testv bash
[root@fb45150dbc21 /]# df -h
Filesystem Size Used Avail Use% 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 of the data volume mounted using the September 97en-from parameter does not need to be running itself
[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/mapper/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
0
[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/mapper/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
1
Migrate data using data volume containers
A backup of the data volume
[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/mapper/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
2
First we need to open a new container using the cent_testv data volume, and we also need to mount the local /vol_data_backup/ directory to the container's /backup, so that we can see the new files in the container /backup/directory directly in /vol_data_backup/ directory. Then package the files under /data/ into data.tar and put them under /backup.
[root@localhost ~]# docker exec -it 4f5bf bash
[root@4f5bf6f33f2c /]# df -h
Filesystem Size Used Avail Use% Mounted 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
restore
First create a new data volume container, then build a new container and mount the data volume container, then unpack tar.
[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/mapper/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
4
Thank you for reading, I hope to help you, thank you for your support of this site!