最佳做法-匿名卷与绑定挂载 [英] Best practice - Anonymous volume vs bind mount
问题描述
在容器中
可以创建匿名卷
具有语法( Dockerfile
或
以下语法,其中卷
具有 / build
条目
below syntax with volumes
having /build
entry
cache:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes:
- /tmp/cache:/cache
- /build
entrypoint: "true"
我的理解是,两种方法都可以使体积 / build
在容器放入<$ c之后也可用$ c>退出状态。
卷是匿名的,因为 / 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
在 Dockerfile
与
volumes:
- /build
在 docker-compose.yml
文件中?
推荐答案
实际上是匿名卷( / build
)的使用受到鼓励的使用,而不是绑定安装( / 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管理卷。
- 卷在Linux和Windows容器上均可工作。
- 可以在多个容器之间更安全地共享卷。
- 通过卷驱动程序可以存储卷在远程主机或云提供商上,可以加密卷的内容或添加其他
功能。 - 新卷可以
- Volumes are easier to back up or migrate than bind mounts.
- You can manage volumes using Docker CLI commands or the Docker API.
- Volumes work on both Linux and Windows containers.
- Volumes can be more safely shared among multiple containers.
- Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
- New volumes can have their content pre-populated by a container.
关于第二个问题,是的。您可以在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屋!