In the following example we create a volume in a container and
share the volume with other containers.
1. Share a docker volume
- create the volume in a container that you do not run
docker create -v /webdata --name webd1 centos
- start a second container and use the volume from the 1st container
docker run -it -d --volumes-from webd1 --name webd2 centos
- attach to the container
docker attach webd2
- put a file in /webdata
touch /webdata/afile
- exit the container with <CTRL>PQ
[root@6c6e823f6eb4 /]# <CRTL>PQ
- start a third container and use the volume from the 1st container
docker run -it -d --volumes-from webd1 --name webd3 centos
-attach to the third container
docker attach webd3
- list the contents of /webdata
ls /webdata
afile
- exit the container with <CTRL>PQ
[root@4531a52b8e2a /]# <CTRL>PQ
- remove all three containers
docker rm -f `docker ps -a|grep webd*|awk '{print $1}'`
- remove the orphaned volume
docker volume ls -qf dangling=true | xargs -r docker volume rm
In the following example we create a directory on the host
and share the directory between containers.
2. share a host directory
- create a directory on the host
mkdir -p /docker/shared
- create a container and use the directory
docker run -d -it --name webd5 -v /docker/shared:/shared centos
-attach to the container
docker attach webd5
- create file in /shared
touch /shared/afile
- exit the container and remove it.
[root@a4f1061d8d9f /]# <CTRL>PQ
docker rm -f webd5
- list the contents of /docker/shared
ls /docker/shared
afile
- start a new container with the shared directory
docker run -d -it --name webd6 -v /docker/shared:/shared centos
- attach to the container
docker attach webd6
- create a file in /shared
touch /shared/bfile
- list the contents of /shared
ls /shared
afile bfile
- exit the container and check /docker/shared
[root@f4ba93a953c3 /]# exit
ls /docker/shared
afile bfile