docker_practice/data_management/volume.md

39 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

##数据卷
数据卷是一个由UFS文件系统专门设计的的特殊目录它可以提供很多有用的特性
* 数据卷可以在容器之间共享和重用
* 对数据卷的改变是立马生效
* 当你更新数据卷中的数据的时候不会被包含到image中
* 卷会一直存在直到没有容器使用他们
###添加一个数据卷
在用docker run命令的时候使用-v标记来添加一个数据卷。在一次run中多次使用可以挂载多个数据卷下面加载一个卷到web容器上。
```
$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py
```
创建一个新的卷到容器的/webapp
*注意也可以在dockerfile中使用volume来添加一个或者多个新的卷到由该image创建的任意容器
###挂载一个主机目录作为数据卷
使用-v标记也可以挂载一个主机的目录到容器中去
```
$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp
training/webapp python app.py
```
上面的命令加载主机的/src/webapp到容器的/opt/webapp
目录。这个在测试的时候特别好用比如我们可以加载我们的源码到容器中来查看他们是否正常工作。目录的路径必须是主机上的绝对路径如果目录不存在docker会自动为你创建它。
*注意:dockerfile 中不能用,各种操作系统的文件路径格式不一样,所以不一定适合所有的主机。
docker 加载的数据卷默认是读写权限,但我们可以把它加载为只读。
```
$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro
training/webapp python app.py
```
加了ro之后就挂载为只读了。
###挂载一个宿主主机文件作为数据卷
-v标记也可以从主机挂载单个文件到容器中
```
$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
```
这样就可以记录在容器输入过的命令了。
*注意很多工具子在使用vi或者sed --in-place的时候会导致inode的改变从docker 1.1
.0起它会报错所以最简单的办法就直接mount父目录。