天天看點

2. volume解惑Volume解惑

Volume解惑

docker是一個容器,在容器中執行的所有操作都會在容器exit後丢失,為了保證資料的持久化,docker提出了volume的概念,目的是将容器中的資料持久化到主控端中。

docker可以使用兩種方法來建立volume,一種是建立容器時使用-v參數,另一種是建立鏡像的時候在Dockerfile檔案中設定volume。

下面來看看這兩種方式的差別:

1. 在Dockerfile建立Volume

Dockerfile

FROM daocloud.io/centos:latest
//這裡指定要在容器中挂載test1和test2目錄,這裡有一點需要注意一定要使用雙引号
VOLUME ["/test1","/test2"]
CMD /bin/bash           

編譯Dockerfile

mkdir test
mv Dockerfile test/
cd test
docker build . -t test           

從上一步生成的鏡像中建立一個容器:

//這裡建立一個名為test1的容器
docker run -it --name test-container test           

檢視容器中生成的test1和test2目錄與實體磁盤的對應關系

docker inspect test-container

//運作結果如下:其中Source指向的就是主控端的目錄
....
"Mounts": [
    {
        "Type": "volume",
        "Name": "01c6e11009f3a1382dc71715a2ecc810e6854a817fbaf47887ae361e1444135c",
        "Source": "/var/lib/docker/volumes/01c6e11009f3a1382dc71715a2ecc810e6854a817fbaf47887ae361e1444135c/_data",
        "Destination": "/test1",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "volume",
        "Name": "3f86f47f6dac9a3bf1a0967aee3f424db27c4dd5d48bd746b7692592085f0f25",
        "Source": "/var/lib/docker/volumes/3f86f47f6dac9a3bf1a0967aee3f424db27c4dd5d48bd746b7692592085f0f25/_data",
        "Destination": "/test2",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
.....           

2. 使用-v參數來建立volume

Dockerfile

FROM daocloud.io/centos:latest
CMD /bin/bash           
mkdir test
mv Dockerfile test/
cd test
docker build . -t test-new           

從上一步生成的鏡像建立容器:

docker run -it -v /test1 -v /test2 --name test-container-new test-new           
docker inspect test-container-new

//運作結果如下:其中Source指向的就是主控端的目錄
....
"Mounts": [
    {
        "Type": "volume",
        "Name": "2c8d289d74801a10f2ec283e83412541009d9fb7d40aec161e8c363b17d6e822",
        "Source": "/var/lib/docker/volumes/2c8d289d74801a10f2ec283e83412541009d9fb7d40aec161e8c363b17d6e822/_data",
        "Destination": "/test1",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "volume",
        "Name": "a0905219edf3ad76be39065598f5443d9230f72a8e2e650d98c4f982b87d952d",
        "Source": "/var/lib/docker/volumes/a0905219edf3ad76be39065598f5443d9230f72a8e2e650d98c4f982b87d952d/_data",
        "Destination": "/test2",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
.....           

通過兩種方式發現雖然都在主控端上生成了持久化目錄,但是主控端的目錄都是随機的

-v

參數進階用法,在主控端生成自定義持久目錄

docker run -it -v /test1:/test1 -v /test2:/test2 --name test-container-new-2 test-new           
docker inspect  test-container-new-2 

//運作結果如下:其中Source指向的就是主控端的目錄
....
"Mounts": [
    {
        "Type": "bind",
        "Source": "/test1",
        "Destination": "/test1",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "bind",
        "Source": "/test2",
        "Destination": "/test2",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
.....           

兩種建立volume方法的總結

建立Volume的兩種方式 随機目錄持久化 指定目錄持久化
Dokerfile VOLUME 可以實作 不支援
-v參數 支援