zoukankan      html  css  js  c++  java
  • Docker 数据管理-Volumes

    Volumes是Docker最为推荐的数据持久化方法。

    Volumes have several advantages over bind mounts:

    • Volumes are easier to back up or migrate than bind mounts.
    • You can manage volumes using Docker CLI commands or the Docker API.
    • Volumes work on both Linux and Windows containers.
    • Volumes can be more safely shared among multiple containers.
    • Volume drivers allow you to store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
    • A new volume’s contents can be pre-populated by a container.

    Choose the -v or --mount flag

      起初, the -v or --volume flag was used for 单机版-Containers and the --mount flag was used for Swarm services. However, starting with Docker 17.06, you can also use --mount with 单机版-Containers. In general, --mount 更清晰 and 更冗长. 最大的不同是 -v 语法将所有的选项组合在一起,--mount语法将他们进行了分离。 

    Tip: 新永不应该使用--mount语法,有经验的用户更熟悉-v or --volume语法。 但是更鼓励使用--mount,因为调查发现它更容易被使用。

    If you need to specify volume driver options, you must use --mount.

    • --mount: 由多个<key>=<value>对组成,使用逗号进行分割。The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.
      • The type of the mount, which can be bind, volume, or tmpfs. This topic discusses volumes, so the type is always volume.
      • The source of the mount. For named volumes, this is the name of the volume. For anonymous volumes, this field is omitted. May be specified as source or src.
      • The destination takes as its value the path where the file or directory is mounted in the container. May be specified as destination, dst, or target.
      • The readonly option, if present, causes the bind mount to be mounted into the container as read-only.
      • The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.

    Create and manage volumes

    Unlike a bind mount, you can create and manage volumes outside the scope of any container.

    Create a volume:

    $ docker volume create my-vol
    

    List volumes:

    $ docker volume ls
    
    local               my-vol
    

    Inspect a volume:

    $ docker volume inspect my-vol
    [
        {
            "Driver": "local",
            "Labels": {},
            "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
            "Name": "my-vol",
            "Options": {},
            "Scope": "local"
        }
    ]
    

    Remove a volume:

    $ docker volume rm my-vol
    

    Start a container with a volume

    If you start a container with a volume that does not yet exist, Docker creates the volume for you. The following example mounts the volume myvol2 into /app/ in the container.

    The -v and --mount examples below produce the same result. You can’t run them both unless you remove the devtest container and the myvol2 volume after running the first one.

    $ docker run -d 
      --name devtest 
      --mount source=myvol2,target=/app 
      nginx:latest

    $ docker run -d 
      --name devtest 
      -v myvol2:/app 
      nginx:latest

    • 可以为多个Container mount同一个volume,以达到数据在多个Container之间共享的目的;
    • 如果 mount target指向的是已有目录,原有数据会被复制到source volume 中。

    参考文档: https://docs.docker.com/

     
  • 相关阅读:
    P1007 独木桥
    P1789 【Mc生存】插火把
    P2658 汽车拉力比赛
    1959 拔河比赛
    P1936 水晶灯火灵
    websocket
    瀑布流布局
    Router
    图片占位
    单位
  • 原文地址:https://www.cnblogs.com/vincenshen/p/8605565.html
Copyright © 2011-2022 走看看