docker_practice/data_management/bind-mounts.md

80 lines
2.3 KiB
Markdown
Raw Normal View History

2017-11-22 16:19:30 +00:00
## 挂载主机目录
### 选择 -v 还是 -mount 参数
2017-11-23 15:48:52 +00:00
Docker 新用户应该选择 `--mount` 参数,经验丰富的 Docker 使用者对 `-v` 或者 `--volume` 已经很熟悉了,但是推荐使用 `--mount` 参数。
2017-11-22 16:19:30 +00:00
### 挂载一个主机目录作为数据卷
使用 `--mount` 标记可以指定挂载一个本地主机的目录到容器中去。
```bash
$ docker run -d -P \
--name web \
# -v /src/webapp:/opt/webapp \
--mount type=bind,source=/src/webapp,target=/opt/webapp \
training/webapp \
python app.py
```
2017-12-03 02:26:48 +00:00
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/opt/webapp`目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 `-v` 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 `--mount` 参数时如果本地目录不存在Docker 会报错。
2017-11-22 16:19:30 +00:00
2017-11-24 04:30:18 +00:00
Docker 挂载主机目录的默认权限是 `读写`,用户也可以通过增加 `readonly` 指定为 `只读`
2017-11-22 16:19:30 +00:00
```bash
$ docker run -d -P \
--name web \
# -v /src/webapp:/opt/webapp:ro \
--mount type=bind,source=/src/webapp,target=/opt/webapp,readonly \
training/webapp \
python app.py
```
2017-11-24 04:30:18 +00:00
加了 `readonly` 之后,就挂载为 `只读` 了。如果你在容器内 `/src/webapp` 目录新建文件,会显示如下错误
2017-11-22 16:19:30 +00:00
```bash
/src/webapp # touch new.txt
touch: new.txt: Read-only file system
```
### 查看数据卷的具体信息
在主机里使用以下命令可以查看 `web` 容器的信息
```bash
$ docker inspect web
```
`挂载主机目录` 的配置信息在 "Mounts" Key 下面
```json
"Mounts": [
{
"Type": "bind",
"Source": "/src/webapp",
"Destination": "/opt/webapp",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
```
### 挂载一个本地主机文件作为数据卷
`--mount` 标记也可以从主机挂载单个文件到容器中
```bash
$ docker run --rm -it \
2017-11-24 04:30:18 +00:00
# -v $HOME/.bash_history:/root/.bash_history \
--mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history \
2017-11-22 16:19:30 +00:00
ubuntu:17.10 \
2017-11-24 04:30:18 +00:00
bash
root@2affd44b4667:/# history
1 ls
2 diskutil list
2017-11-22 16:19:30 +00:00
```
这样就可以记录在容器输入过的命令了。