Lets take a simple one, the library/postgres image, in its dockerfile it exposes a volume /var/lib/postgresql/data Suppose I mapped that locally to /docker/db_data in a single container setup, I understand that the contents of /var/lib/postgresql/data would be found in my laptops /docker/db_data folder.
Not exactly. You can mount a directory from your laptop into a container with -v /docker/db_data:/var/lib/postgressql/data
. If /var/lib/postgressql/data
already exists in the container, it will be overwritten with the content of /docker/db_data
.
However, if you use -v /var/lib/postgressql/data
(not mounting a host directory), any files that the image has in /var/lib/postgressql/data
will be copied into the volume.
You can see the actual location of the data for a volume with the docker inspect
command, eg docker inspect postgressql
will show you something like…
"Volumes": {
"/var/lib/postgressql/data": "/var/lib/docker/volumes/[some_guid_like_string]"
}
But if I want to enable a data volume container to manage both of these, how can this be done?
A container can have multiple volumes with multiple -v
arguments. In your case if you wanted one data volume container with 2 volumes, you would first create a container to serve as the data volume container with:
docker create -v /var/lib/postgressql/data -v /var/jenkins_home --name mydata ubuntu /bin/true
Note the 2 volumes. You docker create
because the data container does not need to be running to store data.
Now your postgres and jenkins containers can access the volumes using the --volumes-from
flag. Here's an example for postgres:
docker run --volumes-from mydata --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
With --volumes-from
, the postgres container will use both the volumes from the mydata
container, although the postgres one only really cares about /var/lib/postgressql/data
.
would it be considered best practises if I used 2 data volume containers, and kept it simple?
I'm not sure about best practice, but I'd say there's no reason for 1 data volume container to manage the data of two unrelated containers. I'd go with separate data volume containers for each.
If, later on, you needed to do something like backup or migrate the data for both postgres and jenkins, you could create another container on the fly with multiple --volumes-from
and back up the whole thing. It's pretty flexible.