zoukankan      html  css  js  c++  java
  • 2. volume解惑

    Volume解惑

    docker是一个容器,在容器中执行的所有操作都会在容器exit后丢失,为了保证数据的持久化,docker提出了volume的概念,目的是将容器中的数据持久化到宿主机中。
    docker可以使用两种方法来创建volume,一种是创建容器时使用-v参数,另一种是创建镜像的时候在Dockerfile文件中设置volume。

    下面来看看这两种方式的区别:

    1. 在Dockerfile创建Volume

    Dockerfile

    FROM daocloud.io/centos:latest
    //这里指定要在容器中挂载test1和test2目录,这里有一点需要注意一定要使用双引号
    VOLUME ["/test1","/test2"]
    CMD /bin/bash
    

    编译Dockerfile

    mkdir test
    mv Dockerfile test/
    cd test
    docker build . -t test
    

    从上一步生成的镜像中创建一个容器:

    //这里创建一个名为test1的容器
    docker run -it --name test-container test
    

    查看容器中生成的test1和test2目录与物理磁盘的对应关系

    docker inspect test-container
    
    //运行结果如下:其中Source指向的就是宿主机的目录
    ....
    "Mounts": [
        {
            "Type": "volume",
            "Name": "01c6e11009f3a1382dc71715a2ecc810e6854a817fbaf47887ae361e1444135c",
            "Source": "/var/lib/docker/volumes/01c6e11009f3a1382dc71715a2ecc810e6854a817fbaf47887ae361e1444135c/_data",
            "Destination": "/test1",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        },
        {
            "Type": "volume",
            "Name": "3f86f47f6dac9a3bf1a0967aee3f424db27c4dd5d48bd746b7692592085f0f25",
            "Source": "/var/lib/docker/volumes/3f86f47f6dac9a3bf1a0967aee3f424db27c4dd5d48bd746b7692592085f0f25/_data",
            "Destination": "/test2",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],
    .....
    

    2. 使用-v参数来创建volume

    Dockerfile

    FROM daocloud.io/centos:latest
    CMD /bin/bash
    

    编译Dockerfile

    mkdir test
    mv Dockerfile test/
    cd test
    docker build . -t test-new
    

    从上一步生成的镜像创建容器:

    docker run -it -v /test1 -v /test2 --name test-container-new test-new
    

    查看容器中生成的test1和test2目录与物理磁盘的对应关系

    docker inspect test-container-new
    
    //运行结果如下:其中Source指向的就是宿主机的目录
    ....
    "Mounts": [
        {
            "Type": "volume",
            "Name": "2c8d289d74801a10f2ec283e83412541009d9fb7d40aec161e8c363b17d6e822",
            "Source": "/var/lib/docker/volumes/2c8d289d74801a10f2ec283e83412541009d9fb7d40aec161e8c363b17d6e822/_data",
            "Destination": "/test1",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        },
        {
            "Type": "volume",
            "Name": "a0905219edf3ad76be39065598f5443d9230f72a8e2e650d98c4f982b87d952d",
            "Source": "/var/lib/docker/volumes/a0905219edf3ad76be39065598f5443d9230f72a8e2e650d98c4f982b87d952d/_data",
            "Destination": "/test2",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],
    .....
    

    通过两种方式发现虽然都在宿主机上生成了持久化目录,但是宿主机的目录都是随机的

    -v参数高级用法,在宿主机生成自定义持久目录

    从上一步生成的镜像创建容器:

    docker run -it -v /test1:/test1 -v /test2:/test2 --name test-container-new-2 test-new
    

    查看容器中生成的test1和test2目录与物理磁盘的对应关系

    docker inspect  test-container-new-2 
    
    //运行结果如下:其中Source指向的就是宿主机的目录
    ....
    "Mounts": [
        {
            "Type": "bind",
            "Source": "/test1",
            "Destination": "/test1",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        },
        {
            "Type": "bind",
            "Source": "/test2",
            "Destination": "/test2",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],
    .....
    

    两种创建volume方法的总结

    创建Volume的两种方式 随机目录持久化 指定目录持久化
    Dokerfile VOLUME 可以实现 不支持
    -v参数 可以实现 支持
  • 相关阅读:
    js简单地发送一个请求
    浏览器缓存知识归纳
    文本选择问题: css & js
    闭包和重写函数 返回IE浏览器版本号
    新项目启动 考虑问题
    Angular 单元格合并
    pointer-events 使用场景
    移动开发 新建空白页面
    CSS Tip
    垂直居中方法
  • 原文地址:https://www.cnblogs.com/guodf/p/6748928.html
Copyright © 2011-2022 走看看