docker data volume vs mounted host directory

后端 未结 4 1838
悲&欢浪女
悲&欢浪女 2020-11-30 22:31

We can have a data volume in docker:

$ docker run -v /path/to/data/in/container --name test_container debian
$ docker inspect test_container
...
Mounts\": [
         


        
4条回答
  •  时光说笑
    2020-11-30 23:15

    is it any different from having the data in a folder mounted using -v /path/to/data/in/container:/home/user/a_good_place_to_have_data?

    It is because, as mentioned in "Mount a host directory as a data volume"

    The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile because built images should be portable. A host directory wouldn’t be available on all potential hosts.

    If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

    You can combine both approaches:

     docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
    

    Here we’ve launched a new container and mounted the volume from the dbdata container.
    We’ve then mounted a local host directory as /backup.
    Finally, we’ve passed a command that uses tar to backup the contents of the dbdata volume to a backup.tar file inside our /backup directory. When the command completes and the container stops we’ll be left with a backup of our dbdata volume.

提交回复
热议问题