Saturday, April 18, 2020

Docker Assignments #5: Docker Bind Mount

By using a bind mount, a file or directory on the host machine is mounted into a container
  1. Create a folder structure as per above image in your host machine
  2. Run an alpine container (name it as 'alpine1')  in interactive-mode and make a note of all files/folders under container "usr" folder
  3. delete alpine1 container
  4. Run an alpine container (name it as 'alpine1') in interactive-mode by doing bind mount (i.e. “dokcer-bindmount” folder from the host machine ) to container “usr” folder
  5. Make a note of all files/folders under "usr" folder of 'alpine1' container
  6. Observe what happens to container built-in folder/files under “usr” folder
  7.  Change the content in file1.txt file under /usr/folder1/file1.txt
  8. Check file1.txt whether changes are reflecting in your host machine or not
  9. Change the content in file2.txt file under docker-bindmount/folder2/file2.txt from host machine
  10. Check whether same changes are reflecting under container /usr/folder2/file2.txt
  11. stop and remove alpine1 container
  12. Check whether changes are still exist in host machine
  13. Do bind mount to container non-exist folder
  14. Implement read-only bind mount
  15. Observe the Disadvantage of doing bind mount to container non-empty/built-in folder
Step 2:
     > docker run -it  --name alpine1 alpine sh  

Step 3: 
     > docker rm -f alpine1

Step 4:    
> docker run -it
  --mount type=bind,source=/c/Users/rama/docker-bindmount,target=/usr 
  --name alpine1 alpine sh  (or)
> docker run -it 
  --mount type=bind,source=$(pwd),target=/usr 
  --name alpine1 alpine sh

Step 6: 
     container preexist contents will be hidden and not accessible to host machine

Step 7: 
     append "hello world" message to file1.txt
     > docker exec -it alpine1 sh
     > / # cd /usr/folder1
     > /usr/folder1 # echo "hello world" >> file1.txt

Step 8: 
     file1.txt changes should be reflected in host machine

Step 10: 
     file2.txt changes should be reflected in alpine1 container

Step 11: 
     > docker rm -f alpine1

Step 12: 
     all file changes must remain same

Step 13: 
    directory will be created automatically if not exist in the container   
> docker run -it
  --mount type=bind,source=/c/Users/rama/docker-bindmount,target=/rama 
  --name alpine1 alpine sh  (or)

> docker run -it 
  --mount type=bind,source=$(pwd),target=/rama 
  --name alpine1 alpine sh 

Step 14: 
    By making container read-only, we can't create or modify contents in the container
> docker run -it
  --mount type=bind,source=/c/Users/rama/docker-bindmount,target=/rama,readonly 
  --name alpine1  alpine sh (or)

> docker run --read-only 
  --name alpine1 alpine touch file1.txt ( i.e. file won't be created )

Step 15: 
  1. If you bind-mount into a non-empty Linux builtin directory (i.e. bin,usr,tmp and etc ) on the container, the directory’s existing contents are obscured/hidden by the bind mount. So, this results in a non-functioning container
Happy Coding :)

No comments:

Post a Comment