Published 6月 01, 2019 by with 0 comment

5 - 02 - Data Volume (Docker Managed Volume)



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 docker managed volume.

1. create a container with -v which means docker need a data volume and mount to contaioner /usr/local/apache2/htdocs
peter@peter-KVM:~$ docker run -d -p 80:80 -v /usr/local/apache2/htdocs httpd
8bbe3bdc4a28133115e4935b57ef1abe13bb48d08f7b40645f32db39dc73f2a2
peter@peter-KVM:~$ 

2. Verify the container mount.
peter@peter-KVM:~$ docker inspect 8bbe3bdc4a
        "Mounts": [
            {
                "Type": "volume",
                "Name": "072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce",
                "Source": "/var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data",
                "Destination": "/usr/local/apache2/htdocs",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }

3. Checking the docker host folder content.
peter@peter-KVM:~$ sudo ls -la /var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data
[sudo] password for peter: 
total 12
drwxr-sr-x 2 root www-data 4096 Jun  1 22:33 .
drwxr-xr-x 3 root root     4096 Jun  1 22:33 ..
-rw-r--r-- 1 root src        45 Jun 12  2007 index.html
peter@peter-KVM:~$ 
peter@peter-KVM:~$ sudo cat /var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data/index.html
### Remove "*" ###
<*html><*body><*h1>It works!<*/h1><*/body><*/html>
peter@peter-KVM:~$ 

4. Verify th container content, I found the docker host content same as container.
peter@peter-KVM:~$ curl 127.0.0.1:80
### Remove "*" ###
<*html><*body><*h1>It works!<*/h1><*/body><*/html>
peter@peter-KVM:~$ 
5. Editing the docker host file, it will update to container file
peter@peter-KVM:~$ sudo bash -c 'echo "Update Version2" > /var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data/index.html'
peter@peter-KVM:~$  
peter@peter-KVM:~$ sudo cat /var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data/index.html
Update Version2
peter@peter-KVM:~$ 
peter@peter-KVM:~$ curl 127.0.0.1:80
Update Version2
peter@peter-KVM:~$ 

6. In to container and editing the container file, it also will update to docker host file.
peter@peter-KVM:~$ docker exec -it 8bbe3bdc4a28 sh
# 
# echo "Update Version3" > /usr/local/apache2/htdocs/index.html
# 
# cat /usr/local/apache2/htdocs/index.html
Update Version3
# 
# exit
peter@peter-KVM:~$ sudo cat /var/lib/docker/volumes/072c518d11c157b22fa331dac6e5a21cbfeb110b43b6f00bffadbf02c61358ce/_data/index.html
Update Version3
peter@peter-KVM:~$ 


Reference:
1. Use volumes
https://docs.docker.com/storage/volumes/


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

0 comments:

張貼留言