1. 程式人生 > >docker筆記之資料持久化(即開即用)

docker筆記之資料持久化(即開即用)

資料卷:

 將宿主機目錄掛載到容器目錄

資料卷特點:

  • 在容器啟動初始化時,如果容器使用的宿主機掛載點有資料,這些資料會拷貝到容器中
  • 資料卷可以在容器直接共享和使用
  • 可以直接對資料卷裡的內容進行修改
  • 資料卷的變化不會影響映象的更新
  • 卷會一直存在,即使掛載資料卷的容器已經刪除

命令操作:

 [[email protected] ~]# docker run -itd --name web01 -v /container_data/web:/data ubuntu
 0a63d7406e9a32590ed5d81cbba50d89632751020a765aa8261e31d685100f14
 [
[email protected]
~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a63d7406e9a ubuntu "/bin/bash" 6 seconds ago Up 4 seconds web01 [[email protected]
~]# docker inspect web01(擷取mount部分) "Mounts": [ { "Type": "bind", "Source": "/container_data/web", "Destination": "/data", "Mode": "", "RW": true, "Propagation": "rprivate" } ],

在宿主機中的掛載目錄建立一個檔案,在容器中也會有,掛載到另一臺容器中,檔案依然存在

操作命令:

 [[email protected] ~]# cd /container_data/web/
 [[email protected] web]# touch index.php
 [[email protected] web]# docker exec web01 ls /data
 index.php
 [[email protected] web]# docker run -itd --name web02 -v /container_data/web:/data ubuntu
 e8d9178cb45707d47c2dc481d32d6df7c06d63cd72eef19c146af4e0dc09c07f
 [[email protected] web]# docker exec web02 ls /data
 index.php
 [[email protected] web]# 

作用:當我們的節點比較多的時候,可以實現共享

容器中的資料卷

 [[email protected] web]# docker run -itd -v /data --name dvdata ubuntu  --建立一個名為dvdata的容器,並以data目錄作為共享
 fd3c66993e0dd953e541ae8b07222fb1f0eeab8ef38e3f6c71c5f63876dbd78e
 [[email protected] web]# docker inspect dvdata
 
 
         "Mounts": [
             {
                 "Type": "volume",
                 "Name": "42c729559e7774f2ea4b0bc6649c456f88ec53958990eba77de7f8ad62c765cc",
                 "Source": "/var/lib/docker/volumes/42c729559e7774f2ea4b0bc6649c456f88ec53958990eba77de7f8ad62c765cc/_data",
                 "Destination": "/data",
                 "Driver": "local",
                 "Mode": "",
                 "RW": true,
                 "Propagation": ""
             }
         ],

刪除之前的容器,重新建立一個web01 web02 ,檢視共享情況

 [[email protected] web]# docker rm -f $(docker ps -q -a)
 fd3c66993e0d
 e8d9178cb457
 0a63d7406e9a
 [[email protected] web]# docker run -itd --name web01 --volumes-from dvdata ubuntu
 docker: Error response from daemon: No such container: dvdata.
 See 'docker run --help'.  --前面全部刪除了,所以這裡會顯示沒有這個容器
 [[email protected] web]# docker run -itd -v /data --name dvdata ubuntu  --重新建立這個容器
 1c49bc9ce9caff87d7c417d37ed8aa2ecb07e315717a21798bf95f02194e5519
 [[email protected] web]# docker run -itd --name web01 --volumes-from dvdata ubuntu
 36d8039fbcf4d5b3444a175598b8fca0ac1e72aef60f49bc00a00869b8523c6d
 [[email protected] web]# docker ps -l
 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 36d8039fbcf4        ubuntu              "/bin/bash"         12 seconds ago      Up 11 seconds                           web01
 [[email protected] web]# docker exec web01 ls /data
 [[email protected] web]# docker exec dvdata touch /data/index.php
 [[email protected] web]# docker exec dvdata ls /data
 index.php
 [[email protected] web]# docker exec web01 ls /data
 index.php
 [[email protected] web]# docker run -itd --name web02 --volumes-from dvdata ubuntu
 1f75d0d0c55f09345d13e3931af2e91b36e2f09ccd9c0b27fe934232b1192d56
 [[email protected] web]# docker exec web02 ls /data
 index.php