[DevOps Series] Detailed explanation of Docker data volume (volume)

[DevOps Series] Detailed explanation of Docker data volume (volume)

I. Overview

Docker's image is formed by superimposing multiple read-only file systems. When starting a container, docker will load the read-only layer and add a read-write layer on top of the read-only layer (top of the stack). If you need to modify a file in the read-only layer, then this file layer needs to be copied to the read-write layer. The read-only version of the file is still there, just hidden by the write-level copy of the file above. When docker is removed or restarted, the previous changed files disappear. In Docker, the combination of read-only layer and read-write layer is called Union File System. Docker packages the application and running environment to form a container for running. If the data generated by the Docker container is not generated through docker commits to generate a new image, so that the data is saved as part of the image, then when the container is deleted, the data will naturally be gone. In order to save data in Docker, Docker designed a mechanism called Volume

In order to achieve good data storage and data sharing, Docker proposed the concept of Volume, which bypasses the default joint file system and exists on the host in the form of a normal file or directory. This directory or file is called a data volume . .

2. Data volume

Insert image description here

A volume is a special directory that can be used by one or more containers. It bypasses UFS and can provide the following features:

  • Data volumes allow data to be shared or reused between containers.
  • Changes in the volume take effect directly.
  • Changes in the data volume are not included in the mirror update.
  • The life cycle of a data volume lasts until no more containers use it.

3. Why use data volume volume?

A volume is a selected directory within one or more containers that provides Docker with persistent or shared data. It is Docker's preferred mechanism for storing data generated and used by containers. Modifications to volumes take effect immediately; when committing or creating an image, the volume is not included in the image.

The role of data volumes:

  1. Persistent data
  2. Share data

Characteristics of data volumes:

  1. Even if it takes effect
  2. Volume updates do not affect mirroring
  3. Volumes persist by default even if containers are stopped or deleted

4. Basic operations of data volumes

4.1 Create data volume

Use the following command to create a data volume

  • docker volume create volume name
docker volume create db_vol

Data volumes created in this way can also be managed by docker volume, such as viewing, deleting, etc.

4.2 View data volume

docker volume ls

4.3 View data volume details

  • docker volume inspect volume name
docker volume inspect db_vol

4.4 Data volume deletion

  • docker volume rm volume name
docker volume rm db_vol

5. Use of data volumes

5.1 Create the data volume first and then mount it

docker volume create data_volume

5.2 Create a data volume used by the container

docker run -d -it \
> --name volumetest \
> --mount source=data-vol,target=/data \
> ubuntu

Note: Adding "\" means that the last carriage return and line feed are added to the comment. The system understands that the command has not ended yet, so it continues to wait for the user to input until the end character is read.

Abbreviation (-v):

docker run -d -it \
> --name volumetest \
> -v data-vol:/data \
> ubuntu

5.3 Directly mount the host directory

In this way, there is no need to create a data volume in advance. You can directly specify a directory on the host to mount it into the container. However, the corresponding directory on the host must exist, otherwise an exception will be reported.

docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /root/vdata.
1). The command to create a container in mount mode is as follows:
docker run -d -it \
> --name volumetest02 \
> --mount type=bind,source=/home/lisen/vdata,target=/vdata \
> ubuntu
2). The -v mode creation command is as follows:
docker run -dit --name volumetest02 -v /home/lisen/vdata:/vdata ubuntu

5.4 Read-only data volumes

The created data volume is read-writeable by default, which is suitable for most situations. The volume can also be set to read-only.

1). The command to create a container in mount mode is as follows:
docker run -d -it \
> --name volumetest02 \
> --mount type=bind,source=/home/lisen/vdata,target=/vdata,ro \
> ubuntu
2). Create in -v mode (can be understood as abbreviation):
docker run -dit --name volumetest02 -v /home/lisen/vdata:/vdata:ro ubuntu

The main function of a data volume is data persistence and data sharing, so read-only mode is generally not used.

5.5 Data volume container

Purpose: The main purpose of the data volume container is to share some continuously updated data between multiple containers. The data volume container is also a container that specifically provides data volumes for other containers to mount.

1). Create a new data volume container
docker run -it -d --name data-volume-con -v /data ubuntu
2). Create a new container to use the data volume container
docker run -it -d --name db-con-1 --volumes-from data-volume-con ubuntu

The parameter –volumes-from is used to specify the data volume container; enter the newly created container and create a new test file in the mounted directory (data directory, the directory specified when creating the data volume container).

5.6 Data volume container

1). Data volume backup

Create a container that mounts both the data volume to be backed up (specified with the volumes-from parameter) and the data volume used to back up the data (specified with the -v parameter). Use the tar compression command to compress The data volume to be backed up specified by the volumes-from parameter is compressed into the data volume used for backup (equivalent to saving to the corresponding directory of the host).

docker run --rm \
> --name backup \
> --volumes-from data-volume-con \
> -v /host-backup:/con-backup \
> ubuntu tar cvf /con-backup/backup200201.tar /data
  • The –rm parameter specifies that the created container is a temporary container and will be automatically deleted after running. We only use this container to complete the backup. After the backup is completed, the data is stored in the host, and the container is naturally no longer needed.
  • –volumes-from parameter specifies the data volume container that needs to be backed up
  • -v parameter specifies the data volume used to back up data. /host-backup is the host directory and /con-backup is the corresponding container directory.
  • The tar command completes data compression. Note that the source of compression is the container directory, because the compression command is actually executed in the container, to be precise, it is executed in the backup container (specified by the –name parameter). After the compression is completed, it will naturally be saved to Host directory.
2). Data restoration

Create a new data volume container, which serves as a container for storing restored data.

docker run -it --name data-volume-con2 -v /data ubuntu

Create a temporary container, mount both the data volume container used to store the restored data and the data volume storing the backup data, and then use the tar command to decompress the backed up data into the data volume storing the restored data.

  • –rm parameter, see explanation in “Data Backup” section
  • –volumes-from parameter, specify the container used to save the restored data
  • The -v parameter is used to specify the data volume where backup data is stored. /host-backup is the directory used to store backup data on the host. Mount it to the /container-back directory of the container. The tar command passes /container- back

Guess you like

Origin blog.csdn.net/songjianlong/article/details/132910493