Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker storage management and backup and recovery of container data

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Docker storage management and container data backup and recovery". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "Docker storage management and container data backup and recovery".

01 Docker storage overview

By default, all files and data created in the container are stored in the writable layer of the container. If you follow the default operation, you will have the following problems:

1) the deleted data of the container will not be saved, and it is very difficult for another process to call the data in the container.

2) the writable layer of the container is closely coupled with the host running the container, so it is impossible to easily move the data elsewhere.

3) to write container data to the container writable layer, you need to use Storage driver (Container Storage driver) to manage the file system for write operations, because this layer makes it much less efficient than writing data directly to the file system of the host.

To solve the above problems, Docker provides four different ways to mount data directly from Docker hosts to containers, namely volume (volumes), bound mount (bind mounts), temporary file system (tmpfs) and named channel (named pipe). These four methods of named pipe are unique to windows systems (since most containers are deployed on linux systems in practice, we mainly introduce the first three mount methods in this article). The characteristics of volume (volumes), bound mount (bind mounts) and temporary file system (tmpfs) mount methods are as follows:

1) Volume is stored in part of the host file system managed by Docker (/ var/lib/docker/volumes/) and is managed entirely by Docker. Volume storage is the most recommended method for saving docker data.

2) bind mounts binding and mounting, which can be stored anywhere on the host system, or even important system files or directories, which can be modified at any time by non-Docker processes on Docker hosts or Docker containers

3) the tmpfs is only stored in the memory of the host system and is not written to the host's file system.

Let's take a look at how to create these three storage methods.

Volume 02 (volumes)

Volume is the only one that uses Docker to create and manage volumes among the three storage methods. The recommended usage scenarios for volumes are as follows:

1) data sharing among multiple running containers

2) when you want to store the container's data on a remote host or cloud

3) Volume is a better solution when you need to back up, restore, or migrate data from one Docker host to another Docker host

4) programs in the container need better IO performance.

Let's take a look at how to manipulate volumes using the docker command

(1) View the list of volumes

1. # View volume list 2. # docker volume ls

(2) create a volume

The specific commands for creating a volume are as follows:

1. # create volume 2. # docker volume create [Volume Name]

Let's now create a volume2 and look at the detailed parameters for creating the volume, as follows

1. # create volume 2. # docker volume create volume2 3. # 4. # View the details of volume2 5. # docker volume inspect volume2

(3) Mount the volume

The mount volume is bound when the container is created. Add a parameter after the creation of the container docker container run to mount the volume. There are two ways to mount the volume, one through-v mount and the other through-mount,-v for separate container mount, while-mount is generally used for docker service (that is, docker swarm). Here are the details of these two parameters:

1.-v:

two。 Consists of three fields separated by colons (:), [HOST-DIR:] CONTAINER-DIR [: OPTIONS]

3. 1) HOST-DIR represents the name of the directory or data volume on the host. When this section is omitted, an anonymous volume is automatically created. If you specify a directory on the host, you need to use an absolute path.

4. 2) CONTAINER-DIR represents the directory or file to be mounted into the container, that is, a directory or file in the container

5. 3) OPTIONS represents a configuration, such as set to read-only permission (ro), and this volume can only be used by the container (Z), or can be used by multiple containers (z). Multiple configuration items are separated by commas.

6.

7.-- mount:

8. Consists of multiple key-value pairs separated by commas. For example: type=volume,source=volume2,destination=/volume2,ro=true

9. 1) type, which specifies the type, can be specified as bind,volume,tmpfs

10. 2) source, specify the volume name when the type is volume and omit this field when the volume is anonymous. When the type is bind, specify the path. You can use the abbreviation src.

11. 3) destination, the path mounted to the container. You can use the abbreviation dst or target.

12. 4) ro is the configuration item, and multiple configuration items are directly separated by commas. Usually use true or false.

Next, we use the-v command to mount the volume as follows:

1. # create a container test10, mount the volume volume1 with-v, and mount the volume1 to the container file1 directory 2. Docker container run-it-- name test10-- hostname test10\ 3.-v volume1:/file1 centos / bin/bash

We can view the mapping between container test10 and volume with the following command

1. # View the detailed configuration information of the container 2. Docker container inspect test10

(4) Delete a volume

Now let's take a look at the command format for deleting volumes.

1. # delete volume 2. Docker volume rm [volume name]

We delete the volume volume2 we just created as follows

03 bind and mount (bind mounts)

Binding mount has been supported since the release of docker, and the functionality of binding mount support is relatively limited compared to volumes. Bind mount by binding a directory on the host to the container, the container can manipulate and modify the contents of the directory on the host. Binding mounts perform very well, but they depend on host file systems with a specific directory structure.

