Share directory or volume with container from host

2019-05-10 23:50发布

I have a directory(maybe later volume), that I would like to share with all my interactive containers. I know, that native Docker volumes are stored under /var/lib/docker/volumes and docker run -v seems the easiest way, but I think Data Volume Container is a much more standardized way. I don't know, how to create this volume container from a directory or an existing another volume. Maybe is it wrong method?

标签: docker share
2条回答
叛逆
2楼-- · 2019-05-11 00:25

There are two ways to create and share volumes: 1. using the VOLUME instruction on the Dockerfile. 2 Specifying the -v <volume_name> option during container runtime and later using --volumes-from=<container> with every subsequent container which need to share the data. Here is an ex with the later:

  1. Start your first container with -v, then add a test file under the directory of the shared volume.
docker run -it -v /test-volume --name=testimage1 ubuntu:14.04 /bin/bash

root@ca30f0f99401:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test-volume ===> test-volume dir got created here

root@ca30f0f99401:/# touch test-volume/1

root@ca30f0f99401:/# cat > test-volume/1     
Test Message!
  1. From the host OS, you can get details of the volume by inspecting your container:

docker inspect ca30f0f99401 | grep -i --color -E '^|Vol'

"Mounts": 
        {
            "Name": "025835b8b47d282ec5f27c53b3165aee83ecdb626dc36b3b18b2e128595d9134",
            "Source": "/var/lib/docker/volumes/025835b8b47d282ec5f27c53b3165aee83ecdb626dc36b3b18b2e128595d9134/_data",
            "Destination": "/test-volume",
            "Driver": "local",
            "Mode": "",
            "RW": true 

"Image": "ubuntu:14.04",
    "Volumes": {
        "/test-volume": {} }
  1. Start another container with a shared volume and check if the shared folder/files exists.
$ docker run -it --name=testimage2 --volumes-from=testimage1 ubuntu:14.04 /bin/bash

root@60ff1dcebc44:/# ls 
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test-volume  tmp  usr  var

root@60ff1dcebc44:/# cat test-volume/1
Test Message!
  1. Goto step-3 to share volume with a new container.
查看更多
The star\"
3楼-- · 2019-05-11 00:41

Make a data volume container by writing a dedicated Dockerfile in which you would:

  • COPY your folder in it
  • declare that copied local container path folder as a VOLUME

Then docker create <imagename> and you get a (created) container, that you can mount in all your other containers, provided you run them with the --volumes-from <containername> option.

查看更多
登录 后发表回答