volumes_from 指令 - docker compose [英] volumes_from instruction - docker compose

查看:19
本文介绍了volumes_from 指令 - 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 服务下的 volumes_from 指令是否会在 xx_test 容器中创建 /build 挂载点?指向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_fromtest 服务中挂载 cachev 服务定义的 /build 卷.

volumes_from mounts /build volume defined by cachev service inside test service.

长答案:

要回答您的问题,让我们运行 test 服务:

To answer your question let's run the test service:

docker 组合测试

在回答您的问题之前,让我们确保描述清楚:

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 cache "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主机的/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 中处理 jsons 的好工具.安装它以使上述命令工作.

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 添加到其 volumes 部分并再次运行 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 inspect [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 中指定的所有卷(如果有)以及 cachev 的所有卷,感谢 volumes_from 指令.

would show all the volumes specified in docker/dev/Dockerfile if any and all the volumes of cachev thanks to volumes_from instruction.

你可以看到 testcache 容器都有:

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 compose up test

in their mounts and this volume survives subsequent runs of docker compose up test

这篇关于volumes_from 指令 - docker compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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