Docker Volume:
Docker volume is a persistent data storage mechanism in Docker containers that allows data to persist even after the container is destroyed or recreated. It provides a way to share data between containers and between a container and the host system.
A volume is a directory within the Docker host's file system that is accessible by one or more containers. When a container is created, a volume can be attached to it by specifying the volume name or the path to the volume in the container's configuration. Any data written to the volume from within the container will be stored in the volume on the host's file system.
Sharing Data Between Containers:
Here I am creating a docker file in which I will install nginx and create a volume inside a container with path /etc/Abhijeet.
create an image from it and run the container:
Now check your volume inside the container and also see the volume details.
Now create a file inside the container then delete that container.
Now delete this container.
Now create another container and mount that volume to save the changes.
docker run -d -v <volumeid>:/etc/Abhijeet --name <container_name> <image_name>
Now enter into this container and verify the volume which you have mounted.
Suppose you want to mount a volume into another volume without deleting the container it, you can do this with the command
docker run -it --name <new_container> --privileged=true --volumes-from <previous_containername> ubuntu /bin/bash
Sharing Data Between host machine and Containers(Part-1):
create a new volume with help of a docker file and run the docker file as done in the first part.
Create a new volume :
docker volume create my-nginx-data
Now run the container and mount this volume with the host machine.
docker run -d --name abhicont -v my-nginx-data:/etc/Abhijeet nginximage
Now Anything you change inside a container in volume /etc/Abhijeet will reflect in my-nginx-data volume in your host machine.
create a file inside a container in volume.
Now verify it inside your host machine for that first find the path of that machine in your host machine.
Now verify changes inside the volume in the host machine:
Sharing Data Between host machine and Containers(Part-2):
here we will share data between the host machine and container without creating a new volume.
First, create some files on your host machine.
now mount this local host machine with the docker container and see the changes
docker run -it --name abhicont1 -v /home/ubuntu:/abhijeet ubuntu bin/bash
in this command ": " will separate the host machine with container volume.
here you can see we have the same file in the container too.
+++++++++++++++++++++++++++THANK YOU++++++++++++++++++++++++