Saturday, April 18, 2020

Docker Assignments #4: Docker Volume


  1. Create docker volume let us name it as 'testvolume'
  2. List all available volumes in the host machine
  3. Run an alpine container (name it as 'alpine1') in interactive-mode by attaching volume (i.e. 'testvolume' ) to the container 'home' folder
  4. Enter into “alpine1” container; go to home folder and create a file called “testfile.txt” with sample text; exit from alpine1 container
  5. Run an alpine container (name it as 'alpine2' ) in interactive-mode by attaching same volume i.e. 'testvolume' to the container 'home' folder
  6. Enter inside “alpine2” container; go to home folder and check the file “testfile.txt” with content ; exit from alpine2 container
  7. Delete alpine1 and alpine2 containers
  8. Run an alpine container (name it as 'alpine3' ) in interactive-mode by attaching same volume i.e. 'testvolume' to the container 'home' folder
  9. Enter into “alpine3” container; go to home folder and check the file “testfile.txt” with content ; exit from alpine3 container
  10. Observe and experience the benefits of Docker Volume
Step 1:
     > docker volume create testvolume    

Step 2: 
     > docker volume ls 

Step 3:      
> docker run -it  
-v testvolume:/home 
  --name alpine1 alpine sh (or)
> docker run -it 
  --mount type=volume,source=testvolume,target=/home 
  --name alpine1 alpine sh 

Step 4: 
     /# cd home
     /home # echo "hello world" > testfile.txt

Step 5:     
> docker run -it  
-v testvolume:/home 
  --name alpine2 alpine sh  

Step 6: 
     /# cd home
     /home # ls

Step 7: 
     > docker rm -f alpine1 alpine 2

Step 8: 
     > docker run -it -v testvolume:/home --name alpine3 alpine sh

Step 9: 
     /# cd home
     /home # ls

Step 10: 
  1. Volumes are easier to back up or migrate than bind mounts
  2. Volumes work on both Linux and Windows containers
  3. Volumes can be stored on remote hosts or cloud providers
  4. Volumes are managed by Docker. So, user can't be alter the content directly
Happy Coding :)


No comments:

Post a Comment