volume_from指令-docker撰写 [英] volumes_from instruction - docker compose
问题描述
使用以下 docker-compose.yml
文件:
test:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes_from:
- cachev
cachev:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes:
- /build
entrypoint: "true"
cachev
服务启动卷容器,该容器在Docker主机的 / var / lib / docker /
文件夹中创建匿名卷,并创建挂载点 / cache
在卷容器中( xx_cachev
)。
cachev
service in above file launches volume container that creates anonymous volume in /var/lib/docker/
folder in docker host and creates mount point /cache
within volume container(xx_cachev
).
是否<$ test
服务下的c $ c> volumes_from 指令在<$中创建 / build
挂载点c $ c> xx_test 容器?指向 xx_cachev
容器的 / build
挂载点?
Does volumes_from
instruction under test
service create /build
mount point in xx_test
container? that points to /build
mount point of xx_cachev
container?
推荐答案
来自 volumes_from
文档:
从另一个服务或容器安装所有卷...
Mount all of the volumes from another service or container...
因此简短的答案是是:
volumes_from
挂载 / build
由 cachev $ c $定义的卷c>在
test
服务内的服务。
volumes_from
mounts /build
volume defined by cachev
service inside test
service.
长答案:
要回答您的问题,我们运行 test
服务:
To answer your question let's run the test
service:
docker compose up测试
在回答问题之前,请确保描述清楚:
Before answering your question, let's make sure the description is clear:
以上文件中的
cachev服务启动了卷容器...
cachev service in above file launches volume container...
只是由于 entrypoint: true
而立即退出的常规容器。
It's just regular container which exits immediately because of entrypoint: "true"
.
docker ps -a
应该显示:
ac68a33abe59缓存 true 16小时前退出(0)4分钟前cache_1
但是在退出之前它会创建 volumes:
中指定的卷。因此,如果其他服务使用其卷(例如进行缓存),我们可以将其称为卷容器。
But before it exits it creates volumes specified in volumes:
. So we can call it volume container if its volumes are used by other service, for caching for instance.
会在docker host中的/ var / lib / docker /文件夹中创建匿名卷
that creates anonymous volume in /var/lib/docker/ folder in docker host
同意。 -/ build
是匿名卷。可以通过查看所有容器安装来验证:
Agree. - /build
is anonymous volume. Can be verified by viewing all container mounts:
docker inspect [cachev_container_id] --format’{{json .Mounts}}’| jq
应显示以下内容:
{
"Type": "volume",
"Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",
"Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",
"Destination": "/build",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
jq
是在bash中处理json的强大工具。
jq
is great utility for working with jsons in bash. Install it for the above command to work.
并在卷容器(xx_cachev)中创建安装点/ cache。
and creates mount point /cache within volume container(xx_cachev).
在您提供的 cachev:
服务规范中看不到任何挂载的迹象。
Don't see any evidence of mounts in cachev:
service spec you provided.
如果将映射-/ tmp / cache:/ cache
添加到其卷
部分并再次运行 docker compose up test
并检查退出的容器,您应该看到:
If you add mapping - /tmp/cache:/cache
to its volumes
section and run docker compose up test
again and inspect the exited container you should see:
{
"Type": "bind",
"Source": "/tmp/cache",
"Destination": "/cache",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
请注意, docker检查[cachev_service_id] --format'{{json .Mounts}}'| jq
将显示所有容器装载,包括使用 VOLUME
在 docker / dev / Dockerfile
中指定的装载。
Please, note that docker inspect [cachev_service_id] --format '{{json .Mounts}}' | jq
will show all container mounts including those specified in docker/dev/Dockerfile
using VOLUME
instruction.
要回答您的问题,我们需要检查 test
服务容器:
To answer to your question we need to inspect test
service container:
docker inspect [test_container_id] --format'{{json .Mounts}}''| jq
:
将显示 docker / dev / Dockerfile
中指定的所有卷借助 volumes_from
指令, cachev
的所有卷。
would show all the volumes specified in docker/dev/Dockerfile
if any and all the volumes of cachev
thanks to volumes_from
instruction.
您可以看到 test
和 cache
容器都具有:
You can see that both test
and cache
containers have:
{
"Type": "volume",
"Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",
"Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",
"Destination": "/build",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
在他们的坐骑中,该体积在随后的 docker组合测试
in their mounts and this volume survives subsequent runs of docker compose up test
这篇关于volume_from指令-docker撰写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!