Docker, Volumes vs Bind Mounts for persistent data such as DB, elasticsearch?

放肆的年华 提交于 2019-12-10 15:16:33

问题


docker data volume vs mounted host directory

says volumes should be preferred over bind mounts

I have a few questions regarding the issue. The post says:

When you create a volume, it is stored within a directory on the Docker host

Bear with me, but I'm new to docker, and I'm wondering what is docker host here.

Is it a machine where I build the image (probably not)?
Is it the machine where the image will be run? If it is so, what happens if I run the image on multiple machines, will it create two independent volumes?
When I have developement and production setup, how docker manages two separate volumes for each environment?

Besides it seems fairly easy to lose data by doing docker-compose down when I use data volumes, that's the first obstacle that makes me to hesitate to use data volumes, is there an obvious solution to mitigate the issue?


回答1:


That's not a doctrine actually - not using bind mounts. Yes, they can damage your host's file system if mounted inaccurately (like -v /bin:/var/log) as soon as your have root privileges inside container by default; also they are less portable but they facilitate file exchange between host and container. When you want to provide initial configuration for your service, or put source code for compilation into container, I believe you would prefer to bind mount instead of creating and running temporary container for docker volume cp operations. Also, you should always use :ro option when possible (read only) to prevent data modification from inside container.

Docker host - it is a machine (PC), where Docker daemon is running.

Is it a machine where I build the image (probably not)?

Not true. You can build using docker CLI or docker API remotely.

Is it the machine where the image will be run?

Yes, images are run by docker daemon and thus it will be the host.

If it is so, what happens if I run the image on multiple machines, will it create two independent volumes?

It depends. Running images on different machines can be achieved in different ways, staring with orchestrators like kubernetes or docker swarm and ending with manual launch on separate docker daemons. With orchestrators it is possible to have same volume, shared among different hosts, but in this case you can't use bind mounts, you use volumes.

When I have developement and production setup, how docker manages two separate volumes for each environment?

Docker doesn't it is you who manages.

Besides it seems fairly easy to lose data by doing docker-compose down when I use data volumes, that's the first obstacle that makes me to hesitate to use data volumes, is there an obvious solution to mitigate the issue?

Volumes can easily persist between docker-compose sessions. The most explicit way to achieve that is to declare volume in advance with

docker volume create foo

and then use it in your compose files:

version: '3'
services:
  abc:
    volumes:
      foo:/foo
volumes:
  foo:
    external: true


来源:https://stackoverflow.com/questions/54519261/docker-volumes-vs-bind-mounts-for-persistent-data-such-as-db-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!