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:
Update CLI
apt-get update
Install Docker
apt-get install docker.io -y
Check Docker Status
service docker status
Here I am creating a docker file in which I will install nginx and create a volume inside a container with path /etc/Aman.
# Use the official nginx image as the base image
FROM nginx
# Create a volume inside the container at /etc/Aman
VOLUME /etc/Aman
# Expose port 80 for nginx
EXPOSE 80
# Start nginx when the container starts
CMD ["nginx", "-g", "daemon off;"]
Now create image from Docker file
docker build -t <image_name>
Now Check Image Create or Not
docker images
Now create a Container from that Images
Docker run -it --name <container_name> <image_name> /bin/bash
now check container
docker ps -a
Now start the container
docker start <container_id>
Now go inside the container
docker attach <container_id>
now create some file in myvolume and exit from conatiner
touch filex filey filez
Now create new continer from update container
docker run -it --name cont_2 --privileged=true --volumes-from cont_1 ubuntu /bin/bash
so here container is created and volume ""myvolume"" is attach is both container all th3 files are there.
Volume by command and shared between Two Container
docker run -it --name <continer_name> -v /<vomlumename> <image_name> /bin/bash
now create another container from update container
docker run -it --name <container_name> --privileged=true --volumes-from <container_name> <image_name> /bin/bash
-
Now We added some file is this container and check on con_4 file automated added or no in volumes
Now Created some file in con_3 and exit from cont_3
Now Start and Attach the Container_4 and see that changes
docker start <container_id> docker attach <container_id>
-
Now see the Change here volume is shared in each container once we update in any file its automatically updated in both volumes of both container
Shared volume by host to container
create a 3 files on your local system
touch devops aws linux
Now Create Conatiner at local Ubuntu
docker run -it --name <container_name> -v /home/ubuntu:/<volume_name> --privileged=true <image_name> /bin/bash
Now Volume is Created and all the file are in Local Ubuntu are shared with Hostvolume
Now Update on Host-Volume and see the change is Aman
+++++++++++++++++++++++++++THANK YOU++++++++++++++++++++++++