Data Volumes of docker

aims

1. File copy between Docker host and container
2. Data volume
3. Data volume container

File copy between Docker host and container docker copy

Preface:
Docker data management
When using Docker in a production environment, it is often necessary to persist data, or to
share data among multiple containers , which inevitably involves container data management operations

There are two main ways to manage data in a container:
Data Volumes, where the data in the container is directly mapped to the local host environment;
Data Volume Containers (Data Volume Containers use specific containers to maintain data volumes).

Of course, there is the most primitive copy method, which is also a method of managing data, but it is basically not used;

The most primitive copy method to manage data :
host file copy to
the file or directory that needs to be copied in the container docker cp Container name: container directory
Example:
docker cp /zking/monitorlogs/ db3:/xieminglu/software

Copy to host
docker cp in the container Container name: Container directory Host directory
Example:
docker cp db3:/xieminglu/software/test.txt /zking

Data volume

Data Volumes
is a special directory that can be used by one or more containers. It directly maps the host operating system directory into the container.
It can provide many useful features:
1. Data volumes can be between containers Sharing and reuse
2. Modifications to the data volume will take effect immediately
3. Updates to the data volume will not affect the mirroring
4. The data volume will always exist by default, even if the container is deleted

Data volume related operations
1. Create data volume
docker volume create my-vol

 此时,数据卷默认会放到/var/lib/docker/volumes路径下,会发现所新建的数据卷位置,查看命令如下:
 ## 参数可以为数字“1”,字母L:大小写均可,但效果不一样 
 ls -1 /var/lib/docker/volumes

2. View all data volumes
docker volume ls

3. View the detailed information of the specified data volume (display a data in JSON format)
docker volume inspect my-vol

4. Delete a volume
docker volume rm my-vol

 注1:数据卷 是被设计用来持久化数据的,它的生命周期独立于容器,Docker不会在容器被删除后自动删除数据卷,
      并且也不存在垃圾回收这样的机制来处理没有任何容器引用的数据卷,无主的数据卷可能会占据很多空间,
      所以要及时删除

Mount the data volume, it is best to create the start container through run instead of create/start. After the
create/start command creates the start container, it is quite troublesome to mount the data volume. Many configuration files need to be modified, but it is not impossible.
5. Start a mount Load data volume container
## demo1
docker run -d
-it --name
mycentos03
--mount source=my-vol,target=/webapp
centos:7

 注意:此行命令执行后的效果是,宿主机路径/var/lib/docker/volumes/my-vol/_data与mycentos03容器路径/webapp完成映射


 ## demo2
 docker run -d \
   -it \
   --name mycentos04\
   --mount type=bind,source=/xieminglu/data,target=/root/webapp02 \
   centos:7    

 注1:linux命令结尾加斜杠有什么用
      加了“\”意为将最后的回车换行给注释了,系统理解为命令还没有结束,因而是继续等待用户进行输入,直到读到结束符,如回车

 注2:source=my-vol,target=/webapp
      my-vol为要挂载的数据卷,如果数据卷不存在,docker会自动创建
      /webapp为容器上目录,如果目录不存在, Docke会自动创建

 注3:mount选项高级用法
      --mount选项的type参数支持三种类型的数据卷
      --mount标志:由多个名值对组成,逗号分隔,每个键值由 <key> = <value> 元组组成 
      1.type=volume普通数据卷(默认即这种类型),映射到主机/var/lib/docker/volumes路径下;
        --mount type=volume,source=my-vol,target=/webapp
        注:这是type的默认值
      2.bind:绑定数据卷,映射到主机指定路径下;
        --mount type=bind,source=/webapp,destination=/webapp2
      3.tmpfs :临时数据卷,只存在于内存中
        docker run -d \
          -it \
          --name tmptest \
          --mount type=tmpfs,destination=/app \
          nginx:latest

Data volume container

Data volume container
If users need to share some continuously updated data among multiple containers, the easiest way is to use a data volume container.
The data volume container is also a container, but its purpose is to specifically provide data volumes for other containers to mount

Data volume container related operations
1. Create a new data volume container
docker run -di --name db_data -v /db_data centos:7
Note: The real storage path of the shared data following -v

2. Use the container db1 and db2 to test whether the data volume container is available
docker run -di --name db1 --volumes-from db_data centos:7
docker exec -it db1 bash
cd db_data

docker run -di --name db2 --volumes-from db_data centos:7
docker exec -it db2 bash
cd db_data

Just create an aaa.txt file to view the effect

Effect: In the above example, db1 and db2 share data through db_data
Insert picture description here

Guess you like

Origin blog.csdn.net/xieminglu/article/details/103552295