一、挂载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


#也可以制作一个容器挂载镜像,文件放在容器里,新创建的容器指定挂载容器,不依赖于主机。