Docker匿名卷 [英] Docker Anonymous Volumes

查看:63
本文介绍了Docker匿名卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 docker-compose.yml 文件中看到了Docker卷定义,如下所示:

I've seen Docker volume definitions in docker-compose.yml files like so:

-v /path/on/host/modules:/var/www/html/modules

我注意到 Drupal的官方形象,即他们的 docker-compose.yml 文件正在使用匿名卷.

I noticed that Drupal's official image, their docker-compose.yml file is using anonymous volumes.

注意评论:

volumes:
  - /var/www/html/modules
  - /var/www/html/profiles
  - /var/www/html/themes
  # this takes advantage of the feature in Docker that a new anonymous
  # volume (which is what we're creating here) will be initialized with the
  # existing content of the image at the same location
  - /var/www/html/sites

容器运行后,是否可以将匿名卷与主机上的路径相关联?如果没有,拥有匿名卷的意义何在?

完整的docker-compose.yml示例:

Full docker-compose.yml example:

version: '3.1'

services:

  drupal:
    image: drupal:8.2-apache
    ports:
      - 8080:80
    volumes:
      - /var/www/html/modules
      - /var/www/html/profiles
      - /var/www/html/themes
      # this takes advantage of the feature in Docker that a new anonymous
      # volume (which is what we're creating here) will be initialized with the
      # existing content of the image at the same location
      - /var/www/html/sites
    restart: always

  postgres:
    image: postgres:9.6
    environment:
      POSTGRES_PASSWORD: example
    restart: always

推荐答案

添加更多信息,以回应@JeffRSon的后续问题/评论,询问匿名卷如何增加灵活性,并从操作:

Adding a bit more info in response to a follow-up question/comment from @JeffRSon asking how anonymous volumes add flexibility, and also to answer this question from the OP:

容器运行后,是否可以将匿名卷与主机上的路径相关联?如果没有,拥有匿名卷的意义何在?

Is there a way to associate an anonymous volume with a path on the host machine after the container is running? If not, what is the point of having anonymous volumes?

TL; DR :您可以通过数据容器"将特定的匿名卷与正在运行的容器相关联,但是这提供了覆盖使用案例的灵活性,现在该使用案例可以更好地满足使用需求的命名卷.

TL;DR: You can associate a specific anonymous volume with a running container via a 'data container', but that provides flexibility to cover a use case that is now much better served by the use of named volumes.

在Docker 1.9中添加卷管理之前,匿名卷非常有用.在此之前,您无法选择命名卷.在1.9版中,卷变成了具有各自生命周期的离散且可管理的对象.

Anonymous volumes were helpful before the addition of volume management in Docker 1.9. Prior to that, you didn't have the option of naming a volume. With the 1.9 release, volumes became discrete, manageable objects with their own lifecycle.

在1.9之前,由于无法命名卷,您必须先创建一个数据容器来引用它

Before 1.9, without the ability to name a volume, you had to reference it by first creating a data container

docker create -v /data --name datacontainer mysql

然后将数据容器的匿名卷装入需要访问该卷的容器中

and then mounting the data container's anonymous volume into the container that needed access to the volume

docker run -d --volumes-from datacontainer --name dbinstance mysql

这几天,最好使用命名卷,因为它们更易于管理且更明确.

These days, it's better to use named volumes since they are much easier to manage and much more explicit.

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

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