相当于 Docker Compose v3 中的volumes_from [英] Equivalent of volumes_from in Docker Compose v3

查看:22
本文介绍了相当于 Docker Compose v3 中的volumes_from的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有看似相似的问题(docker-compose volumes_from 等效于版本 3如何替换 docker 中的volumes_from-composer v3),但我认为他们没有回答这个问题(或者至少我不明白这些答案是如何解决问题的).所以让我试着再问一次,非常具体.

There are seemingly similar questions here (docker-compose volumes_from equivalent with version 3, How to replace volumes_from in docker-composer v3) but I don't think they answer the question (or at least I don't understand how the answers solve the problem). So let me try to ask again, very specifically.

我有这个 v2 docker-compose.yml:

I have this v2 docker-compose.yml:

version: '2'
services:
  full-tests:
    volumes:
      - ..:/opt/project
      - ../../../ext-libs:/opt/ext-libs
      - ./third-mapping:/opt/third

  unit-tests:
    volumes_from: full-tests

关键是卷集定义一次,我可以使用 volumes_from 轻松重用它们.

The point is that the set of volumes is defined once and I can easily reuse them using volumes_from.

你会如何在 v3 中重写这个?

How would you rewrite this in v3?

推荐答案

要回答您的问题 - 使用 v3 不可能 - 请参阅以下部分.v3 不得作为后继者(也是 docker 官方声明),应用于swarm case".

To answer your question - its impossible with v3 - see the section below. v3 shall not be used as a successor ( also a official statement by docker ) it shall be used in "swarm cases".

尽管如此,您应该做的是使用命名卷.

nevertheless, what you do should do is using named volumes.

您可以将它与这样的主机安装卷结合使用

You can combine it with host-mount volumes like this

docker volume create --name volume1 -o type=none -o device=/home/$USER/projects/01 -o o=bind

您可以使用 3.2 中引入的长语法来简化此操作:https://docs.docker.com/compose/compose-file/#long-syntax-2 这样您就可以在 docker-compose 文件中定义主机上的命名卷 + 绑定例子:

You can simplify this using the long-syntax introduced in 3.2: https://docs.docker.com/compose/compose-file/#long-syntax-2 so you can define the named volume + bind on the host in the docker-compose file example:

services:
   full-tests:
    volumes:     
      - type: volume
        source: ../
        target: /opt/project
      - type: volume
        source: ../../../ext-libs
        target: /opt/ext-libs

或者简而言之,就像你曾经拥有的

or in short as you had

services:
   full-tests:
     volumes:     
      - ../:/opt/project
      - ../../../ext-libs:/opt/ext-libs

但是你不能做的是,将长语法放在顶级卷"定义下,为该卷命名并在服务的卷部分重用它 - 这是不可能的.为此,您将使用

What you cannot do though, putting the long-syntax under the top-level "volumes" definition to give that volume a name and reused it in the volumes section in the services - that is not possible. To do so, you would use a

volumes:
  project:
    external: true
  third-party:
    external: true

然后使用 cli 上的docker volume create"语法使用绑定选项创建这些卷,如上面的概述

And then use the "docker volume create" syntax on the cli to create those volumes with a bind option, as outlines above

但你永远不会得到volumes_from在这里为你做的事情

but you will never get what volumes_from was doing for you here

v3 中没有与volumes_from 等效的东西,因为v3 不是v2 的后继者,它是一个替代方案——请参阅我的评论和此处的来源https://github.com/rancher/rancher/issues/3316#issuecomment-310889283

There is no equivalent of volumes_from in v3, since v3 is not a successor of v2, its an alternative - please see my comment and the sources here https://github.com/rancher/rancher/issues/3316#issuecomment-310889283

总而言之——volumes_from 和volumes 有重叠,因为volumes_from 只是以错误的方式/在错误的领域使用.

To sum it up - volumes_from and volumes have an overlap in the case volumes_from was just used the wrong way / in the wrong field.

a) 如果您希望数据在 堆栈升级(向下 + 向上)中持久保存,您可以选择命名卷 - 现在,如果 2+ 服务需要共享它,只需挂载此命名卷使用 volumes:.

a) If you want data to be persisted across stack upgrades ( down + up ), you pick named volumes - and now, if 2+ services needs to share this, just mount this named volume using volumes:.

b) 但是,如果您希望数据在堆栈升级时保持不变(例如,因为它的源代码和图像实际上包含一个升级),就像在通常的应用程序中一样 + httpd/代理场景,您将为此创建一个匿名卷,例如/var/www 在 Dockerfile 中使用 Volume/var/www 然后使用 volumes_from 将其挂载到 httpd 服务中.

b) If you though, do not want the data to persist over stack upgrades ( e.g. because its source code and the image actually includes an upgrades this ) as a in a usual application + httpd/proxy scenario, you will create a anon-volume for this e.g. /var/www in the Dockerfile using Volume /var/www and then use volumes_from to mount it in the httpd service.

b 的要点是,在堆栈升级时,匿名卷将被删除(`docker-compose down 删除匿名容器,但没有命名容器),因此升级按预期进行,您有一个新的代码库

the main point with b is, that on stack upgrades, the anon volume will be removed ( `docker-compose down removes anon containers, but not named ones ) and thus the upgrade works as expected, you have a new codebase

尝试对命名卷做同样的事情会让你在第一次升级时大吃一惊,因为代码在命名卷上,这将覆盖代码库在新鲜"图像上/新容器,因此升级后您将在旧代码库上运行.

Trying to do the same with named volumes will give you a huge suprise on the first upgrade, since the code is on a named volume and that will overlay the codebase on the "fresh" image / new container, thus you will run on the old codebase after the upgrade.

这篇关于相当于 Docker Compose v3 中的volumes_from的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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