Docker - 4

 Docker volume and how to share it

----------------------------------

-> Volume is simply a directory inside our container.

-> Firstly, we have to declare the directory as a volume and then share the volume.

->Even if we stop container, still we can access volume.

-> volume will be created in one container.

-> you can declare a directory as a volume only while creating conatiner.

-> you can not create volume in existing container.

->  you can share one volume across any number of containers.

-> volume will not be included when you update an image.

you can mapped volume in two ways

1. Container <---> container

2. Host <-----> container.

=======================================================================

1)Docker create( to create the container)

2)Docker start(to run the container)

3)Docker run( to create and start the container)


benefits of volume

-----------------

-> Decoupling container from storage

->share volume among different containers

-> Attach volume to containers

-> on deleting container volume does not delete.


Creating volume from Dockerfile.

==============================

1. Create a Dockerfile and Write


FROM ubuntu

VOLUME ["/myvolume1"]


then create image from this dockerfile


docker build -t myimage .


Now create a container from this image & run


docker run -it --name container1 myimage /bin/bash


Now do ls, you can set myvolume1



Now share volume with another container

 

        Container1 <-------> container2

docker run -it --name container2 --privileged=true --volumes-from container1 ubuntu /bin/bash

                       (new)    (old)


Now after creating container2, myvolume1  is visible whatever you do in one volume, can see from other volume


touch /myvolume1/samplefile

docker start container1

docker attach container1

ls /myvolume1


you can see samplefile here.

exist

==================================================


Now, try to create volume by using command

===========================================


docker run -it --name container3 -v /volume2 ubuntu /bin/bash 


do ls --> cd /volume2

Now create one file cont3file and exist


Now create one more container, and share volume2


-> docker run -it --name container4 --privileged=true --volumes-from container3 ubuntu /bin/bash


Now you are inside container, do ls , you can see volume2


Now create one file inside this volume and then check in container 3, you can see that file.


==================================================================================================


Volume (Host - Container)


-> verify files in  /home/jatin


-> docker run -it --name hostcont -v /home/jatin/rajput --privileged=true ubuntu /bin/bash


-> cd /rajput


Do ls, now you can see all files of host machine.


-> touch rajputfile (in container)

exist


Now check in host machine , you can see the files


see other commands

------------------

--> docker volume ls

--> docker volume create <volumename>

--> docker volume rm <volumename>

--> docker volume prune

{it removed all unused docker volume}

-> docker volume inspect <volumename>

-> docker container inspect <containername>

Comments

Popular posts from this blog

GIT - 3

Docker - 6

GIT - 1