SoFunction
Updated on 2025-03-09

Data management for basic Docker learning

Preface

There are two main ways to manage data in docker containers: Data Volumes and Data Volume Containers. Below we will introduce data management in Docker in detail. If you need it, let’s learn it together.

Data volume

Data volumes are a special directory for container use. They bypass the file system and can provide many useful features:

Data volumes can be shared and reused among containers;

Modification of data volumes will be effective immediately;

Updates to data volumes will not affect the mirroring;

The volume will remain until there is no container used.

The use of data volumes is similar to the use of directories or files under Linux.mountoperate.

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: Unable to access/data/: There is no file or directory
[root@localhost ~]# mkdir /data/
[root@localhost ~]# docker run -itd -v /data/:/data1 centos bash
096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5

      -vUsed to specify the mount directory

      “:”The previous /data/ is the local directory

      “:”The following /data1/ is the directory in the container

[root@localhost ~]# touch /data/
[root@localhost ~]# echo "test" > /data/
[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/

[root@096460f831bf /]# cat /data1/
test
[root@096460f831bf /]# touch /data1/
[root@096460f831bf /]# exit
exit
[root@localhost ~]# ls /data/
 

Whether it is the container being stopped or deleted, the data still exists

[root@localhost ~]# docker stop 09646
09646
[root@localhost ~]# ls /data/
 
[root@localhost ~]# docker rm 09646
09646
[root@localhost ~]# ls /data/
 

Mount 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

In fact, when mounting the directory, you can specify the container name, and if it is not specified, it will be defined randomly. For example, we did not specify it above, so we generated a name tender_euclid, which can be used to use the command.Docker ps Look at the column on the far right.

[root@localhost ~]# docker run -itd --volumes-from tender_euclid centos bash
3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164

In this way, we use the centos image to create a new container and use 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/
 
[root@3222c7c5c456 /]# touch /data1/
[root@3222c7c5c456 /]# ls -l /data1/
total 4
-rw-r--r--. 1 root root 5 Oct 20 05:53 
-rw-r--r--. 1 root root 0 Oct 20 05:59 
-rw-r--r--. 1 root root 0 Oct 20 06:31 
[root@3222c7c5c456 /]# exit
exit
[root@localhost ~]# ls /data/
  

Data volume container

Define the data volume container

Sometimes, we need multiple containers to share data with each other, similar to NFS in Linux. So you can build a special data volume container, and then other containers can directly mount the data volume.

First create a data volume container

[root@localhost ~]# docker run -itd -v /data/ --name cent_testv centos bash
fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096

Notice:Here /data/ 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/
  

Other containers mount the data volume

[root@localhost ~]# docker run -itd --volumes-from cent_testv centos bash
0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3

Notice:The container that mounts the data volume using the --volumes-from parameter does not need to remain in operation itself

[root@localhost ~]# docker ps
CONTAINER ID  IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES
0a80861145c9  centos    "bash"    3 seconds ago  Up 2 seconds       mad_carson
fb45150dbc21  centos    "bash"    14 minutes ago  Up 14 minutes       cent_testv
3222c7c5c456  centos    "bash"    58 minutes ago  Up 58 minutes       sick_albattani
e136b27a8e17  centos    "bash"    About an hour ago Up About an hour      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-0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3 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@0a80861145c9 /]# touch /data/
[root@0a80861145c9 /]# exit
exit
[root@localhost ~]# docker exec -it cent_testv bash
[root@fb45150dbc21 /]# ls /data/

[root@fb45150dbc21 /]# exit
exit

Migrate data using 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 of all, we need to use the cent_testv data volume to open a new container. At the same time, we also need to mount the local /vol_data_backup/ directory to the /backup of the container. In this way, we can see the newly created files in the /backup directory in the container directly in the /vol_data_backup/ directory. Then package the files under the /data/ directory into files and put them in the /backup directory.

[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/


[root@4f5bf6f33f2c /]# tar cvf /backup/ /data/
tar: Removing leading `/' from member names
/data/
/data/
[root@4f5bf6f33f2c /]# exit
exit
[root@localhost ~]# ls /vol_data_backup/

recover

First create a new data volume container, then build a new container and mount the data volume container, and then unpack the tar package.

[root@localhost ~]# docker run -itd -v /data --name cent_testv2 centos bash
4cd696928bbe6e0aec9bf8b6856323d7228eb65006b21849eff9f0d41dcea90f
[root@localhost ~]# docker run -itd --volumes-from cent_testv2 -v /vol_data_backup/:/backup centos
7169e8be6d3e5836b626806696046195ed600a1f95b308495e90e6c7b15170d5
[root@localhost ~]# docker exec -it 7169 bash
[root@7169e8be6d3e /]# df -h
Filesystem                       Size Used Avail Use% Mounted on
/dev/mapper/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@7169e8be6d3e /]# ls /backup/

[root@7169e8be6d3e /]# mv /backup/ .
[root@7169e8be6d3e /]# ls
 bin  etc lib lost+found mnt proc run srv tmp var
backup    data dev  home lib64 media  opt root sbin sys usr
[root@7169e8be6d3e /]# tar xvf 
data/
data/
[root@7169e8be6d3e /]# ls /data/

[root@7169e8be6d3e /]# exit
exit
[root@localhost ~]# ls /vol_data_backup/
[root@localhost ~]#

Summarize

Docker provides sufficient support for data management, and using data volume containers is a good choice. The above is the entire content of this article. I hope it will help you study or work. If you have any questions, you can leave a message to communicate.