Container data volume of Data Volume and data management detail in Docker

  • 2020-06-07 05:38:54
  • OfStack

Volume (Volume)

A volume (Volume), known as a data mount point in a container, can bypass the federated file system to provide persistent data for Docker, which can also be Shared between the host-container or multiple containers. With volumes, we can make changes to the data take effect directly without having to rebuild the image.

1. Data volume

A data volume is a specified directory that can be Shared between one or more containers, bypassing the federated file system. Volumes provide 1 useful feature for persisting or sharing data.

The primary goal of data volume design is to provide persistent data regardless of the life cycle of the container. Therefore, when deleting a container, Docker does not automatically delete the volume until no container is referenced.

1.1 Add data volumes

Can be found in docker create and docker create When the command creates a container, add a data volume to the container with the -ES21en parameter. The -ES22en parameter parameter can be used multiple times to add multiple data volumes.

For example, you can add a volume to the container as follows:


$ sudo docker run -t -i -v /home/test --name test itbilu/test /bin/bash

This creates a volume at the /webapp location within the container.

In addition to adding volumes when creating containers, you can also add them via the Dockerfile directive in the Dockerfile file, and Volume can be used multiple times to add multiple volumes.

Note: The sample image used in this article (itbilu/test) was created from the following Dockerfile file:


# Version: 0.0.3
FROM ubuntu:16.04
MAINTAINER  ask 3 "cn.liuht@gmail.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hello World,  I'm a container ' \ 
 > /var/www/html/index.html
RUN mkdir /home/itbilu/
ENV ITBILU_PATH /home/itbilu/
VOLUME [$ITBILU_PATH]
EXPOSE 80

1.2 volume location

After adding the volume, you can pass docker inspect To see where the data volume is in the container:


$ sudo docker inspect test

docker inspect Can be used to view detailed configuration information for containers or mirrors. You can view the volume information of the container under the Mounts node of the container:


...
"Mounts": [{
 "Type": "volume",
 "Name": "5f869c580c06e6079b0de2c5ce682c1c9467286c76b506703d87bf11d1271c24",
 "Source": "/var/lib/docker/volumes/5f869c580c06e6079b0de2c5ce682c1c9467286c76b506703d87bf11d1271c24/_data",
 "Destination": "/home/test",
 "Driver": "local",
 "Mode": "",
 "RW": true,
 "Propagation": ""
},
{
 "Type": "volume",
 "Name": "e4fd6c3a91ba2e03b14cf174c2023f366abbe9f2f73ca07e6bac223f68e47773",
 "Source": "/var/lib/docker/volumes/e4fd6c3a91ba2e03b14cf174c2023f366abbe9f2f73ca07e6bac223f68e47773/_data",
 "Destination": "[/home/itbilu/]",
 "Driver": "local",
 "Mode": "",
 "RW": true,
 "Propagation": ""
}],
...

In the above example, there are two mount volumes, one at docker run The container is created when it is created, and the other one is created by the VOLUME directive in the Dockerfile file that is created to run the container image. Where Source represents the host source file location, Destination represents the mount location of the data volume in the container, and RW represents whether the volume is read/write.

1.3 Mount local data to the container data volume

In the previous example, we ran the container without specifying the local directory to mount the data volume into, where Docker USES a default data directory. -v In addition to creating a data volume in the container, parameters can mount a directory from the host to a data volume in the container.

For example, run the container and mount the ~/code/itbilu directory local to the container's /home/itbilu data volume:


$ sudo docker run -t -i -v ~/code/itbilu:/home/itbilu --name test itbilu/test /bin/bas

Note: When mounting a local directory to a mount directory in a container, if data already exists in the data volume within the container, the local content overlaps with the data in the data volume without deleting the data.

Where the container directory must use an absolute path, and the local directory can use an absolute path or some other form.

Mount Shared storage

