A data volume is a specially-designated directory within one or more containers
that bypasses the Union File System.
Data volumes are designed to persist data, independent of the container's life cycle.
Docker therefore never automatically delete volumes when you remove a container,
nor will it "garbage collect" volumes that are no longer referenced by a container.
You can add a data volume to a container using the -v flag with the docker create and docker run command.
You can use the -v multiple times to mount multiple data volumes
You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container
created from that image.
The -v flag can also be used to mount a single file - instead of just directories - from the host machine.
$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
Creating and mounting a Data Volume Container
Let's create a new named container with a volume to share.
While this container doesn't run an application,
it reuses the training/postgres image so that ... saving disk space.
$ sudo docker create -v /dbdata --name dbdata training/postgres
$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
... The result is only the files from the dbdata container are visible.
... You can use multiple --volumes-from parameters to bring together multiple data volumes from multiple containers.
$ sudo docker run -d --name db3 --volumes-from db1 training/postgres
... To delete the volume from disk, you must explicitly call
docker rm -v against the last container with a reference to the volume.