Sharing volume between Docker containers

前端 未结 1 632
醉话见心
醉话见心 2021-02-01 22:25

I am using Docker to deploy some services and I want to share the Docker volumes between different containers.

Suppose I have a Docker container A which mounts a volume a

相关标签:
1条回答
  • 2021-02-01 22:52

    You may find a lot of pointers mentioning data-only containers and --volumes-from. However, since docker 1.9, volumes have become first class citizens, they can have names, and have more flexibility:

    It's now easy to achieve the behavior you want, here's an example :

    1. Create a named data volume with name service-data:

      docker volume create --name service-data
      
    2. You can then create a container that mounts it in your /public folder by using the -v flag:

      docker run -t -i -v service-data:/public debian:jessie /bin/bash
      

      For testing purpose we create a small text file in our mapped folder:

      cd public
      echo 'hello' > 'hello.txt'
      
    3. You may then attach your named volume to a second container, but this time under the data folder:

      docker run -t -i -v service-data:/data debian:jessie /bin/bash
      ls /data       #-->shows "hello.txt"
      

    Just remember, if both containers are using different images, be careful with ownership and permissions!

    0 讨论(0)
提交回复
热议问题