In addition to the ability to mount local directories to container volumes, some Docker volume plug-ins allow you to mount Shared volumes such as iSCSI, NFS, FC. The advantage of using Shared volumes is that they are host-independent, which means that volumes can be started on any container as long as you have access to the Shared storage and the plug-in is installed.

Detailed reference:

Mount a shared-storage volume as a data volume

Mount the local file to the container data volume

-v Parameters can mount not only directories, but also individual files. Such as:


$ sudo docker run -t -i -v ~/.bash_history:/root/.bash_history \
--name test itbilu/test /bin/bash

This will mount the local ~/.bash_ES100en file into the new container so that you can access the bash history on the host from within the container.

2. Data volume container

If you have some persistent data to share between containers, or want to use in a non-persistent container, it is best to create a named volume container and then mount the data from it.

Next, we create a new named Shared container. Instead of running one application, this container USES the training/postgres image to create a Shared layer among all containers to save disk space.


$ sudo docker create -v /dbdata --name dbstore training/postgres /bin/true

Note: training/postgres is a mirror provided in the official Docker document. This article USES it directly.

Use the data volume container

After creating the data volume container, we can pass through --volumes-from Option to mount a data container to another container:


$ sudo docker run -d --volumes-from dbstore --name db1 training/postgres

It can also be Shared across multiple containers. For example, mount to another container:


$ sudo docker run -d --volumes-from dbstore --name db2 training/postgres

At this point, if the named /dbdata directory in the training/postgres image is mounted from the dbstore container, files under /dbdata in the training/postgres image are hidden. Only the files in the dbstore container are ultimately visible.

You can also extend the mount chain to mount a volume from an existing dbstore container such as db1, db2:


$ sudo docker run -d --name db3 --volumes-from db1 training/postgres

In this case, if you remove the container of the mounted volume, either the original dbstore container, or the subsequent db1 or db2 containers, the volume will not be removed. To remove a volume from a hard disk, you must use it docker rm -v The command deletes the last container that references the volume.

3. Backup, restore and migrate data volumes

In addition to the above operations, common data volume operations are data volume backup, restore, merge operations. Here are 1 common operations:

3.1 Backup data volumes

When we described the data volume container earlier, we created a container named dbdata and a /dbdata data volume in the container. Next, you can use it in the create container --volumes-from Parameter to mount the data volume and back up the data:


# Version: 0.0.3
FROM ubuntu:16.04
MAINTAINER  ask 3 "cn.liuht@gmail.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hello World,  I'm a container ' \ 
 > /var/www/html/index.html
RUN mkdir /home/itbilu/
ENV ITBILU_PATH /home/itbilu/
VOLUME [$ITBILU_PATH]
EXPOSE 80
0

In this operation, we create a container through the ubuntu image, which is passed when creating the container --volumes-from Parameter shares the data in the data volume container and mounts the current directory ($(pwd) to the data volume. After the container runs, the data volumes are backed up using the tar command.

At the end of the command execution, the container stops and the data backed up can be found in the local directory of the current operation.

3.2 Backup data volumes

After the data backup, you can restore the backup data to the data volume in the container while creating the container, thus achieving the data migration.

First, create and run the container and add 1 data volume:


# Version: 0.0.3
FROM ubuntu:16.04
MAINTAINER  ask 3 "cn.liuht@gmail.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hello World,  I'm a container ' \ 
 > /var/www/html/index.html
RUN mkdir /home/itbilu/
ENV ITBILU_PATH /home/itbilu/
VOLUME [$ITBILU_PATH]
EXPOSE 80
1

Then through tar Command to restore backup data:


# Version: 0.0.3
FROM ubuntu:16.04
MAINTAINER  ask 3 "cn.liuht@gmail.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hello World,  I'm a container ' \ 
 > /var/www/html/index.html
RUN mkdir /home/itbilu/
ENV ITBILU_PATH /home/itbilu/
VOLUME [$ITBILU_PATH]
EXPOSE 80
2

In this way, the data is restored to the /dbdata directory of the container dbdata2, where it can be manipulated and used.

conclusion


Related articles: