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?
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 tobind mount
instead of creating and running temporary container fordocker 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.
Not true. You can build using
docker CLI
ordocker API
remotely.Yes, images are run by docker daemon and thus it will be the host.
It depends. Running images on different machines can be achieved in different ways, staring with orchestrators like
kubernetes
ordocker 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 usebind mounts
, you usevolumes
.Docker doesn't it is you who manages.
Volumes can easily persist between
docker-compose
sessions. The most explicit way to achieve that is to declare volume in advance withand then use it in your compose files: