最佳实践 - 匿名卷与绑定安装 [英] Best practice - Anonymous volume vs bind mount

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

问题描述

在容器中,

可以创建匿名卷

Dockerfile

以下语法,volumes 具有 /build 条目

below syntax with volumes having /build entry

cache:
  build: ../../
  dockerfile: docker/dev/Dockerfile
  volumes:
    - /tmp/cache:/cache
    - /build
  entrypoint: "true"

<小时>

我的理解是,在容器进入 Exited 状态后,两种方法(以上)都使 /build 卷也可用.


My understanding is, both approach(above) make volume /build also available after container goes in Exited state.

Volume 是匿名的,因为 /build 指向 docker 主机中某个随机的新位置(在 /var/lib/docker/volumes 目录中)

Volume is anonymous because /build points to some random new location(in /var/lib/docker/volumes directory) in docker host

我发现匿名卷比命名卷更安全(比如 /tmp/cache:/cache).

I see that anonymous volumes are more safer than named volumes(like /tmp/cache:/cache).

因为 /tmp/cache 位置容易受到攻击,因为该位置被多个 docker 容器使用的可能性更大.

Because /tmp/cache location is vulnerable because there is more chance that this location is used by more than one docker container.

1)

为什么不鼓励使用匿名卷?

Why anonymous volume usage is discouraged?

2)

VOLUME/build in Dockerfile

不一样

volumes: 
 - /build 

docker-compose.yml 文件中?是否存在需要同时提及两者的场景?

in docker-compose.yml file? Is there a scenario, where we need to mention both?

推荐答案

其实匿名卷 (/build) 的用法是鼓励 使用 bind挂载 (/tmp/cache:/cache):

Actually, anonymous volumes (/build) usage is encouraged over the use of bind mounts (/tmp/cache:/cache):

与绑定安装相比,卷有几个优点:

Volumes have several advantages over bind mounts:

  • 与绑定安装相比,卷更容易备份或迁移.
  • 您可以使用 Docker CLI 命令或 Docker API 管理卷.
  • Volumes 适用于 Linux 和 Windows 容器.
  • 可以更安全地在多个容器之间共享卷.
  • 卷驱动程序让您可以在远程主机或云提供商上存储卷,以加密卷的内容,或添加其他功能.
  • 新卷的内容可以由容器预先填充.

关于你的第二个问题,是的.您可以在 docker-compose 文件或 Dockerfile 中创建匿名卷.不需要在两个地方都指定.

Regarding your second question, yes. You can create anonymous volumes in docker-compose file or in the Dockerfile. No need to specify in both places.

这篇关于最佳实践 - 匿名卷与绑定安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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