初次學習 Docker Volume 的基本使用 (四)

coding01發表於2019-02-21

group_5622_0
group_5622_0

在很早的一篇帖子裡 dockone.io/question/24 就有人問:「請教下程式碼放在 Docker 裡面還是外面呢」多數人評論類似下面的觀點:

由於開發環境程式碼一直在變動,而且多人通過 git 協作,於是程式碼都是放在外面,構建一個執行環境的 image,然後程式碼部分用 volume 對映進去,方便隨時調整。

我的觀點也是這樣的,目前我學習的 docker 更多的是本地開發使用,還未到測試或者真實環境下部署的時候,所以我目前贊同將 docker 作為部署開發環境使用,然後將程式碼和資料庫用 volume 對映到容器中。

所以今天的文章話題是:學習 Docker Volume

Docker Volume

A volume is a specially-designated directory within one or more containers that bypasses the Union File System. Volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically delete volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container. Also known as: data volume

There are three types of volumes: host, anonymous, and named:

  • A host volume lives on the Docker host’s filesystem and can be accessed from within the container.

  • A named volume is a volume which Docker manages where on disk the volume is created, but it is given a name.

  • An anonymous volume is similar to a named volume, however, it can be difficult, to refer to the same volume over time when it is an anonymous volumes. Docker handle where the files are stored.

Docker Volume 掛載

主要有兩種引數方式掛載,一種是 -v,另一種是建立資料卷容器,以--volumes-from 掛載。

-v 方式掛載

-v [host-dir]:[container-dir]:[rw|wo]複製程式碼

其中,

· host-dir:表示主機上的目錄,如果不存在,Docker 會自動在主機上建立該目錄。
· container-dir:表示容器內部對應的目錄,如果該目錄不存在,Docker 也會在容器內部建立該目錄。
· rw|ro:用於控制卷的讀寫許可權。

所以[host-dir]:[container-dir] 一共就有四種組合,其中 container-dir 有沒有存在,先不做嘗試考慮。

一、假如不指定 host-dir,我們看看:

docker run -it -p 8890:8080 --rm -v /usr/local/tomcat/webapps --name test1 tomcat:8.0複製程式碼

接著使用檢視容器中掛載資料卷的情況:

docker inspect test1複製程式碼

這時候看到的掛載的路徑是臨時的;而容器中對應的目錄,也沒有被覆蓋:

二、假如指定了 host-dir,我們來看看:

docker run -it -d -p 8891:8080 --rm -v /Users/ye/docker/learning/javademo/volume2:/usr/local/tomcat/webapps --name test2 tomcat:8.0複製程式碼

接著使用檢視容器中掛載資料卷的情況:

docker inspect test2複製程式碼

可以看出,將主機本地的資料夾掛在上去了:

這時候我們可以看到,在容器中對應的目錄下的檔案,和主機目錄下的保持一致了

容器中的檔案
容器中的檔案

如果在主機中增加一個檔案 world.java,我們再看看:

保持一致了!

--volumes-from 掛載

很多時候,我們會將一些相關的容器部署到同一個主機上,這時候希望這些容器之間可以共享一些資料。這時,我們可以建立一個資料卷容器,然後就可以供多個容器掛載使用了。

這裡我就不繼續往下進行闡述了,因為我學到 Docker Volume 還沒真正使用過資料卷容器,所以沒有發言權,等我使用過了,我將補充這方面的學習內容。

資料卷操作命令

主要有create、inspect、ls、prune、rm這幾個命令,其中拿 ls 舉個例子。

docker volume ls

List volumes
顯示所有資料卷

命令:

docker volume ls [OPTIONS]複製程式碼

如:

其中:[OPTIONS] 命令:

Name, shorthand Default Description
--filter, -f Provide filter values (e.g. ‘dangling=true’)
--format Pretty-print volumes using a Go template
--quiet, -q false Only display volume names

具體其它的幾個個命令,都比較簡單:

Command Description
docker volume create Create a volume
docker volume inspect Display detailed information on one or more volumes
docker volume ls List volumes
docker volume prune Remove all unused volumes
docker volume rm Remove one or more volumes

更多參考官網說明:docs.docker.com/engine/refe…

總結

雖然在學習過程中,發現使用 -v 的掛載方式要多於使用資料卷容器的方式,主要是因為在本地學習為主,或者以單專案開發為主。但在現實產品開發中,我相信用--volume from 的方式會很多,尤其是生產環境下。有待於我們繼續學習,也希望有人能提點我~~~,萬謝!


coding01 期待您關注

qrcode
qrcode


也很感謝您能看到這了

qrcode
qrcode

相關文章