volumes_from instruction - docker compose

2020-04-20 13:51发布

问题:

With the below docker-compose.yml file:

test:
  build: ../../
  dockerfile: docker/dev/Dockerfile
  volumes_from:
    - cachev

cachev:
  build: ../../
  dockerfile: docker/dev/Dockerfile
  volumes:
    - /build
  entrypoint: "true"

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).


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?

回答1:

From volumes_from docs:

Mount all of the volumes from another service or container...

So the short answer is yes:

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

Long answer:

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

docker compose up test

Before answering your question, let's make sure the description is clear:

cachev service in above file launches volume container...

It's just regular container which exits immediately because of entrypoint: "true".

docker ps -a should show:

ac68a33abe59 cache "true" 16 hours ago Exited (0) 4 minutes ago cache_1

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.

that creates anonymous volume in /var/lib/docker/ folder in docker host

Agree. - /build is anonymous volume. Can be verified by viewing all container mounts:

docker inspect [cachev_container_id] --format '{{json .Mounts}}' | jq

should show something like:

  {
    "Type": "volume",
    "Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",
    "Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",
        "Destination": "/build",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
      }

jq is great utility for working with jsons in bash. Install it for the above command to work.

and creates mount point /cache within volume container(xx_cachev).

Don't see any evidence of mounts in cachev: service spec you provided.

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"
  }

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.

To answer to your question we need to inspect test service container:

docker inspect [test_container_id] --format '{{json .Mounts}}' | jq:

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

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": ""
  }

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



回答2:

Yes, you can verify by executing a command inside both containers. if creat file in test container under touch /build/fromtest.txt path, it will be visible in cacheV container on the same path /build/fromtest.txt.

volumes_from

Mount all of the volumes from another service or container

compose-file-volumes_from

A demo you can try

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

log will be

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.txt