持久数据的Docker卷是否只能通过容器路径? [英] Docker volumes for persistent data - is it enough to pass container path only?

查看:133
本文介绍了持久数据的Docker卷是否只能通过容器路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据了解,docker卷允许在docker容器中指定将在docker主机上共享/存储的目录/文件。例如postgres Dockerfile包含以下行

  VOLUME / var / lib / postgresql / data 

所以这意味着 / var / lib / postgresql / data 将实际上存储在主机上,我将可以从主机系统访问该文件。例如,当我检查pg容器时:

 Mounts:[
{
Name 4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f,
Source:/ mnt / sda1 / var / lib / docker / volumes / 4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f / _data,
Destination:/ var / lib / postgresql / data
Driver:local,
Mode:,
RW:true
}
],

这意味着我可以在主机下找到 Source 路径下的卷。我想知道 Source 路径中的 4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f 是什么意思。我的docker主机上找不到这样的id的容器和图像。



我无法理解的一件事是,当我删除容器并重新创建它,然后路径是不同的,实际上没有数据持续...



所以似乎数据持久性我必须添加主机和容器路径的卷来强制它将数据始终保存在相同的路径下 - 这是否正确?

解决方案

以下如何在docker容器中列出卷,我写了一个脚本( updateDataContainerPath )其中: / p>


  • 记录文件中的路径(以容器命名)

  • 或如果该文件已经存在,将会:


    • 删除Mount.Source文件夹(这是空的) li>
    • 将文件中记录的路径移动到Mount.Source路径

    • 记录该文件中的Mount.Source路径




      这样,我可以删除( docker rm -v 选项,显然)一个容器,并重新创建它,同时保持数据放在该卷中。



      你可以看到 gitolite /例如运行


      As I understand docker volumes allow to specify directory/file in docker container that will be shared/stored on docker host. For example postgres Dockerfile contains following line

      VOLUME /var/lib/postgresql/data
      

      So it means that /var/lib/postgresql/data will be actually "stored" on host and I will have access to that file(s) from a host system. For example when I inspect pg container:

      "Mounts": [
              {
                  "Name": "4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f",
                  "Source": "/mnt/sda1/var/lib/docker/volumes/4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f/_data",
                  "Destination": "/var/lib/postgresql/data",
                  "Driver": "local",
                  "Mode": "",
                  "RW": true
              }
          ],
      

      This means that I can find volume on host under Source path. I was wondering what means 4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f in the Source path. I can't find neither container nor image with such id on my docker host.

      One thing I can't understand is that it seems that when I remove container and recreate it, then Source path is different, and in fact no data persist...

      So it seems that for data persistance I have to add volume with both host and container paths to force it to store data always under same path - is this correct?

      解决方案

      Following "How do you list volumes in docker containers?", I have written a script (updateDataContainerPath) which:

      • either records that path in a file (named after the container)
      • or, if that file already exist, will:
        • delete Mount.Source folder (which is empty)
        • move the path recorded in the file to Mount.Source path
        • record Mount.Source path in that file

      That way, I can delete (docker rm, without the -v option, obviously) a container and recreate it, while keeping the data put in that volume.

      You can see that script used in gitolite/run for instance.

      这篇关于持久数据的Docker卷是否只能通过容器路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