042、用volume container 共享資料 (2019-03-05 週二)

三角形發表於2019-03-05
 
volume container 是專門為其他容器提供 volume 的容器,他提供的卷也可以是bind mount ,也可以是docker managed volume
 
建立容器vc_data,因為它只負責提供資料,無需執行,docker create即可。
容器mount了兩個volume,bind mount用來存放webserver的靜態檔案,docker managed volume用來存放一些實用工具
 
[root@docker-lab ~]# cat ~/htdocs/index.html
This a test HTML file
[root@docker-lab ~]# docker create --name vc_data -v ~/htdocs:/usr/local/apache2/htdocs -v /other/useful/tools busybox
6c12ead74cd754c2da6e0e801b798a93d38965d84fc2eea9c120bcf627e99560
[root@docker-lab ~]# docker inspect vc_data
 
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/root/htdocs",
                "Destination": "/usr/local/apache2/htdocs",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696",
                "Source": "/var/lib/docker/volumes/14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696/_data",
                "Destination": "/other/useful/tools",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ]
 
其他容器可以使用 --volumes-from 使用vc_data 這個volume container
 
[root@docker-lab ~]# docker run --name web01 -d -p 80 --volumes-from vc_data httpd
90feafa492fe582ba97072e660a5661587b14bd40de60d6b78d14a3127294328
[root@docker-lab ~]# docker run --name web02 -d -p 80 --volumes-from vc_data httpd
a04615d5f438bc609ce35e04c2a3d76bb6df78e627d4136cf56e83b18d794517
[root@docker-lab ~]# docker run --name web03 -d -p 80 --volumes-from vc_data httpd
88613c4f0e6cd112a359bf68dfe515e7469883ea76f2df85f37c022f84e5fd63
 
[root@docker-lab ~]# docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                   NAMES
88613c4f0e6c        httpd               "httpd-foreground"   3 seconds ago       Up 2 seconds        0.0.0.0:32770->80/tcp   web03
a04615d5f438        httpd               "httpd-foreground"   8 seconds ago       Up 7 seconds        0.0.0.0:32769->80/tcp   web02
90feafa492fe        httpd               "httpd-foreground"   13 seconds ago      Up 12 seconds       0.0.0.0:32768->80/tcp   web01
 
[root@docker-lab ~]# docker inspect web01 -f '{{.NetworkSettings.IPAddress}}'
172.17.0.2
[root@docker-lab ~]# docker inspect web02 -f '{{.NetworkSettings.IPAddress}}'
172.17.0.3
[root@docker-lab ~]# docker inspect web03 -f '{{.NetworkSettings.IPAddress}}'
172.17.0.4
 
[root@docker-lab ~]# curl http://127.0.0.1:32768
This a test HTML file
[root@docker-lab ~]# curl http://127.0.0.1:32769
This a test HTML file
[root@docker-lab ~]# curl http://127.0.0.1:32770
This a test HTML file
 
[root@docker-lab ~]# docker inspect vc_data -f '{{.Mounts}}'
[
    {bind  /root/htdocs /usr/local/apache2/htdocs   true rprivate} 
    {volume 14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696 /var/lib/docker/volumes/14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696/_data /other/useful/tools local  true }
]
[root@docker-lab ~]# docker inspect web01 -f '{{.Mounts}}'
[
    {bind  /root/htdocs /usr/local/apache2/htdocs   true rprivate} 
    {volume 14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696 /var/lib/docker/volumes/14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696/_data /other/useful/tools local  true }
]
[root@docker-lab ~]# docker inspect web02 -f '{{.Mounts}}'
[
    {bind  /root/htdocs /usr/local/apache2/htdocs   true rprivate} 
    {volume 14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696 /var/lib/docker/volumes/14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696/_data /other/useful/tools local  true }
]
[root@docker-lab ~]# docker inspect web03 -f '{{.Mounts}}'
[
    {bind  /root/htdocs /usr/local/apache2/htdocs   true rprivate} 
    {volume 14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696 /var/lib/docker/volumes/14ae5efb5762a333bcceb26503ca974122d67cf31ed68da1b22773065b6fa696/_data /other/useful/tools local  true }
]
 
上面可以看到三個web容器都成功的共享了 volume container 中的volume。
 
volume container 特點:
 
1、與bind mount 相比,不必為每一個容器指定host path,所有path都在volume container的容器中定義,容器只需要與volume container關聯,實現了容器與host的解耦
 
2、使用volume container 的容器其mount point是一致的,有利於配置和的規範和標準化,也帶來一定的侷限性,使用時需要綜合考慮
 
 

相關文章