By using a bind mount, a file or directory on the host machine is mounted into a container
- Create a folder structure as per above image in your host machine
- Run an alpine container (name it as 'alpine1') in interactive-mode and make a note of all files/folders under container "usr" folder
- delete alpine1 container
- 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
- Make a note of all files/folders under "usr" folder of 'alpine1' container
- Observe what happens to container built-in folder/files under “usr” folder
- Change the content in file1.txt file under /usr/folder1/file1.txt
- Check file1.txt whether changes are reflecting in your host machine or not
- Change the content in file2.txt file under docker-bindmount/folder2/file2.txt from host machine
- Check whether same changes are reflecting under container /usr/folder2/file2.txt
- stop and remove alpine1 container
- Check whether changes are still exist in host machine
- Do bind mount to container non-exist folder
- Implement read-only bind mount
- 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:
- 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