(六)data-packed volume container
在上一節的例子中volume container 的資料歸根到底還是在 host 里,有沒有辦法將資料完全放到 volume container 中,同時又能與其他容器共享呢?
當然可以,通常我們稱這種容器為 data-packed volume container,其原理是將資料打包到鏡像中,然后通過 docker managed volume 共享,
我們用下面的 Dockfile 構建鏡像:
root@cuiyongchao:~/dockerfile/test01# pwd
/root/dockerfile/test01
root@cuiyongchao:~/dockerfile/test01# ls
Dockerfile htdocs
root@cuiyongchao:~/dockerfile/test01# cat Dockerfile
FROM busybox:latest
ADD htdocs /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocs
root@cuiyongchao:~/dockerfile/test01#
ADD 將靜態檔案添加到容器目錄 /usr/local/apache2/htdocs,
VOLUME 的作用與 -v 等效,用來創建 docker managed volume,mount point 為 /usr/local/apache2/htdocs,因為這個目錄就是 ADD 添加的目錄,所以會將已有資料拷貝到 volume 中,
build 新鏡像 datapacked:
root@cuiyongchao:~/dockerfile/test01# docker build -t datapack .
Sending build context to Docker daemon 3.584kB
Step 1/3 : FROM busybox:latest
---> f0b02e9d092d
Step 2/3 : ADD htdocs /usr/local/apache2/htdocs
---> 028540f701e4
Step 3/3 : VOLUME /usr/local/apache2/htdocs
---> Running in 0f14a2d71a20
Removing intermediate container 0f14a2d71a20
---> 2de846cade1b
Successfully built 2de846cade1b
Successfully tagged datapack:latest
root@cuiyongchao:~/dockerfile/test01#
用新鏡像創建 data-packed volume container:
root@cuiyongchao:~/dockerfile/test01# docker create --name vc_data datapack
81a3830bbc5eed741a0ca1a87a932187f9c42fbbefb118b4a18b3dbde4237701
root@cuiyongchao:~/dockerfile/test01# docker run -d -p 80 --volumes-from vc_data httpd
0b53eab130a061b6810e82fa1130026e48c69ab870660e4acc20b8248b845a55
root@cuiyongchao:~/dockerfile/test01# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b53eab130a0 httpd "httpd-foreground" 12 seconds ago Up 11 seconds 0.0.0.0:32778->80/tcp eager_murdock
因為在 Dockerfile 中已經使用了 VOLUME 指令,這里就不需要指定 volume 的 mount point 了,啟動 httpd 容器并使用 data-packed volume container:
root@cuiyongchao:~/dockerfile/test01# curl 10.0.0.20:32778
this updata secondy!
root@cuiyongchao:~/dockerfile/test01#
容器能夠正確讀取 volume 中的資料,data-packed volume container 是自包含的,不依賴 host 提供資料,具有很強的移植性,非常適合 只使用 靜態資料的場景,比如應用的配置資訊、web server 的靜態檔案等,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/228762.html
標籤:其他
