docker data volume (Data Volumes)

 

Docker copy file copy between the host and the container Docker

Preface:

Docker Data Management

   Docker use in a production environment, often need to be persistent data, or is required between a plurality of containers

   Data sharing, data management operations which necessarily involve container

   Management data container mainly in two ways:

   Data Volumes data volumes within the container data directly mapped to the local host environment;

   Data volume container (Data Volume Containers used to maintain a specific data volume container

 

Of course, the most primitive copy mode, this is the way to manage data, but the basic will not be used;

The most original copy manage data:

Host file copy to the container

docker cp need to copy the file or directory name container: container directory

 Example:

docker cp /liuwenwu/docker/ mycentos02:/liuww/softwar

copy to the host vessel

docker cp container name: container directory host directory

 Example:

docker cp mycentos02:liuww/softwar/docker/bbb.txt /liuwenwu/docker

 

Data volumes

Data volume (Data Volumes)

   Data volume is a special directory for the one or more containers, it will host operating system directory directly mapped into the container,

   It can provide a lot of useful features:

   1. The data volume can be shared and reused between the vessel

   2. The data volume changes will take effect immediately

   3. Update on the volume of data, will not affect the image

   4. Data volume default will always exist, even if the container is deleted

 

   Data volume related operations

   1. Create a data volume

docker volume create my-vol

 At this time, data volume into the default / var / lib / docker / volumes path, will find the new location of the data volume, the view command as follows:

     ## parameters can be the number "1", the letter L: uppercase or lowercase, but the effect is not the same

ls -1 /var/lib/docker/volumes

 2. Check all the data volumes

docker volume ls

3. See details specified data volume (JSON format display data)

docker volume inspect my-vol 

4. Delete a volume

 docker volume rm my-vol

 Note 1: The data volume is designed to persistent data, which is independent of the container life cycle, the data volume Docker not automatically deleted after the container is removed,

          And there is no garbage collection mechanism to deal with such a container without any reference to data volumes, data volumes without the Lord might occupy a lot of space,

          So to promptly remove

   ## mounted data volumes, it is best to create a startup run by container rather than create / start

   After ## create / start command to create a boot container, and then mount the data volume rather cumbersome, a lot of you want to modify the configuration file, but not not

5. Start a mount data volume container

 ## demo1
     docker run -d \
       -it \
       --name mycentos03\
       --mount source=my-vol,target=/webapp \
       centos:7

 

Note: the effect of execution of this command line, the host path / var / lib / docker / volumes / my-vol / _data tomcat01 container path and / webapp the mapping

   

 ## demo2
     docker run -d \
       -it \
       --name tomcat02 \
       --mount type=bind,source=/liuww/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

数据卷容器

数据卷容器

   如果用户需要在多个容器之间共享一些持续更新的数据,最简单的方式是使用数据卷容器。

   数据卷容器也是一个容器,但是它的目的是专门提供数据卷给其他容器挂载

 

   数据卷容器相关操作

   1.新建数据卷容器

docker run -di --name db_data -v /db_data centos:7

注:-v 后面接的共享数据真实存放路径

 2.用容器db1、db2测试数据卷容器是否可用

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

 随便创建一个aaa.txt文件就可以查看效果了

 效果:上面例子中db1、db2通过db_data来共享了数据

 

Guess you like

Origin www.cnblogs.com/liuwenwu9527/p/11963560.html