Thursday, November 23, 2023

Docker Volumes and Bind Mounts -9

Volumes is nothing  but the docker storage

Docker has multiple storage drivers that allow one to work with the underlying storage devices. 


TechnologyStorage Driver
OverlayFSoverlay or overlay2
AUFSaufs
Btrfsbrtfs
Device Managerdevicemanager
VFSvfs
ZFSzfs



In Docker, you have a separate volume that can shared across containers. These are known as data volumes

features of data volume are −

  • They are initialized when the container is created.
  • They can be shared and also reused amongst many containers.
  • They exist even after the container is deleted.
docker info 
The command will provide all relative information on the Docker component installed on the Docker Host.







Listing all the Volumes

You can also list all the docker volumes on a docker host.

docker volume ls

The command will output all the volumes on the dockerhost





Creating a Volume

docker volume create –-name=volumename –-opt options






Let me go inside the volume folder





docker inspect is used to see the details of this image




By default it creates _data folder inside the IBM Volume.




Now, i want to add some file in this volume. 




I want to create a text editor so we need to use "vi"


Once,  you click on Enter, the below screen gets displayed.
Type letter i, to enter the details





After entering the below details , and the last we will be
using linux cmd that is ":wq!" , to go back to prev screen





Then, it will exit from that window and go back to prev scren
Now, Type ls to check the details inside that folder as shown below





To see, the details, use the below command
 cat project.details.txt
Now, let us create one container and attach that volume

docker run -idt --name 007test1 -v IBM:/IBM nginx

(Source/Destination)
I am taking the entire folder .














Run docker ps 






Now, we need to go inside that container. So run the below command.

docker exec -it 007test1 bash






Run ls 











After running cat cmd, i was able to see my details









If we want to create a directory also we can create. 







Now exit from that 



Let me create one more container with the same volume. 
docker run -idt --name rose -v IBM:/tmp/tmpnginx nginx:latest 
Next run docker ps command


Let us run that container by using exec command










Let me run the cat command






Let me create few folders inside this, with any names as shown below




Let me go to docker ps again




Now, both the containers are having different data, let me check the volumes





Now, let me go to the container



Now, let us delete those two containers and check, what will happens to my volumes
docker rm -f 62f0b9dd126f 817110eec58f







Now, those two containers have been deleted. But my data still exists. Let us check 



Now, again we can create a new container and have that volume of data  inside that container
docker run -idt --name starfish -v IBM:/var/ibm bash 








In the same way, we can create a container ,and assign to that particular volume
Next, I am going  to delete all the containers and check, if my volume data is lost or not
Than also i can see my volume of data.








Let me check the Volumes by running the command docker volume ls

 
We also have a  command docker volume prune , which will delete the volumes.




We can also delete the volume by using the rm command.



Folder Sharing
Let me first create a directory and add some sample txt file inside it.
Now, i have created two files 1 and 2 








Now, i want to create a container and attached this folder to it.
docker run -idt --name ab123 -v  /root/mystore:/mystore nginx:latest





Now, i am inside this container

























Exit from that

I am able to read the data, outside container . the file which i have created inside 
container, i am able to view it




Let me delete the container.
























Now, i have deleted the container, then also i can access the shared folder data.













FYI

By Default all the data of the container are stored in the writable container layer

This data only exists, while the container is active. If the container no longer exists or 
deleted  the data is also deleted along with the container.

Data on the writable layer in the container is written using a storage driver.

The writable container layer is tightly coupled to the host machine.

















Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...