等效于Docker Compose v3中的volume_from [英] Equivalent of volumes_from in Docker Compose v3

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

问题描述

这里似乎存在类似的问题( docker-compose volume_from与版本3 如何在docker- 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中重写它?

推荐答案

要回答您的问题-在v3中是不可能的-请参阅以下部分。 v3不得用作后继(也是docker的官方声明),而应在大量案例中使用。

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文件$的主机上定义命名卷+绑定b $ b示例:

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

或简而言之

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

然后使用 docker volume create cli上的语法来创建具有绑定选项的卷,如上文所述

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不是v2的后继产品,因此v3中没有volumes_from的等效内容,它是另一种选择-请在此处查看我的评论和来源< a href = https://github.com/rancher/rancher/issues/3316#issuecomment-310889283 rel = noreferrer> 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_from和volumes有重叠。

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)如果要跨堆栈升级数据(向下+向上),则选择命名为vol umes-现在,如果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 / proxy场景中的this的升级),所以您将为此创建一个非卷。使用 Volume / var / www 在Dockerfile中 / var / www ,然后使用volume_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的要点是,在堆栈上升级时,将删除anon卷(`docker-compose down删除anon容器,但未命名容器),因此升级按预期进行,您有了新的代码库

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中的volume_from的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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