什麼是volumes
volumes是一個可供一個或多個容器使用的特殊目錄,它繞過了UFS。
voulmes有什麼特點容器間可共用修改volumes即可生效修改volumes不影響映象volumes具有永續性volumes的用法引數:-v或者--mount
這兩個引數的區別:
使用-v進行卷掛載,如果Host上的卷不存在,則會自動建立。
使用--mount進行卷掛載,如果Host的卷不存在,則會報錯並退出。
我們一般推薦使用--mount,因為它更加優雅。
直接掛載test到apline容器中的/test:
檢視當前的volumes列表:
在test下建立一個test.test的檔案,並且將test掛載到另一個容器apline2的/test2下:
檢視test這個volume的詳細資訊:
root@phyger-xubuntu:/home/phyger# docker volume inspect test[ { "CreatedAt": "2020-06-26T11:29:45+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/test/_data", "Name": "test", "Options": null, "Scope": "local" }]root@phyger-xubuntu:/home/phyger#
volumes的清理
一般情況下,volumes獨立於容器的生命週期,即容器被刪除時volumes不受影響,但如果使用者想要刪除容器的時候也刪除volumes,可以在刪除容器的時候加上-v引數進行同步刪除。
你可能發現了名為test的volume還存在,為什麼沒有被刪除?這是因為我們在使用volume的時候使用了它的名稱,使用volume的名稱進行掛載的卷用docker rm -v是無法刪除的,因為docker rm -v只刪除連結數為0的匿名卷。
舉個例子:
root@phyger-xubuntu:/home/phyger# docker run -it -v /test --name alpine_test alpine sh/ # lsbin dev etc home lib media mnt opt proc root run sbin srv sys test tmp usr var/ # ^Proot@phyger-xubuntu:/home/phyger# docker volume lsDRIVER VOLUME NAMElocal a4588769d7f4a23745c632124fd308f233aac0c8be880cfedc61f5db8da7d241local nexus-datalocal nexus-etclocal nexus-optlocal testroot@phyger-xubuntu:/home/phyger#
上面VOLUME NAME為一串ID的就是匿名卷。(一般不建議使用匿名卷)
root@phyger-xubuntu:/home/phyger# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES19aea8644fe2 alpine "sh" 2 minutes ago Up About a minute alpine_testdf5b9dc452b3 sonatype/nexus3 "sh -c ${SONATYPE_DI…" 22 hours ago Up 39 minutes 0.0.0.0:8081-8082->8081-8082/tcp nexus3root@phyger-xubuntu:/home/phyger# docker rm -f -v 19aea8644fe219aea8644fe2root@phyger-xubuntu:/home/phyger# docker volume lsDRIVER VOLUME NAMElocal nexus-datalocal nexus-etclocal nexus-optlocal testroot@phyger-xubuntu:/home/phyger#
如上,匿名卷使用docker rm -v引數可以刪除掉。
最新評論