使用下面的docker-compose.yml文件:
test:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes_from:
- cachev
cachev:
build: ../../
dockerfile: docker/dev/Dockerfile
volumes:
- /build
entrypoint: "true"上述文件中的cachev服务启动卷容器,该容器在停靠主机的/var/lib/docker/文件夹中创建匿名卷,并在卷容器(xx_cachev)中创建挂载点/cache。
volumes_from服务下的test指令是否在xx_test容器中创建/build挂载点?指向xx_cachev容器的xx_cachev挂载点?
发布于 2019-10-18 12:51:17
来自volumes_from 文档
从另一个服务或容器中安装所有卷..。
所以简单的回答是,yes,
volumes_from在test服务中挂载由cachev服务定义的/build卷。
较长的答覆:
要回答您的问题,让我们运行test服务:
docker compose up test
在回答您的问题之前,让我们确保描述清楚:
上述文件中的cachev服务启动卷容器..。
它只是因为entrypoint: "true"而立即退出的常规容器。
docker ps -a应该显示:
ac68a33abe59 cache "true" 16 hours ago Exited (0) 4 minutes ago cache_1
但在退出之前,它会创建volumes:中指定的卷。因此,如果它的卷被其他服务使用,例如用于缓存,我们就可以称它为卷容器。
,它在/var/lib/ docker /文件夹中创建匿名卷。
同意。- /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中使用jsons的很好的实用工具。安装它,以使上面的命令工作。
并在卷容器(Xx_cachev)中创建挂载点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将使用VOLUME指令显示所有容器挂载,包括在docker/dev/Dockerfile中指定的容器挂载。
要回答您的问题,我们需要检查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
发布于 2019-10-17 20:52:57
是的,您可以通过在两个容器中执行一个命令来验证。如果在touch /build/fromtest.txt路径下的测试容器中创建文件,它将在同一路径/build/fromtest.txt上的cacheV容器中可见。
volumes_from
从另一个服务或容器中安装所有卷。
一个你可以尝试的演示
test:
image: alpine
command: sh -c "touch /build/fromtest.txt && echo hell from test-container && ls /build/"
volumes_from:
- cachev
cachev:
image: node:alpine
command: sh -c "touch /build/fromcache.txt && echo hello from cache-container && ls /build/"
volumes:
- /build日志将是
Recreating compose-volume_cachev_1 ... done
Recreating compose-volume_test_1 ... done
Attaching to compose-volume_cachev_1, compose-volume_test_1
cachev_1 | hello from cache-container
test_1 | hell from test-container
test_1 | fromcache.txt
test_1 | fromtest.txt
cachev_1 | fromcache.txt
cachev_1 | fromtest.txthttps://stackoverflow.com/questions/58439706
复制相似问题