溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Docker管理containers的數(shù)據(jù)的方法

發(fā)布時(shí)間:2021-08-06 10:03:43 來源:億速云 閱讀:142 作者:chen 欄目:云計(jì)算

本篇內(nèi)容主要講解“Docker管理containers的數(shù)據(jù)的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Docker管理containers的數(shù)據(jù)的方法”吧!

對(duì)于Docker來說,containers是通過如下兩種方式管理數(shù)據(jù):

  1. Data volumes

  2. Data volume containers

Data volumes

一個(gè)數(shù)據(jù)卷是指container中的一個(gè)特殊目錄,具有以下特點(diǎn):

  1. 初始化:在創(chuàng)建container時(shí)進(jìn)行初始化。若container所屬image在數(shù)據(jù)卷的掛載點(diǎn)有數(shù)據(jù),那么這些數(shù)據(jù)則是在初始化時(shí)拷貝到數(shù)據(jù)卷。

  2. Container之間可以共享數(shù)據(jù)卷,數(shù)據(jù)卷也可被重用。

  3. 數(shù)據(jù)卷中的數(shù)據(jù)修改是立現(xiàn)的,即一旦被修改,數(shù)據(jù)卷掛載到的containers都實(shí)時(shí)可見。

  4. 在更新image時(shí),不會(huì)更新數(shù)據(jù)卷中的數(shù)據(jù)變化。

  5. 即使刪除container,其數(shù)據(jù)卷也不會(huì)被刪除。

Add a data volume

在執(zhí)行命令docker create或者docker run時(shí)加入?yún)?shù)-v即可為一個(gè)container添加一個(gè)數(shù)據(jù)卷,可以多次使用-v以添加多個(gè)數(shù)據(jù)卷。簡(jiǎn)單示例如下:

docker run -d -P --name web -v /webapp training/webapp python app.py

上述命令會(huì)在container的/webapp創(chuàng)建掛載點(diǎn)。

如果想要知道/webapp在主機(jī)上對(duì)應(yīng)的目錄,可執(zhí)行:

docker inspect web

結(jié)果如下:

...
"Volumes": {
  "/webapp": "/var/lib/docker/vfs/dir/b0518f7a863879aa391da6e1d0c8455db1b0d7d6f716f49463952ebd558bbe1b"
},
"VolumesRW": {
 "/webapp": true
}
...

“Volumes”是/webapp的主機(jī)目錄?!盫olumesRW”表示該數(shù)據(jù)卷可被讀寫。

我們發(fā)現(xiàn)/webapp的主機(jī)目錄具有一個(gè)隨機(jī)產(chǎn)生的名字,并不友好,所以,在創(chuàng)建container時(shí),可指定將主機(jī)上的某目錄作為數(shù)據(jù)卷掛載到container的指定目錄下,只需要在使用-v參數(shù)時(shí)指定一下:

docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

如上,將主機(jī)的/src/webapp掛載到container的/opt/webapp,如果/src/webapp不存在,會(huì)被創(chuàng)建。該數(shù)據(jù)卷默認(rèn)可被讀寫,若需要限制其為可讀,執(zhí)行如下:

docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

當(dāng)然,也可以掛載一個(gè)單獨(dú)的文件:

docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

如上,在container中使用的命令都會(huì)記錄下來,當(dāng)離開container后,仍舊可以查看歷史記錄。

Creating and mounting a data volume container

如果需要在containers之間共享持久化數(shù)據(jù),或者使用非持久化containers的持久化數(shù)據(jù),可以考慮創(chuàng)建數(shù)據(jù)卷container,并將其中數(shù)據(jù)掛載到其他containers。

創(chuàng)建數(shù)據(jù)卷containers如下:

docker create -v /data --name dbdata training/postgres /bin/true

Container dbdata并不執(zhí)行任何應(yīng)用。

可以使用參數(shù)–volumes-from將/data掛載到別的containers中:

docker run -d --volumes-from dbdata --name db1 training/postgres
docker run -d --volumes-from dbdata --name db2 training/postgres

db1和db2共享數(shù)據(jù)。

數(shù)據(jù)卷掛載具有傳遞性,可通過db1或者db2將/data掛載到另一個(gè)container:

docker run -d --name db3 --volumes-from db1 training/postgres

如果此時(shí)刪除dbdata、db1、db2,數(shù)據(jù)卷是不會(huì)被刪除的,一定要在刪除數(shù)據(jù)卷掛載的最后一個(gè)container時(shí)將其刪除,如:

docker rm -v db3

到此,相信大家對(duì)“Docker管理containers的數(shù)據(jù)的方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI