代码之家  ›  专栏  ›  技术社区  ›  void.pointer

Dockerfile中卷的实际用途是什么?

  •  5
  • void.pointer  · 技术社区  · 6 年前

    首先,我想说清楚,我在研究这个课题时做了尽职调查。非常密切相关的是 this SO question ,这并不能真正解决我的困惑。

    我明白当 VOLUME 是在Dockerfile中指定的,这指示Docker在映射到容器内指定目录的容器期间创建一个未命名卷。例如:

    # Dockerfile
    VOLUME ["/foo"]
    

    这将创建一个卷来包含存储在其中的任何数据 /foo docker volume ls )会以随机数字的形式出现。

    docker run ,此卷不可重用。这是造成混乱的关键点。对我来说,卷的目标是包含持久状态 所有实例 没有显式的卷映射

    #!/usr/bin/env bash
    # Run container for the first time
    docker run -t foo
    
    # Kill the container and re-run it again. Note that the previous 
    # volume would now contain data because services running in `foo`
    # would have written data to that volume.
    docker container stop foo
    docker container rm foo
    
    # Run container a second time
    docker run -t foo
    

    我希望未命名的卷可以在2之间重用 run -v 选项,则为每个 运行

    -五 共享持久状态 运行 命令,我为什么要指定 体积 在我的文件里?没有 体积 ,我可以这样做(使用前面的示例):

    #!/usr/bin/env bash
    # Create a volume for state persistence
    docker volume create foo_data
    
    # Run container for the first time
    docker run -t -v foo_data:/foo foo
    
    # Kill the container and re-run it again. Note that the previous 
    # volume would now contain data because services running in `foo`
    # would have written data to that volume.
    docker container stop foo
    docker container rm foo
    
    # Run container a second time
    docker run -t -v foo_data:/foo foo
    

    现在,第二个容器将把数据装载到 /福 这是前一个例子。我不需要你也能做到 体积

    所以我的问题是:这有什么意义 体积 当您必须通过主机上的命令显式地将命名卷映射到容器时?要么我遗漏了什么,要么这只是混淆和混淆。

    请注意,我在这里的所有断言都基于我对docker行为的观察,以及我从文档中收集的内容。

    1 回复  |  直到 6 年前
        1
  •  15
  •   S-Man    6 年前

    VOLUME EXPOSE 有点不合时宜。我们今天所知的命名卷是在 Docker 1.9 差不多三年前。

    体积 说明(或使用 --volume 选项)是创建用于数据共享或持久性的卷的唯一方法。事实上,最好的做法是创建纯数据容器,其唯一目的是保存一个或多个卷,然后使用 --volumes-from 选项。以下是一些描述这种过时模式的文章。

    还有,看看 moby/moby#17798 (Data-only containers obsolete with docker 1.9.0?) 其中讨论了从仅数据容器到命名卷的更改。

    今天,我考虑 体积 教学是一种先进的工具,只应用于特殊情况,并经过仔细考虑。例如,官方postgres图像声明 VOLUME at /var/lib/postgresql/data /var/lib/postgresql/数据

    然而 体积 指导是要付出代价的。

    • 无法删除Dockerfile中声明的卷。下游映像无法向存在卷的路径添加数据。

    对于GitLab问题,有人想用预配置的数据扩展GitLab映像以进行测试,但是由于 VOLUME at /var/opt/gitlab

    热释光;博士: 体积