Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
docs(docker): 卷操作
Browse files Browse the repository at this point in the history
  • Loading branch information
zjZSTU committed Oct 1, 2019
1 parent a0f56fa commit 4d8c160
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/source/docker/storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
storage/[译]在Docker中管理数据
storage/[docker volume]创建和管理卷
storage/[volume][bind mount]标识符-v和--mount解析
storage/[volume][bind mount]bind propagation设置
storage/[volume][bind mount]bind propagation设置
storage/使用卷管理数据
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
* `readonly`:是否以[只读方式挂载](https://docs.docker.com/storage/volumes/#use-a-read-only-volume)到容器
* `volume-opt`:可以多次指定,由选项名及其值组成键值对

如果卷驱动程序接受逗号分隔的列表作为选项,则必须从外部`csv`解析器转义该值。要转义`volume opt`,请用双引号(`""`)将其括起来,并用单引号(`''`)将整个挂载参数括起来。比如

```
$ docker service create \
--mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"'
--name myservice \
<IMAGE>
```

### 绑定挂载操作

* `-v --volumes`:由3个字段组成,用冒号(`:`)隔开,字段的顺序必须正确
Expand Down
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 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.
135 changes: 135 additions & 0 deletions docs/source/docker/storage/使用卷管理数据.md
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": ""
}
],
```

0 comments on commit 4d8c160

Please sign in to comment.