gpt4 book ai didi

Docker 匿名卷

转载 作者:IT老高 更新时间:2023-10-28 21:22:46 25 4
gpt4 key购买 nike

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

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

我注意到 Drupal's official image ,他们的 docker-compose.yml 文件正在使用 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 示例:

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 的后续问题/评论,询问匿名卷如何增加灵 active ,并回答 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:您可以通过“数据容器”将特定匿名卷与正在运行的容器相关联,但这提供了灵 active 来涵盖现在更好地服务于使用的用例命名卷。

在 Docker 1.9 中添加卷管理之前,匿名卷很有帮助。在此之前,您无法选择命名卷。在 1.9 版本中,卷变成了具有自己生命周期的离散、可管理的对象。

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

docker create -v /data --name datacontainer mysql

然后将数据容器的匿名卷挂载到需要访问该卷的容器中

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

如今,最好使用命名卷,因为它们更易于管理且更明确。

关于Docker 匿名卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com