docker mounting volumes on host

前端 未结 6 676
陌清茗
陌清茗 2020-11-28 18:34

I have successfully been able to share folders between a docker container with volumes using

docker run -v /host/path:/container/path ...

B

6条回答
  •  情书的邮戳
    2020-11-28 18:57

    Basically VOLUME and -v option are almost equal. These mean 'mount specific directory on your container'. For example, VOLUME /data and -v /data is exactly same meaning. If you run the image that have VOLUME /data or with -v /data option, /data directory is mounted your container. This directory doesn't belong to your container.

    Imagine that You add some files to /data on the container, then commit the container into new image. There isn't any files on data directory because mounted /data directory is belong to original container.

    $ docker run -it -v /data --name volume ubuntu:14.04 bash
    root@2b5e0f2d37cd:/# cd /data
    root@2b5e0f2d37cd:/data# touch 1 2 3 4 5 6 7 8 9
    root@2b5e0f2d37cd:/data# cd /tmp
    root@2b5e0f2d37cd:/tmp# touch 1 2 3 4 5 6 7 8 9
    root@2b5e0f2d37cd:/tmp# exit
    exit
    
    $ docker commit volume nacyot/volume  
    835cfe3d8d159622507ba3256bb1c0b0d6e7c1419ae32751ad0f925c40378945
    nacyot $ docker run -it nacyot/volume
    root@dbe335c7e64d:/# cd /data
    root@dbe335c7e64d:/data# ls
    root@dbe335c7e64d:/data# cd /tmp
    root@dbe335c7e64d:/tmp# ls
    1  2  3  4  5  6  7  8  9
    root@dbe335c7e64d:/tmp# 
    root@dbe335c7e64d:/tmp# 
    

    This mounted directory like /data is used to store data that is not belong to your application. And you can predefine the data directory that is not belong to the container by using VOLUME.

    A difference between Volume and -v option is that you can use -v option dynamically on starting container. It mean you can mount some directory dynamically. And another difference is that you can mount your host directory on your container by using -v

提交回复
热议问题