Hello.
I have been aware of Docker for some time but never really “understood” how it works until recently. I am reading a book and based on it have created a few container running a few applications.
I understand that Docker recommend to use one container per application and this is what I have done. Also for persistent data I was using a local directory until I discovered Data Volumes. Since it seems to be the preferred way, I have a question.
Like for applications are you supposed to create a data volume per application or could you reuse one for all of them?
I have 3 containers that run 3 applications. I also have 2 containers that run Mariadb and are connected to 2 of the applications. The persistent data for the moment for the database are in 2 local directory.
Do I need to create 2 data volumes container to replace each local directory or can I create 2 mountpoint in one volume and then link it to each database?
I hope I am making sense. As I said I am new to Docker and I am not always sure of which terms to use for certain concept.
Philippe
You can create 2 mountpoints in one volume. I actually never used it but I done some research as I intend to use it soon. What I understood is first you have to create the folder you need to mount for the first container. Then create a named volume
docker run -v NameOfVolume:/PathOfContainerYouWantTheContent --name=test image
You can place all your content in the new path of the NameOfVolume
/var/lib/docker/volumes/NameOfVolume
Then on the second container use the below. Note you need to have the name of the container
docker run --volumes-from test image
Not sure what will happen to the second container is the first is destroyed
thanks for the answer. so if I understand well there is no big difference using a volume or a directory on the host apart from the fact that it is kind of integrated in Docker?
If you use a named volume, the content created in a docker will persist.
If you use a directory, the content created in that path of the container will be erased.
You only need one volume for many dockers only if you want the content to be the same for all docker. Named volumes is easier.
Thanks for the clarification. It is much clearer now.
I have one more silly question if you don’t mind!
Does it make sense to create let’s say one named volume and reuse it for all databases container and another named volume that would contain all kind of data (like configuration data for each specific container).
I understand that each containers would then be able to access all mounted path in these volumes, but does it really matter? or am I being silly trying to minimize the amount of named volumes and should create one per docker container?
If you intend the containers to share access to that directory (just like two independent processes running normally on a UNIX system might), use the same docker volume.
Otherwise, you can use separate volumes and they will read/write different results.
If you are concerned about system resource usage, there is not any noteworthy overhead associated with volumes that I’m aware of.
Hi,
IMHO, there’s no need in using less volumes out of space or performance concerns. As a matter of fact, one should come up with a volume management policy prior to having one docker host full of containers and volumes.
As for sharing named volumes between containers, here are some use cases I can think of:
Data exchange: one container produces data that the other will consume
Config files, that must be accessible to the host, and yet be the same for all containers
Container farms that are supposed to access the same data and have no consistency issues from concurrent access