-1

Below is the instruction of docker-compose that creates multiple volumes in a container and exits:

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

I see that, the container is in exited state:

a160b66b510d        dev_cache           "true"                   2 minutes ago        Exited (0) About a minute ago                        dev_cache_1

The volumes created in dev_cache_1 container are used by another container.

Can a container use volumes(mount points) created by another container(that is in exited state)?

overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

2

Yes, created volumes survive exited containers that created them. So other containers may use these volumes. See the answer here

Docs also state:

Populate a volume using a container

If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.

Community
  • 1
  • 1
rok
  • 9,403
  • 17
  • 70
  • 126
  • yes, you can inspect the container that use volumes from `cache` to verify it – rok Oct 19 '19 at 11:29
  • Nothing has changed: neither image nor configuration. If you want new containers to run use: `docker-compose up --force-recreate [service_name]` – rok Oct 19 '19 at 11:48
  • this [answer](https://stackoverflow.com/a/52762779/3317808) says, "That anonymous volume will be mounted at every RUN command, prepopulated with the contents of the image, and then discarded at the end of the RUN command. Only changes to the container are saved, not changes to the volume." – overexchange Oct 19 '19 at 11:48
  • the answer talks about adding volumes in Dockerfile. Your question is about docker-compose. So created service containers will use volumes created by other service containers during deployment – rok Oct 19 '19 at 11:50
  • In that case.. am missing some thing... Is `VOLUME /build` in `Dockerfie` not same as `volumes: /build` in `docker-compose`? can you please clarify in this [question](https://stackoverflow.com/q/58463298/3317808)? – overexchange Oct 19 '19 at 11:53
  • it's the same. clarified – rok Oct 19 '19 at 15:31