The parameters of the bind mount are the same as those of the volume, which can be mounted by-v or-mount, which is used for separate container mounts and-mount is generally used for docker service. We create a directory bind_mount_test on the local host, create a file file1 in the directory, and then bind and mount the directory to the container / home/bind_mount_test directory as follows:

1. # create a container test1 and bind the directory bind_mount_test to the container's / home/bind_mount_test directory 2. Docker container run-it-v\ 3. / root/bind_mount_test:/home/bind_mount_test-- name test1 centos / bin/bash

If the bound and mounted host directory does not exist, it will be created automatically. Now we create another file file2.txt in the host directory bind_mount_test and enter the content "kan dao ma". Let's see if we can see it in the container. At the same time, we create a file file3.txt in the container test1's bind_mount_test directory to see if it can be seen on the host. The specific operations are as follows

Through the above operations, we can find that the container directory mounted by binding is completely managed by the file system of the host, unlike the volume is managed by docker itself, so issues such as directory permissions and SELINUX need to be managed by ourselves.

04 temporary file system (tmpfs)

Volume and binding mount is mainly used to share files between the host and the container. The data can be retained after the container is stopped. The tmpfs mount is temporary and the data is kept in the host memory. When the container stops, the data will be deleted. The data of this mount mode cannot be shared between the two containers, and the mount mode only supports linux systems.

Tmpfs mount also has two command parameters, one is-tmps and the other is-mount, similar to volume and bind mount,-- tmpfs for single container mount and mount for docker service mount. Let's mount the container app directory to memory through-tmpfs, as follows:

1. # create a container tmptest and mount the app directory to memory via the tmpfs command. 2. Docker container run-it-- name tmptest-- tmpfs / app centos / bin/bash05 data volume container

In the above chapter, we learned about several storage methods of containers. What to do if we want to share some real-time updated data among containers? in fact, the solution given by docker is the user data volume container. The data volume container mounts the file directory in a volume manner, and other containers share data by mounting this container. The container in which the data volume is mounted is called the data volume container. A data volume container is a common container that specifically provides data volumes for mounting use by other containers. Now let's take a look at how to share data through the data volume container, as follows:

1) create a data volume test_volume, create a data volume container (which is actually a normal container), and mount the created volume to the data volume container

1) create a data volume test_volume, while creating a data volume container (which is actually a normal container), and mount the created volume to the data volume container 1. # create volume test_volume 2. Docker volume create test_volume 3. 4. # create a data volume container data_container Also mount the test_volume volume to the container's share_dir directory 5. Docker container run-it-v test_volume:/share_dir\ 6.-- name data_container-- hostname data_container centos / bin/bash

2) create two ordinary containers test100 and test200 inherit the data volumes mounted by the data volume container at the same time

1. # create container test100,--volumes-from command to inherit specified data volume container 2. Docker container run-it-- volumes-from data_container\ 3.-- name test100-- hostname test100 centos / bin/bash 4. 5. # create container test200 -- volumes-from command is used to inherit the specified data volume container 6. Docker container run-it-- volumes-from data_container\ 7.-- name test200-- hostname test200 centos / bin/bash

3) We operate on the file file1.txt in the container test100 and test200 respectively. Finally, we check whether the file1.txt file is updated in real time in the data volume container.

Through the above operations, we can find that the data sharing requirements between different containers can be realized through the data volume container.

06 container data backup and recovery

(1) data backup

If we want to back up the data in the data volume of the data volume container just now, we can create a backup container, which inherits the data volume of the data volume to back up the data. In fact, it is to create a backup container to mount the host file directory to the backup container by mounting binding, and then back up the inherited data volume directory to the directory mapped by binding and mounting in the backup container. Thus, the specific operations of data backup are as follows:

1. # create a backup container Then the container not only inherits the data volumes of the data volume container, but also mounts the host system backup directory 2. # backup the share_dir of the data volume mapped directory to the mount binding mapped directory backup by executing the tar command in the container. Docker container run-- volumes-from data_container\ 4.-v / root/backupfile:/backup centos tar cvf / backup/backup.tar / share_dir/

(2) data recovery

Data recovery is actually similar to backup. We create a new container and mount it to the new container by mounting and binding the backup directory of the host system. Then we can restore the data by executing the decompression command of the tar package in the container. The specific operations are as follows:

Thank you for reading, the above is the content of "Docker storage management and container data backup and recovery". After the study of this article, I believe you have a deeper understanding of Docker storage management and container data backup and recovery, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report