09-Docker-Volumes数据管理

09-Docker-Volumes数据管理

Docker Version: 19.03.5

😄 Written by Zak Zhu

参考

数据卷类型

types-of-volumes

  • bind

    • 挂载宿主机上指定文件或目录到容器中目标路径
    • 容器中目标路径不存在, 能够自动创建
    • 独立于容器的读写层和生命周期, 实现数据共享和持久化
    • 非常好的性能, 但依赖于特定目录结构的宿主机文件系统
    • 适用于生产环境

  • volume

    • 由Docker创建和管理, 不需要指定宿主机上的源路径, 只需要指定容器中目标路径.
    • 宿主机上的源路径, Docker会在/var/lib/docker/volumes/路径下自动创建
    • 容器中目标路径不存在, 能够自动创建
    • 独立于容器的读写层和生命周期, 实现数据共享和持久化
    • 适用于开发环境

  • tmpfs

    • 挂载宿主机内存到容器中目标路径
    • 容器中目标路径不存在, 能够自动创建
    • 容器停止, 挂载被删除, 文件数据丢失
    • 非持久化, 且不能共享数据于容器间
    • 适用于非持久数据或敏感信息

数据卷操作

--mount '<key>=<value>,<key>=<value>,...'
KEY DESCRIPTION OF VALUE
type The type of the mount, which can be bind, volume, tmpfs.
source ---> For bind mounts, this is the path to the file or directory on the Docker host.

---> For named volumes, this is the name of the volume.
---> For anonymous volumes, this field is omitted.
target The path where the file or directory is mounted in the container.
readonly If present, causes the bind mount or named volume to be mounted into the container as read-only.
tmpfs-size Size of the tmpfs mount in bytes.
Unlimited by default.
tmpfs-mode File mode of the tmpfs in octal. For instance, 700 or 0700.
Defaults to 1777 or world-writable.


bind数据卷

  • 启动容器A, 同时挂载一个bind数据卷

    docker container run \
    	--detach --name 'container_A' \
    	--mount 'type=bind,source=/tmp/data/,target=/app_data/'
    	busybox sleep 3600
    
  • 启动容器B, 同时挂载一个只读的bind数据卷

    docker container run \
    	--detach --name 'container_B' \
    	--mount 'type=bind,source=/tmp/data/,target=/app_data/,readonly' \
    	busybox sleep 3600
    
  • 查看容器B上数据卷挂载信息

    docker container inspect container_B
    

    1


volume数据卷

  • 启动容器C, 同时挂载一个只读的named volume数据卷

    docker container run \
    	--detach --name 'container_C' \
    	--mount 'type=volume,source=devdb,target=/data/,readonly' \
    	busybox sleep 3600
    
  • 查看容器C上数据卷挂载信息

    docker volume inspect devdb
    

    2

  • 启动容器D, 同时挂载一个annoymous volume数据卷

    docker container run \
    	--detach --name 'container_D' \
    	--mount 'type=volume,target=/data/' \
    	busybox sleep 3600
    
  • 查看容器D上数据卷挂载信息

    docker container inspect -f '{{.Mounts}}' container_D
    

    3

  • 列举所有volume数据卷

    docker volume ls
    

    4

  • 删除容器C以及挂载的named volume数据卷

    # Step1: stop container_C
    docker container stop container_C
    
    # Step2: remove container_C
    docker container rm container_C
    
    # Step3: remove the named volume 
    docker volume rm devdb
    
  • 删除容器D以及挂载的annoymous volume数据卷

    # Step1: stop container_D
    docker container stop container_D
    
    # Step2: remove container_D and the annoymous volume
    docker container rm --volumes container_D
    

tmpfs数据卷

  • 启动容器E, 同时挂载一个tmpfs数据卷

    docker container run \
    	--detach --name 'container_E' \
    	--mount 'type=tmpfs,target=/session/,tmpfs-size=128M' \
    	busybox sleep 3600
    
  • 查看容器E上数据卷挂载信息

    docker container inspect container_E
    

    5

posted @ 2020-02-19 22:00  ZakZhu  阅读(301)  评论(0编辑  收藏  举报