docker - Volumes_from 指令 - docker compose

标签 docker docker-compose dockerfile

使用以下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说明。

您可以看到 testcache容器有:

  {
    "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/

相关文章:

docker - 在dockerfile中指定 “privileged”容器模式?

linux - 使用 Ansible 迭代从 Docker API 返回的 json

docker 撰写 : Change volume mapping from a docker volume to a folder on the server

wordpress - 扩展Wordpress最新的Dockerfile

mysql - Docker MYSQL '[2006] MySQL server has gone away'

memory-management - 没有足够的内存来运行整个 docker-compose 堆栈

linux - 如何更改docker容器中 'flink'上的默认用户 'root'?

docker - 在 Dockerfile 中运行 sqlcmd

postgresql - Postgres 无法在 swarm 服务器重启时启动

python - 在同一个 Dockerfile 中使用 Python 和 Node.js 并创建一个我在云中同时使用的镜像