在宿主機中找一個本地文件系統之上的目錄,然後把這一目錄直接與,docker容器文件系統之上的目錄或文件,創建所謂的綁定關係,相似於硬盤掛載html
好處是,容器關閉甚至是刪除時,都不用擔憂數據丟失了nginx
兩個容器能夠共享同一個本地目錄,同時指向就能夠了web
綁定掛載卷docker
本地和容器的路徑都須要指定進行綁定,有着脫離容器生命週期的數據持久功能code
docker掛載卷server
只需在容器中指定掛載卷,在本地中會自動建立,有docker本身維護,路徑在docker inspect中能夠查看htm
[root@localhost ~]# docker run --name b2 -it -v /data busybox / # ls bin data dev etc home proc root sys tmp usr var / # echo "hello worlk" > /data/index.html / # cat /data/index.html hello worlk
新終端生命週期
[root@localhost _data]# docker inspect b2 "Mounts": [ { "Source":"/var/lib/docker/volumes/2117d576ec10fbcf375af1fe35ff56d6e5b2bffa0db784d9ad664f412d6318f1/_data", } ], [root@localhost _data]# cat /var/lib/docker/volumes/2117d576ec10fbcf375af1fe35ff56d6e5b2bffa0db784d9ad664f412d6318f1/_data/index.html hello worlk
[root@localhost ~]# docker run --name b2 -it --rm -v /data/voumes/b2:/data busybox / #
新終端ip
[root@localhost _data]# docker inspect b2 "Mounts": [ { "Type": "bind", "Source": "/data/voumes/b2", "Destination": "/data", } [root@localhost _data]# echo "<h1>Busybox httpd server.</h1>" > /data/voumes/b2/index.html [root@localhost _data]# cat /data/voumes/b2/index.html <h1>Busybox httpd server.</h1>
目錄會自動建立it
驗證數據持久鏈接,從新建立容器,宿主機路徑與容器不一樣的路徑創建關聯關係
[root@localhost ~]# docker run --name b2 -it --rm -v /data/voumes/b2:/data busybox / # exit [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd141921d661 haoran/httpd:v0.2 "/bin/httpd -f -h /d…" 3 days ago Exited (137) 3 days ago t2 162ebb705c34 nginx:1.14-alpine "nginx -g 'daemon of…" 5 days ago Exited (0) 4 days ago web1 [root@localhost ~]# docker run --name b2 -it -v /data/voumes/b2:/data/web busybox / # cat /data/web/index.html <h1>Busybox httpd server.</h1>
複製使用其餘容器的卷,爲docker run命令使用--volumes-from選項
[root@localhost ~]# docker run --name juanpeizhimoban -it -v /data/infracon/volume/:/data/web/html busybox / # 基礎底層卷模板鏡像能夠不運行 [root@localhost ~]# docker inspect -f {{.Mounts}} juanpeizhimoban [{bind /data/infracon/volume /data/web/html true rprivate}] [root@localhost ~]# docker inspect -f {{.NetworkSettings.IPAddress}} juanpeizhimoban 10.0.0.2
複製基礎底層卷模板鏡像
[root@localhost _data]# docker run --name b2 --network container:juanpeizhimoban --volumes-from juanpeizhimoban -it --rm busybox / # [root@localhost ~]# docker inspect b2 "Source": "/data/infracon/volume", "Destination": "/data/web/html", / # ip a 37: eth0@if38: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:0a:00:00:02 brd ff:ff:ff:ff:ff:ff inet 10.0.0.2/16 brd 10.0.255.255 scope global eth0 valid_lft forever preferred_lft forever