docker data volume

The role of data volumes

Docker data may be stored in a virtual machine disk like media, referred to as data volume (Data Volume) in the Docker. Volume data can be used to store data Docker applications, it can also be used to share data between Docker containers. Volume data is presented to form Docker container is a directory that supports shared among multiple vessels, modifications will not affect the image. Use Docker data volume, similar to a file system using the mount to mount the system.

Volume data management foundation

docker provides two volumes: the bind mount, docker Managed Volume
the bind mount
* is the bind mount will mount a directory or file on the host to the container, using an intuitive and efficient, easy to understand, use -v option to specify the path.
Here Insert Picture Description
bind mount default permissions to read and write rw, ro read-only can be specified at mount time, -v option to specify the path, if not present, will be automatically created when mounted.
docker managed volume

[root@server1 docker]# docker volume ls

Here Insert Picture Description
View existing data volumes.

[root@server1 docker]# docker volume prune 

Here Insert Picture Description
Clear all data volumes.
Here Insert Picture DescriptionThe following experiments need to use the registry mirror.

[root@server1 docker]# docker run -d --name registry1 registry:2.3.1 

Here Insert Picture Description
View data volume again, generate a data volume.

[root@server1 docker]# docker inspect registry1

Here Insert Picture Description
source directory is the volume of the host, is a docker container directory automatically generated, if the existing directory pointed to mount, the original data is copied to the volume.
Note that, even if the container is removed, the data volume will not disappear.
Here Insert Picture Description
Through the above we find that the resulting directory is too long, we can customize when generating container.

[root@server1 docker]# docker run -d --name registry2 -v registry2:/var/lib/registry registry:2.3.1

Here Insert Picture Description

[root@server1 volumes]# docker inspect registry2

Here Insert Picture Description
Generating a container, designated directory / var / lib / docker / volumes / registry2, only you need to directly input the name of the directory will be generated in / var / lib / docker under / volumes directory.

[root@server1 volumes]# docker volume create vol1

Creating a data volume named vol1.

[root@server1 volumes]# docker run -d --name registry3 -v vol1:/var/lib/registry registry:2.3.1

Create a container and the container mount data volume specified directory.

[root@server1 volumes]# docker inspect registry3

Here Insert Picture Description
You can view the path to the source container.

[root@server1 volumes]# docker volume create vol2

Here Insert Picture Description

[root@server1 volumes]# docker run -it --name vm2 -v vol2:/data:ro ubuntu

Here Insert Picture Description
新建数据卷,创建容器时挂载数据卷可以指定目录读写权限。
bind mount与docker managed volume的区别

bind mount docker managed volume
volume位置 可任意指定 /var/lib/docker/volumes/…
对已有mount point的影响 隐藏并替换为volume 原有数据复制到volume
是否支持单个文件 支持 不支持,只能是目录
权限控制 可设置只读,默认为读写 无控制 ,均为读写
移植性 移植性若 移植性强

convoy卷插件实现nfs

首先需要在server1和server2两台主机搭建nfs文件系统。

[root@server1 yum.repos.d]# yum install -y nfs-utils.x86_64
[root@server1 yum.repos.d]# systemctl start rpcbind
[root@server1 yum.repos.d]# systemctl start nfs
[root@server1 yum.repos.d]# mkdir /mnt/nfs

下载服务,开启服务,创建需要共享的目录。

[root@server1 mfs]# vim /etc/exports
/mnt/nfs        *(rw,no_root_squash)

指定共享的目录和权限。

[root@server1 mnt]# showmount -e localhost

Here Insert Picture Description
发现目录。
在server2同样要下载服务,创建目录,只是不用编辑/etc/exports文件,因为server2不需要将目录共享,只是要挂载serve1共享的目录。

[root@server2 mnt]# showmount -e 172.25.26.1

Here Insert Picture Description

[root@server2 mnt]# mount 172.25.26.1:/mnt/nfs/ /mnt/nfs

Here Insert Picture Description
挂载目录。
下载convoy包。

[root@server1 ~]# tar zxf convoy.tar.gz 
[root@server1 ~]# ls
convoy  convoy.tar.gz  docker  dockerun  images
[root@server1 ~]# cd convoy/
[root@server1 convoy]# ls
convoy  convoy-pdata_tools  SHA1SUMS
[root@server1 convoy]# cp convoy convoy-pdata_tools /usr/local/bin/

下载包后将两个文件复制到/usr/local/bin目录下,方便使用。

[root@server1 convoy]# mkdir /etc/docker/plugins

创建目录。

[root@server1 plugins]# echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec
[root@server1 plugins]# convoy daemon --drivers vfs --driver-opts vfs.path=/mnt/nfs &

Here Insert Picture Description
创建卷

[root@server1 nfs]# convoy create vol1

Here Insert Picture Description
Here Insert Picture Description
可以看到生成vol1目录。

[root@server1 ~]# ls
convoy  convoy.tar.gz  docker  images
[root@server1 ~]# scp -r convoy  server2:

将解压出来的convoy目录给srever2也拷一份,接下来在server2进行操作。

[root@server2 convoy]# cp convoy convoy-pdata_tools /usr/local/bin/
[root@server2 convoy]#  mkdir -p /etc/docker/plugins/
[root@server2 convoy]# echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec
[root@server2 convoy]# convoy daemon --drivers vfs --driver-opts vfs.path=/mnt/nfs &

和在server1的操作一致。

[root@server2 convoy]# convoy list

Here Insert Picture Description
查看信息。
使用卷

[root@server1 images]# docker run -it --name vm1 
\ -v vol1:/data --volume-driver=convoy ubuntu

创建容器,将vil1挂载到容器的data目录中。

Here Insert Picture Description
在容器内的data目录创建文件,在server1中的共享目录/mnt/nfs/vol1可以同步到所创建的文件。
因为使用的是nfs文件系统,这些文件也同步到了server2的目录中。
Here Insert Picture Description

[root@server2 ~]# docker run -it --name vm2 -v vol1:/data \
 --volume-driver=convoy ubuntu

Create a container vm2 in server2 in.
Here Insert Picture Description
In the data directory container server2 you can see the files created.
Here Insert Picture Description
Delete some files.
Here Insert Picture DescriptionOn server1 you can see the file data is synchronized.

Guess you like

Origin blog.csdn.net/qq_41961805/article/details/90672957