We can use volumes with docker for persistent storage.To create a volume use command
#docker volume create mytestvol
This will create a volume named mytestvol. To list it
#docker volume ls
We can see our volume mytestvol is listed in it. To inspect that
#docker volume inspect mytestvol
The volumes that we create will be created inside /var/lib/docker/volumes.Go inside that folder and we can see that our volume mytestvol is present there
To delete a volume
#docker volume rm mytestvol2
Here mytestvol2 is the new volume that we created.
Now we will check how to mount a volume to a container.
#docker container run -dit --name mygoof --mount source=volpool,target=/vol centos:centos6
Here docker will run a new centos6 container with a new volume named volpool attached to /vol directory of centos container.Here docker will create a new volume automatically.Now check
#docker volume ls
We can see new volume volpool is created
#docker ps
We can see our container named mygoof running
Now to inspect volume we can login to container
#docker container exec -it mygoof /bin/bash
Now move inside /vol and create a file with any content in it.
#cd /vol
#echo "hai robo" > test
After that exit the container and view it inside /var/lib/docker/volumes/volpool/_data
#cd /var/lib/docker/volumes/volpool/_data
#cat test
We can see test file with same content here.
Volumes are independent of container.That means if we delete a container,the volume attached to it won't gets deleted.We can mount these volume to another container and use it over there.
Also we can't delete a mounted volume without removing the container attached to it.
#docker container rm mygoof
#docker volume rm volpool
Now the volume will be deleted
#docker volume create mytestvol
This will create a volume named mytestvol. To list it
#docker volume ls
We can see our volume mytestvol is listed in it. To inspect that
#docker volume inspect mytestvol
The volumes that we create will be created inside /var/lib/docker/volumes.Go inside that folder and we can see that our volume mytestvol is present there
To delete a volume
#docker volume rm mytestvol2
Here mytestvol2 is the new volume that we created.
Now we will check how to mount a volume to a container.
#docker container run -dit --name mygoof --mount source=volpool,target=/vol centos:centos6
Here docker will run a new centos6 container with a new volume named volpool attached to /vol directory of centos container.Here docker will create a new volume automatically.Now check
#docker volume ls
We can see new volume volpool is created
#docker ps
We can see our container named mygoof running
Now to inspect volume we can login to container
#docker container exec -it mygoof /bin/bash
Now move inside /vol and create a file with any content in it.
#cd /vol
#echo "hai robo" > test
After that exit the container and view it inside /var/lib/docker/volumes/volpool/_data
#cd /var/lib/docker/volumes/volpool/_data
#cat test
We can see test file with same content here.
Volumes are independent of container.That means if we delete a container,the volume attached to it won't gets deleted.We can mount these volume to another container and use it over there.
Also we can't delete a mounted volume without removing the container attached to it.
#docker container rm mygoof
#docker volume rm volpool
Now the volume will be deleted