Docker, Volumes vs Bind Mounts for persistent data

2019-06-28 01:31发布

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条回答
劫难
2楼-- · 2019-06-28 02:03

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
查看更多
登录 后发表回答