docker 實踐(八)docker 主機共享volume

一、掛載host volume到容器

1.1.指定掛載本地資源到容器

1.1.1.本地文件

# cat index.html 
There is nothing here

1.1.2.創建容器,掛載本地資源

# docker run -d -p 80:80 -v ~/index.html:/usr/share/nginx/html/index.html --name "cklng" nginx
83826f3fdb3d940fb2fd18af4223da695758e8ac7eb73e07215c461904e161cb
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
83826f3fdb3d        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp       cklng

1.1.3.訪問容器

# curl http://127.0.0.1:80
There is nothing here
# docker inspect 83826f3fdb3d
....
 "Mounts": [
            {
                "Type": "bind",
                "Source": "/root/index.html",  #本地目錄文件
                "Destination": "/usr/share/nginx/html/index.html",  #容器目錄文件
                "Mode": "", 
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
....

1.2.不指定本地資源掛載到容器

1.2.1.運行容器,不指定本地資源

# docker run -d -p 80:80 -v /usr/share/nginx/html/ --name "cklng1" nginx                       
8d8fc0a101a03bc7b098f0838e9583cf2a673ecbc6c543739cf2f2803bb713e3

1.2.2.訪問容器

# curl http://127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

1.2.3.查看容器詳情

# docker inspect 8d8fc0a101a0
...
"Mounts": [
            {
                "Type": "volume",
                "Name": "af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef",
                "Source": "/var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data",  #主機資源路徑
                "Destination": "/usr/share/nginx/html",  #掛載到容器的目錄
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
....
#ls /var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data/
50x.html    index.html

#容器生產一個隨機目錄作爲mount的源

#查看mount源內容:

# cat /var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data/index.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

1.2.3.查看volume詳情

# docker volume ls
DRIVER              VOLUME NAME
local               af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef
# docker volume inspect af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef
[
    {
        "CreatedAt": "2018-12-19T01:05:34-05:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data",
        "Name": "af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef",
        "Options": null,
        "Scope": "local"
    }
]

修改volume內容:

# cat /var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data/index.html    
THIS IS NO NO NO PANGE!

訪問容器:

# curl http://127.0.0.1:80
THIS IS NO NO NO PANGE!

進入容器查看:

# docker exec -it 8d8fc0a101a0 /bin/bash
root@8d8fc0a101a0:/# cat /usr/share/nginx/html/index.html 
THIS IS NO NO NO PANGE!

1.2.4.容器停止後volume

# docker stop 8d8fc0a101a0
8d8fc0a101a0
# docker rm 8d8fc0a101a0
8d8fc0a101a0
# cat /var/lib/docker/volumes/af5af5d925c4e4109d5d9ab4ff9112bee5ec56b1dfc64ce1294e23ed458c50ef/_data/index.html    
THIS IS NO NO NO PANGE!

#host文件依然存在


使用-v 可以刪除本地掛載資源

# docker rm -v 71b9d9f74d2a

2.容器共享數據

2.1.如上共享,掛載本地的同一個volume

2.1.1.創建三個容器,掛載同一個目錄

# docker run -d -p 80 -v ~/index.html:/usr/share/nginx/html/index.html --name "cklng1" nginx
75cb89bb9430ed47022c76a854c64cdc043571d8be0215d7223355713fb9c078
[root@localhost ~]# docker run -d -p 80 -v ~/index.html:/usr/share/nginx/html/index.html --name "cklng2" nginx 
4e8699f856f0709088b1e24effb9f38b939018f6af9880c1a988a2e42fb13c35
[root@localhost ~]# docker run -d -p 80 -v ~/index.html:/usr/share/nginx/html/index.html --name "cklng3" nginx 
46c536bc8eeacb073f84cccef9c217edd8196e431b033f8c1182233aa02731df

容器進程:

# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
46c536bc8eea        nginx               "nginx -g 'daemon of…"   5 seconds ago       Up 3 seconds        0.0.0.0:32771->80/tcp    cklng3
4e8699f856f0        nginx               "nginx -g 'daemon of…"   8 seconds ago       Up 7 seconds        0.0.0.0:32770->80/tcp    cklng2
75cb89bb9430        nginx               "nginx -g 'daemon of…"   14 seconds ago      Up 13 seconds       0.0.0.0:32769->80/tcp    cklng1

訪問三個容器:

# curl http://127.0.0.1:32771
There is nothing here
# curl http://127.0.0.1:32770
There is nothing here
# curl http://127.0.0.1:32769
There is nothing here
#結果一致

#修改源

# echo "it's changed" > index.html 
# curl http://127.0.0.1:32769      
it's changed
# curl http://127.0.0.1:32770
it's changed
# curl http://127.0.0.1:32771
it's changed


2.2.創建一個volme容器

參數說明:

--volumes-from Mount volumes from the specified container(s) #掛載volume從指定的容器

2.2.1.先創建一個volume容器

# docker create --name ckl_data -v ~/index.html:/usr/share/nginx/html/index -v /data/ckl/testdir centos
a1bb53fa787c470611e5ebd9fd06c399447f3fdf18442eba08ae1b745d63a70c

#查看創建容器volume詳情:

# docker inspect a1bb53fa787c
...
"Mounts": [
            {
                "Type": "bind",
                "Source": "/root/index.html",  #掛載源1
                "Destination": "/usr/share/nginx/html/index",  #掛載點1
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "e78b4894d0f13c2ab68d362728dd101dc3b3a55666dd5978a204f30731b4f9a6",
                "Source": "/var/lib/docker/volumes/e78b4894d0f13c2ab68d362728dd101dc3b3a55666dd5978a204f30731b4f9a6/_data", #隨機掛載源2
                "Destination": "/data/ckl/testdir",  #掛載點2
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
....


2.2.2.創建三個容器,指定需要掛載的容器

# docker run -d --name "cklcent1" --volumes-from ckl_data nginx 
480ebd88ea200f23bb0f0ac336fc7183a81832bdfcd40ea02bc2902fd980ae3d
# docker run -d --name "cklcent2" --volumes-from ckl_data nginx 
9e4a4095722595dc196b5e39c10ee970067855970e69ef571ad0e2a118f461a2
# docker run -d --name "cklcent3" --volumes-from ckl_data nginx 
0a58aea9bd47895d9cf8c000f9d1f604f14970c47b1cb72e5d0f2015568de3d1

2.2.3.查看容器內容:

# docker exec -it 0a58aea9bd47 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir"
it's changed
# docker exec -it 9e4a40957225 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir"            
it's changed
# docker exec -it 480ebd88ea20 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir"            
it's changed


進入掛載源,新增文件,測試:

# cd /var/lib/docker/volumes/e78b4894d0f13c2ab68d362728dd101dc3b3a55666dd5978a204f30731b4f9a6/_data
[root@localhost _data]# touch ckl.log

#已經共享的目錄,文件一致

#  docker exec -it 0a58aea9bd47 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir"
it's changed
ckl.log
# docker exec -it 9e4a40957225 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir" 
it's changed
ckl.log
#  docker exec -it 480ebd88ea20 /bin/bash -c "cat  /usr/share/nginx/html/index && ls /data/ckl/testdir" 
it's changed
ckl.log


#也可以製作一個容器掛載鏡像,文件放在容器裏,新創建的容器指定掛載容器,不依賴於主機。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章