May 22, 2021 Docker From entry to practice
If you have some continuously updated data that needs to be shared between containers, it's a good idea to create a data volume container.
A data volume container is in fact a normal container that is designed to provide data volumes for other containers to mount.
First, create a named data volume container dbdata:
$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
Then, use
--volumes-from
to mount the data volumes in the dbdata container.
$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
You can also use
--volumes-from
parameters to mount multiple data volumes from multiple containers.
You can also mount a data volume from another container that already has a data volume mounted.
$ sudo docker run -d --name db3 --volumes-from db1 training/postgres
Note: Containers
--volumes-from
parameter do not need to remain operational themselves.
If you delete mounted containers, including dbdata, db1, and db2, the data volume is not automatically deleted. I
f you want to delete a data volume, you must use the
docker rm -v
command to specify that the associated container be deleted at the same time when deleting the last container that still has it. T
his allows users to upgrade and move data volumes between containers.
Specific actions will be covered in the next section.