This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions
121
docs/source/docker/storage/imgs/volumes-shared-storage.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
|
||
# 使用卷管理数据 | ||
|
||
参考:[Use volumes](https://docs.docker.com/storage/volumes/) | ||
|
||
![](./imgs/types-of-mounts-volume.png) | ||
|
||
## 优势 | ||
|
||
卷与在容器的可写层中保存持久化数据相比是更好的选择,因为卷不会增加使用的容器大小,并且卷的内容给定于容器的生命周期之外 | ||
|
||
使用卷(`volume`)管理数据有如下优势: | ||
|
||
* 卷比绑定挂载更容易备份(`back-up`)或迁移(`migrate`) | ||
* 可以使用`Docker CLI`命令或`Docker API`管理卷 | ||
* 卷可以在`Linux`和`Windows`容器上工作 | ||
* 卷可以在多个容器之间更安全地共享 | ||
* 卷驱动程序允许在远程主机或云提供商上存储卷,加密卷的内容或添加其他功能 | ||
* 新卷的内容可以由容器预先填充 | ||
|
||
## 启动容器挂载卷 | ||
|
||
在启动容器同时挂载卷,如果卷不存在,则`Docker`会自动创建。示例如下,创建容器并挂载卷`myvol2`到`/app` | ||
|
||
``` | ||
# --mount | ||
$ docker run -d \ | ||
--name devtest \ | ||
--mount source=myvol2,target=/app \ | ||
nginx:latest | ||
# -v | ||
$ docker run -d \ | ||
--name devtest \ | ||
-v myvol2:/app \ | ||
nginx:latest | ||
``` | ||
|
||
启动后可通过`docker inspect`检查容器挂载信息,指定了卷在主机的地址,在容器的挂载地址,是否读写等等信息 | ||
|
||
``` | ||
$ docker inspect devtest | ||
[ | ||
... | ||
... | ||
... | ||
... | ||
"Mounts": [ | ||
{ | ||
"Type": "volume", | ||
"Source": "myvol2", | ||
"Target": "/app" | ||
} | ||
], | ||
... | ||
... | ||
"Mounts": [ | ||
{ | ||
"Type": "volume", | ||
"Name": "myvol2", | ||
"Source": "/var/lib/docker/volumes/myvol2/_data", | ||
"Destination": "/app", | ||
"Driver": "local", | ||
"Mode": "z", | ||
"RW": true, | ||
"Propagation": "" | ||
} | ||
], | ||
... | ||
... | ||
] | ||
``` | ||
|
||
## 使用容器填充卷 | ||
|
||
启动创建新卷的容器,并且该容器在要装入的目录(如`/app/`)中包含文件或目录,则该目录的内容将复制到该卷中。使用该卷的其他容器也可以访问预填充的内容 | ||
|
||
示例如下,启动容器`nginx`,创建新卷`nginx-vol`,挂载到容器的`/usr/share/nginx/html`目录,其是`nginx`默认的`HTML`保存地址 | ||
|
||
``` | ||
$ docker run -d \ | ||
--name=nginxtest \ | ||
--mount source=nginx-vol,destination=/usr/share/nginx/html \ | ||
nginx:latest | ||
$ docker run -d \ | ||
--name=nginxtest \ | ||
-v nginx-vol:/usr/share/nginx/html \ | ||
nginx:latest | ||
``` | ||
|
||
其实`html`文件夹中的数据预填充到了卷`nginx-vol`中,启动另一个容器`ubuntu`,挂载卷`nginx-vol`到`/app` | ||
|
||
``` | ||
$ docker run -it --rm --name=ubuntu --mount source=nginx-vol,destination=/app alpine:latest | ||
/ # cd app | ||
/app # ls | ||
50x.html index.html | ||
``` | ||
|
||
此时`/app`中出现了预填充的`html`文件 | ||
|
||
## 使用只读卷 | ||
|
||
可以设置容器只对卷的数据进行读访问(`read-only`)。多个容器可以挂载同一个卷,其中一些容器可以以读写方式挂载,而其他容器可以同时以只读方式挂载 | ||
|
||
使用`ro, readonly`作为卷的选项 | ||
|
||
``` | ||
# --mount | ||
$ docker run -d \ | ||
--name=nginxtest \ | ||
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \ | ||
nginx:latest | ||
# -v | ||
$ docker run -d \ | ||
--name=nginxtest \ | ||
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \ | ||
nginx:latest | ||
``` | ||
|
||
使用`docker inspect`查询 | ||
|
||
``` | ||
"Mounts": [ | ||
{ | ||
"Type": "volume", | ||
"Name": "nginx-vol", | ||
"Source": "/var/lib/docker/volumes/nginx-vol/_data", | ||
"Destination": "/usr/share/nginx/html", | ||
"Driver": "local", | ||
"Mode": "z", | ||
"RW": false, // 在这里 | ||
"Propagation": "" | ||
} | ||
], | ||
``` |