In this tutorial we are going to see how we can mount a docker volume and share among multiple containers. Data shared inside this mount points remains persistent, Even after deleting Docker containers.
So lets try to understand this concept practically.
Create a new volume and inspect its Mount Point.
[root@siddhesh ~]# docker volume create sid-vol
sid-vol
[root@siddhesh ~]# docker volume inspect sid-vol
[
{
"CreatedAt": "2020-04-11T06:20:57Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/sid-vol/_data",
"Name": "sid-vol",
"Options": {},
"Scope": "local"
}
]
[root@siddhesh ~]#
Now lets try to mount this volume under Docker Container.
[root@siddhesh ~]# docker run -it -v sid-vol:/data --name centos-container centos
[root@09e003bf7026 /]# df -h /data
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 4.7G 248M 4.5G 6% /data
[root@09e003bf7026 /]#
As you can see here now /data partition under Centos Container of docker.
Now lets create one folder and file to see if it shows the same under another container of docker.
[root@09e003bf7026 /]# echo "instance1_vol1" > /data/file1.txt
[root@09e003bf7026 /]# mkdir /data/instance1
Now I'll mount same data volume under different container ie ubuntu to see If I can see data created through Centos container.
[root@siddhesh ~]# docker run -it -v sid-vol:/data --name ubuntu-container ubuntu
root@1f0c8e2a7ff4:/# df -h /data/
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 4.7G 474M 4.2G 10% /data
root@1f0c8e2a7ff4:/# ls -lhrt /data/
total 8.0K
-rw-r--r-- 1 999 root 15 Apr 11 06:30 file1.txt
drwxr-xr-x 2 999 root 6 Apr 11 06:30 instance1
-rw-r--r-- 1 999 999 92 Apr 11 06:34 dump.rdb
root@1f0c8e2a7ff4:/#
Here you can see file & folder under Ubuntu which was created through Centos container.
This data will be remain safe even after deletion of docker container. This is one of important aspect of Docker we all must understand when you want to store important production data.
Thank you. It's really great !!