美文网首页Dockerfile_volume
2018-05-02 深入理解Docker Volume

2018-05-02 深入理解Docker Volume

作者: Janny_lwj | 来源:发表于2018-05-02 10:50 被阅读0次

    想要了解Docker

    Volume,首先我们需要知道Docker的文件系统是如何工作的.Docker镜像是由多个文件系统(只读层)叠加而成.当我们启动一个容器的时候,Docker会加载镜像层并在其上添加一个读写层.如果运行中的容器修改了现有的一个已存在的文件,那该文件将会从读写层下的只读层复制到读写层,该文件的只读版本仍然存在,只是已经被读写层中该文件的副本所隐藏.当删除Docker容器,并通过该镜像重新启动时,之前的更改将会丢失.Docker,只读层以及在顶部的读写层的组合被称为Union

    FIle System(联合文件系统).

    为了能够保存(持久化)数据以及共享容器间的数据,Docker提出了Volume的概念.简单来说,Volume就是目录或者文件,它可以绕过默认的联合文件系统,而以正常的文件或者目录的形式存在于宿主机上.

    我们可以通过两种方式来初始化Volume,这两种方式有些细小而又重要的差别.我们可以在运行时使用-v来声明Volume:

    1.       root@syx-VB:/home/syx#dockerrun -it --name container-test -h CONTAINER -v /data ubuntu /bin/bash

    2.       root@CONTAINER:/# ls /data/

    3.       root@CONTAINER:/#

    root@syx-VB:/home/syx#dockerrun -it --name container-test -h CONTAINER -v /data ubuntu /bin/bash

    root@CONTAINER:/# ls /data/

    root@CONTAINER:/#

    上面的命令会将/data挂载到容器中,并绕过联合文件系统,我们可以在主机上直接操作该目录.任何在该镜像/data路径的文件的文件都会被复制到Volume.我们可以使用docker

    inspect命令找到Volume在主机上的存储位置:

    1.       $dockerinspect container-test

    2.       "Mounts": [

    3.       {

    ]4.       "Name": "6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134",

    5.       "Source": "/var/lib/docker/volumes/6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134/_data",

    6.       "Destination": "/data",

    7.       "Driver": "local",

    8.       "Mode": "",

    9.       "RW": true

    10.   }

    11.   ],

    $dockerinspect container-test

    "Mounts": [

    {

    "Name": "6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134",

    "Source": "/var/lib/docker/volumes/6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134/_data",

    "Destination": "/data",

    "Driver": "local",

    "Mode": "",

    "RW": true

    }

    ],

    这说明Docker把在/var/lib/docker下的某个目录挂载到了容器内的/data目录下.让我们从主机添加文件都此文件夹下:

    1.       root@syx-VB:~# touch /var/lib/docker/volumes/6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134/_data/test-file

    root@syx-VB:~# touch /var/lib/docker/volumes/6407cbb6700a4076cdeeef60629f1748ff34310102480a3f702fd3fee9e69134/_data/test-file

    进入容器

    1.       root@syx-VB:~#dockerattach container-test

    2.       root@CONTAINER:/# ls /data/

    3.      test-file

    root@syx-VB:~#dockerattach container-test

    root@CONTAINER:/# ls /data/

    test-file

    只要将主机的目录挂载到容器的目录上,那改变就会立即生效.我们可以在Dockerfile中通过使用VOLUME指令来达到相同的目的:

    1.       FROM ubunut VOLUME /data

    FROM ubunut VOLUME /data

    但是还有另一件只有-v参数能够做到而Dockerfile是做不到的事情就是在容器上挂载指定的主机目录.例如:

    1.       root@syx-VB:~#dockerrun -v /home/syx/dockerfile:/data ubuntu ls /data

    2.       df_test1

    root@syx-VB:~#dockerrun -v /home/syx/dockerfile:/dataubuntu ls /data

    df_test1

    该命令将挂载主机的/home/syx/dockerfile目录到容器内的/data目录上.任何在/home/syx/dockerfile目录下的文件都会出现在容器内.这对于在主机和容器之间共享文件是非常有用的,例如挂载需要编译的源代码.为了保证可移植性,挂载主机目录不需要从Dockerfile指定.当使用-v参数时,镜像目录下的任何文件都不会被复制到Volume.

    数据共享

    如果要授权一个容器访问另一个容器的Volume,我们可以使用-volumes-from参数来执行dockerrun

    1.       root@syx-VB:~#dockerrun -it -h NEWCONTAINER --volumes-from container-test ubuntu /bin/bash

    2.       root@NEWCONTAINER:/# ls /data/

    3.       test-file

    root@syx-VB:~#dockerrun -it -h NEWCONTAINER--volumes-from container-test ubuntu /bin/bash

    root@NEWCONTAINER:/# ls /data/

    test-file

    值得注意的是,就算你这个时候把container-test停止了,它仍然会起作用.只要有容器连接Volume,他就不会被删除,如果这个时候你执行:

    1.      root@syx-VB:~#dockerrm container-test

    2.      Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f

    3.      Error: failed to remove containers: [container-test]

    root@syx-VB:~#dockerrm container-test

    Error response from daemon: Conflict, Youcannot remove a running container. Stop the container before attempting removalor use -f

    Error: failed to remove containers:[container-test]

    数据容器

    常见的使用场景是使用纯数据容器来持久化数据库,配置文件或者数据文件等.例如:

    1.      $dockerrun --name dbdate postgres echo “Data-Only container for postgres”

    $dockerrun --name dbdate postgres echo“Data-Only container for postgres”

    该命令将会创建一个已经包含在Dockerfile里定义过Volume的postgres镜像,运行echo命令然后退出.当我们运行docker

    ps命令时,echo可以帮助我们识别某镜像的用途.我们可以用-volume-from命令来识别其他容器的Volume:

    1.       $dockerrun -d --volumes-from dadate --name db1 postgres

    $dockerrun -d --volumes-from dadate--name db1 postgres

    使用数据容器的两个注意点:

    1.不要运行数据容器,这纯属是在两非自愿

    2.不要为了数据容器而使用最小的镜像”,busyboxscratch,只使用数据库镜像本身就可以了.你已经拥有了该镜像,所以不需要占用额外的空间.

    备份

    如果你在用数据容器,那做备份是相当容易的.

    1.       root@syx-VB:~#dockerrun --rm --volumes-from dbdate -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /var/lib/postgresql/data

    root@syx-VB:~#dockerrun --rm --volumes-from dbdate -v$(pwd):/backup ubuntu tar cvf /backup/backup.tar /var/lib/postgresql/data

    该命令会将Volume里所有的东西压缩为一个tar包(官方的postgres

    Dockerfile在/var/lib/postgresql/data目录下定义了一个Volume).

    1.      root@syx-VB:~# ls

    2.       backup.tar

    root@syx-VB:~# ls

    backup.tar

    删除Volumes

    这个功能太重要了,如果你已经使用docker

    run来删除你的容器,那可能会有很多孤立的Volume仍在占用着空间.

    Voulume可以被删除的条件:

    1.该容器可以用docker

    rm -v来删除且没有其他容器连接到该Volume(以及主机目录是也没被指定为Volume).注意,-v是必不可少的.

    2.dockerrun中使用rm参数.

    相关文章

      网友评论

        本文标题:2018-05-02 深入理解Docker Volume

        本文链接:https://www.haomeiwen.com/subject/nhiqrftx.html