目錄
前言
更詳細(xì)的Diff
適合Volumes的場(chǎng)景
適合bind mounts的場(chǎng)景
適合tmpfs mounts的場(chǎng)景
使用
前言
回到目錄
我們可以將數(shù)據(jù)寫(xiě)到容器的可寫(xiě)入層,但是這種寫(xiě)入是有缺點(diǎn)的:
當(dāng)容器停止運(yùn)行時(shí),寫(xiě)入的數(shù)據(jù)會(huì)丟失。你也很難將這些數(shù)據(jù)從容器中取出來(lái)給另外的應(yīng)用程序使用。
容器的可寫(xiě)入層與宿主機(jī)是緊密耦合的。這些寫(xiě)入的數(shù)據(jù)在可以輕易地被刪掉。
寫(xiě)入容器的可寫(xiě)入層需要一個(gè)存儲(chǔ)驅(qū)動(dòng)(storage driver)來(lái)管理文件系統(tǒng)。這個(gè)存儲(chǔ)驅(qū)動(dòng)通過(guò)linux內(nèi)核提供了一個(gè)union filesystem。相比于數(shù)據(jù)卷(data volume),這種額外的抽象會(huì)降低性能。
Docker提供了3種方法將數(shù)據(jù)從Docker宿主機(jī)掛載(mount)到容器:
volumes,
bind mounts
tmpfs mounts。
一般來(lái)說(shuō),volumes 總是最好的選擇。
不管你選擇哪種掛載方式,從容器中看都是一樣的。數(shù)據(jù)在容器的文件系統(tǒng)中被展示為一個(gè)目錄或者一個(gè)單獨(dú)的文件。
一個(gè)簡(jiǎn)單區(qū)分volumes,bind mounts和tmpfs mounts不同點(diǎn)的方法是:思考數(shù)據(jù)在宿主機(jī)上是如何存在的。
Volumes由Docker管理,存儲(chǔ)在宿主機(jī)的某個(gè)地方(在linux上是/var/lib/docker/volumes/)。非Docker應(yīng)用程序不能改動(dòng)這一位置的數(shù)據(jù)。Volumes是Docker最好的數(shù)據(jù)持久化方法。
Bind mounts的數(shù)據(jù)可以存放在宿主機(jī)的任何地方。數(shù)據(jù)甚至可以是重要的系統(tǒng)文件或目錄。非Doc
ker應(yīng)用程序可以改變這些數(shù)據(jù)。
tmpfs mounts的數(shù)據(jù)只存儲(chǔ)在宿主機(jī)的內(nèi)存中,不會(huì)寫(xiě)入到宿主機(jī)的文件系統(tǒng)。
更詳細(xì)的Diff
回到目錄
Volumes:由Docker創(chuàng)建和管理。你可以通過(guò)docker volume create命令顯式地創(chuàng)建volume,Docker也可以在創(chuàng)建容器或服務(wù)是自己創(chuàng)建volume。
當(dāng)你創(chuàng)建了一個(gè)volume,它會(huì)被存放在宿主機(jī)的一個(gè)目錄下。當(dāng)你將這個(gè)volume掛載到某個(gè)容器時(shí),這個(gè)目錄就是掛載到容器的東西。這一點(diǎn)和bind mounts類(lèi)似,除了volumes是由Docker創(chuàng)建的,和宿主機(jī)的核心(core functionality)隔離。
一個(gè)volume可以同時(shí)被掛載到幾個(gè)容器中。即使沒(méi)有正在運(yùn)行的容器使用這個(gè)volume,volume依然存在,不會(huì)被自動(dòng)清除??梢酝ㄟ^(guò)docker volume prune清除不再使用的volumes。
volumes也支持volume driver,可以將數(shù)據(jù)存放在另外的機(jī)器或者云上。
Bind mounts:Docker早期就支持這個(gè)特性。與volumes相比,Bind mounts支持的功能有限。使用bind mounts時(shí),宿主機(jī)上的一個(gè)文件或目錄被掛載到容器上。
警告:使用Bind mounts的一個(gè)副作用是,容器中運(yùn)行的程序可以修改宿主機(jī)的文件系統(tǒng),包括創(chuàng)建,修改,刪除重要的系統(tǒng)文件或目錄。這個(gè)功能可能會(huì)有安全問(wèn)題。
tmpfs mounts:tmpfs mounts的數(shù)據(jù)不會(huì)落盤(pán)。在容器的生命周期內(nèi),它可以被用來(lái)存儲(chǔ)一些不需要持久化的狀態(tài)或敏感數(shù)據(jù)。例如,swarm服務(wù)通過(guò)tmpfs mounts來(lái)將secrets掛載到一個(gè)服務(wù)的容器中去。
適合Volumes的場(chǎng)景
回到目錄
在不同的容器中共享數(shù)據(jù)。If you don’t explicitly create it, a volume is created the first time it is mounted into a container. When that container stops or is removed, the volume still exists. Multiple containers can mount the same volume simultaneously, either read-write or read-only. Volumes are only removed when you explicitly remove them.
When the Docker host is not guaranteed to have a given directory or file structure. Volumes help you decouple the configuration of the Docker host from the container runtime.
When you want to store your container’s data on a remote host or a cloud provider, rather than locally.
當(dāng)你需要備份或遷移數(shù)據(jù)的時(shí)候,When you need to be able to back up, restore, or migrate data from one Docker host to another, volumes are a better choice. You can stop containers using the volume, then back up the volume’s directory (such as /var/lib/docker/volumes/).
適合bind mounts的場(chǎng)景
回到目錄
宿主機(jī)和容器共享配置文件。Docker提供的DNS解決方案就是如此,將宿主機(jī)的/etc/resolv.conf掛載到每個(gè)容器中。
開(kāi)發(fā)環(huán)境需要在宿主機(jī)和容器中共享代碼。docker的開(kāi)發(fā)就是如此,畢竟容器中一般是沒(méi)有編輯器的
When the file or directory structure of the Docker host is guaranteed to be consistent with the bind mounts the containers require.
適合tmpfs mounts的場(chǎng)景
回到目錄
tmpfs mounts主要用在你既不想在容器內(nèi),又不想在宿主機(jī)文件系統(tǒng)保存數(shù)據(jù)的時(shí)候。這可能是出于安全原因,也可能是你的應(yīng)用需要寫(xiě)非常多的非持久化數(shù)據(jù),tmpfs mounts這時(shí)候可以保證容器性能。
使用
回到目錄
volume(-v)
參數(shù)–volume(或簡(jiǎn)寫(xiě)為-v)只能創(chuàng)建bind mount。示例:
docker run --name $CONTAINER_NAME -it
-v /localhost/app:/container/app:rw
-v /localhost/app:/container/app:ro
nginx:latest /bin/bash
注釋?zhuān)?/p>
命令格式:[宿主機(jī)目錄:]容器目錄[:OPTIONS]]]
如果指定宿主機(jī)目錄,則必須是絕對(duì)路徑,如果路徑不存在則會(huì)自動(dòng)創(chuàng)建
實(shí)例中的 rw 為讀寫(xiě),ro 為只讀
–mount
參數(shù)–mount默認(rèn)情況下用來(lái)掛載volume,但也可以用來(lái)創(chuàng)建bind mount和tmpfs。如果不指定type選項(xiàng),則默認(rèn)為掛載volume,
volume是一種更為靈活的數(shù)據(jù)管理方式,volume可以通過(guò)docker volume命令集被管理。
示例:
docker run --name
C
O
N
T
A
I
N
E
R
N
A
M
E
?
i
t
?
?
?
m
o
u
n
t
t
y
p
e
=
b
i
n
d
,
s
o
u
r
c
e
=
CONTAINER_NAME -it \ --mount type=bind,source=
CONTAINERN?AME?it???mounttype=bind,source=PWD/
C
O
N
T
A
I
N
E
R
N
A
M
E
/
a
p
p
,
d
e
s
t
i
n
a
t
i
o
n
=
/
a
p
p
?
?
?
m
o
u
n
t
s
o
u
r
c
e
=
CONTAINER_NAME/app,destination=/app \ --mount source=
CONTAINERN?AME/app,destination=/app???mountsource={CONTAINER_NAME}-data,destination=/data,readonly
avocado-cloud:latest /bin/bash
注釋?zhuān)?/p>
掛載volume命令格式:[type=volume,]source=my-volume,destination=/path/in/container[,…]
創(chuàng)建bind mount命令格式:type=bind,source=/path/on/host,destination=/path/in/container[,…]
如果創(chuàng)建bind mount并指定source則必須是絕對(duì)路徑,且路徑必須已經(jīng)存在
示例中readonly表示只讀
mount 官方文檔里面參數(shù)有個(gè)表格:
原文:
Propagation setting Description
shared Sub-mounts of the original mount are exposed to replica mounts, and sub-mounts of replica mounts are also propagated to the original mount.
slave similar to a shared mount, but only in one direction. If the original mount exposes a sub-mount, the replica mount can see it. However, if the replica mount exposes a sub-mount, the original mount cannot see it.
private The mount is private. Sub-mounts within it are not exposed to replica mounts, and sub-mounts of replica mounts are not exposed to the original mount.
rshared The same as shared, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rslave The same as slave, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rprivate The default. The same as private, meaning that no mount points anywhere within the original or replica mount points propagate in either direction.
使用示例:
docker run --name
C
O
N
T
A
I
N
E
R
N
A
M
E
?
i
t
?
?
?
m
o
u
n
t
t
y
p
e
=
b
i
n
d
,
s
o
u
r
c
e
=
CONTAINER_NAME -it \ --mount type=bind,source=
CONTAINERN?AME?it???mounttype=bind,source=PWD/$CONTAINER_NAME/app,destination=/app,bind-Propagation=slave
avocado-cloud:latest /bin/bash
tmpfs
tmpfs 不在磁盤(pán)上持久存儲(chǔ),也不在 Docker 容器里面存儲(chǔ),他存儲(chǔ)在 localhost 的內(nèi)存中,它可以在容器的整個(gè)生命周期內(nèi)被容器所使用。
使用示例:
docker run -d -it -p 80:80 --name tmptest
–mount type=tmpfs,destination=/usr/share/nginx/html
nginx:latest
容器對(duì)目錄所有的讀寫(xiě)操作都在內(nèi)存中。
指定 tmpfs 的權(quán)限情況:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-456207.html
docker run -d -it -p 80:80 --name tmptest
–mount type=tmpfs,destination=/usr/share/nginx/html,tmpfs-mode=1770
nginx:latest文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-456207.html
到了這里,關(guān)于docker 數(shù)據(jù)掛載的三種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!