Published 6月 01, 2019 by with 0 comment

5 - 01 - Data Volume (Bind Mount)




Data volume: In Docker host, the folder or document can be mount to container file system.
There are two types data volume: bind mount and docker managed volume.
This lab will introduce the bind mount.

1. Let's create a file index.html at /home/peter/htdocs
peter@peter-KVM:~$ mkdir htdocs
peter@peter-KVM:~$ cd htdocs/
peter@peter-KVM:~/htdocs$ echo 'Version1' > index.html
peter@peter-KVM:~/htdocs$ 
peter@peter-KVM:~/htdocs$ cat /home/peter/htdocs/index.html 
Version1
peter@peter-KVM:~/htdocs$ 

2. Run a container httpd and bind mount the index.html file.
peter@peter-KVM:~$ docker run -d -p 80:80 -v ~/htdocs:/usr/local/apache2/htdocs httpd
c5dac4450290d19c4e9cc083a52a0bf41c50f2ce8b7cb58e62a0d4f6e6ebcb90
peter@peter-KVM:~$ 

3. Verify, we will see the container file is replaced by docker host file.
peter@peter-KVM:~$ curl 127.0.0.1:80
Version1
peter@peter-KVM:~$ 

4. Changing the host file and the container file will be changed too.
peter@peter-KVM:~$ echo "Update version2" > /home/peter/htdocs/index.html 
peter@peter-KVM:~$ 
peter@peter-KVM:~$ curl 127.0.0.1:80
Update version2
peter@peter-KVM:~$ 

5. Stop an remove the container, the docker host file still exist.
peter@peter-KVM:~$ docker stop c5dac4450290d19c
c5dac4450290d19c
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker rm c5dac4450290d19c
c5dac4450290d19c
peter@peter-KVM:~$ 
peter@peter-KVM:~$ cat /home/peter/htdocs/index.html 
Update version2
peter@peter-KVM:~$ 


Reference:
1. Use bind mounts
https://docs.docker.com/storage/bind-mounts/


最初發表 / 最後更新: 2019.06.01 / 2019.06.01

0 comments:

張貼留言