使用以下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
)内。
test
服务下的 volumes_from
指令是否在 xx_test
容器中创建 /build
挂载点?指向 xx_cachev
容器的 /build
挂载点?
最佳答案
来自 volumes_from
docs :
Mount all of the volumes from another service or container...
所以简短的答案是是:
volumes_from
坐骑/build
体积由 cachev
定义内部服务test
服务。
长答案:
为了回答您的问题,让我们运行 test
服务:
docker compose up test
在回答您的问题之前,我们先确保描述清楚:
cachev service in above file launches volume container...
这只是一个常规容器,由于entrypoint: "true"
而立即退出。
docker ps -a
应显示:
ac68a33abe59 cache "true" 16 hours ago Exited (0) 4 minutes ago cache_1
但在退出之前,它会创建volumes:
中指定的卷。 。因此,如果它的卷被其他服务使用(例如用于缓存),我们可以将其称为卷容器。
that creates anonymous volume in /var/lib/docker/ folder in docker host
同意。 - /build
是匿名卷。可以通过查看所有容器安装来验证:
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 的一个很棒的实用程序。安装它以使上述命令起作用。
and creates mount point /cache within volume container(xx_cachev).
在cachev:
中没有看到任何安装的证据您提供的服务规范。
如果添加映射- /tmp/cache:/cache
到其volumes
部分并运行 docker compose up test
再次检查退出的容器,您应该看到:
{
"Type": "bind",
"Source": "/tmp/cache",
"Destination": "/cache",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
请注意docker inspect [cachev_service_id] --format '{{json .Mounts}}' | jq
将显示所有容器安装,包括 docker/dev/Dockerfile
中指定的容器安装使用VOLUME
说明。
为了回答您的问题,我们需要检查 test
服务容器:
docker inspect [test_container_id] --format '{{json .Mounts}}' | jq
:
将显示 docker/dev/Dockerfile
中指定的所有卷如果有的话,以及 cachev
的所有卷感谢volumes_from
说明。
您可以看到 test
和cache
容器有:
{
"Type": "volume",
"Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",
"Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",
"Destination": "/build",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
在他们的坐骑中,此卷在 docker compose up test
的后续运行中幸存下来
关于docker - Volumes_from 指令 - docker compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58439706/