Docker系列之一:Docker介绍及在Ubuntu上安装
Docker系列之二:Docker 入门
Docker系列之三:使用Docker镜像和仓库
Docker系列之四:Dockerfile的使用
Docker系列之五:Volume 卷的使用——以Redis为例
Docker系列之六:Volume 卷的使用——在Dockerfile中的用法
Docker系列之七:Docker 网络(内部网络、容器之间的连接)
Docker系列之八:在Dockerfile中使用多段构建Muti-stage build
Docker系列之九:Docker用于持续集成,构建Jenkins和Docker服务器
Docker系列之十:Docker Compose的使用
卷在Docker里非常重要,在系列之三中提到过,Docker利用联合加载技术在root文件系统层上加载更多的只读文件系统,这里简称联合文件系统(Union File System),卷是在一个或者多个容器内被选定的目录,可以绕过这个联合文件系统为Docker提供持久数据或者共享数据,这意味着对卷的修改会直接生效,并绕过镜像,卷可以在容器之间共享,即使容器停止,卷里的内容依旧存在。
创建一个文件, index.html
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat index.html
Hello world
执行如下命令
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker run -itd -p 5005:80 -v $PWD:/usr/share/nginx/html nginx
7b56643e727d89f9cf4191a7850c8aab6816d13480a2afa0515a63aa51a65378
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# curl localhost:5005
Hello world
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it 7b56 bash
root@7b56643e727d:/# cd /usr/share/nginx/html
root@7b56643e727d:/usr/share/nginx/html# ls
index.html
可以看到我们将/home/cong/DockerDemo文件夹里面的内容挂载到容器里的/usr/share/nginx/html中了。
然后我们进入容器里面,修改index.html的内容如下:
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it 7b56 bash
root@7b56643e727d:/usr/share/nginx/html# cat index.html
Hello world
my greet
输入exit退出容器后,查看本机的index.html文件
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# cat index.html
Hello world
my greet
可以看出,当修改了容器里面的index.html后,本机的index.html也同步被修改了。这就很好的解决了一个问题,当容器销毁后,修改的内容被保存在本机了。这对于将数据库放入容器中非常有用。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker run -itd -p 5005:80 -v $PWD:/usr/share/nginx/html:ro nginx
290e3eaa20816eb526213c731807a60bf8291e554f730bd5e4d0c98b2018e823
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it 290e bash
root@290e3eaa2081:/# cd /usr/share/nginx/html
root@290e3eaa2081:/usr/share/nginx/html# ls
index.html
root@290e3eaa2081:/usr/share/nginx/html# apt-get update && apt-get install vim -y && apt-get install sudo
root@290e3eaa2081:/usr/share/nginx/html# sudo chmod 777 index.html
chmod: changing permissions of 'index.html': Read-only file system
可以看出,容器里面的index.html文件为只读文件,强行改权限也不行。
上面我们已基本掌握了volume的用法了,在本机上安装好redis客户端,运行 redis镜像,测试成功,如下:
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# apt-get install redis-tools -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
redis-tools is already the newest version (2:3.0.6-1).
0 upgraded, 0 newly installed, 0 to remove and 268 not upgraded.
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker run -itd -p 6379:6379 redis
c56171cd4309148b0ca606208be33b2488e2bc3a7e93fee7dc1230003bb3f66f
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker port dreamy_kare 6379
0.0.0.0:6379
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# redis-cli -h localhost -p 6379
localhost:6379> set foo 1
OK
localhost:6379> get foo
"1"
localhost:6379> config get dir
1) "dir"
2) "/data"
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it c561 bash
root@c56171cd4309:/data# ls
dump.rdb
接下来我们要将redis存储数据的文件使用卷给挂载出来,这样即使这个容器销毁了,数据依然保存在本机中。进入容器里面,发现dump.rdb文件在/data这个路径中,dump.rdb文件就是redis存储数据的文件。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker run -itd -p 6379:6379 -v $PWD:/data redis
1b25aef5f610668a210e5bfb92c0cd9bf3cf05260e8d3376a676d509463fe4f2
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it 1b25 bash
root@1b25aef5f610:/data# ls
index.html
root@1b25aef5f610:/data# exit
exit
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# redis-cli -h localhost -p 6379
localhost:6379> config get dir
1) "dir"
2) "/data"
localhost:6379> get foo 1
(error) ERR wrong number of arguments for 'get' command
localhost:6379> set foo 1
OK
localhost:6379> get foo
"1"
localhost:6379> exit
#等待一会儿后,才能看到dump.rdb文件
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker exec -it 1b25 bash
root@1b25aef5f610:/data# ls
dump.rdb index.html
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# ls
dump.rdb index.html
开启另一个redis容器, 再将本地dump.rdb挂载进去
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b25aef5f610 redis "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:6379->6379/tcp kind_curran
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker run -itd -p 6380:6379 -v $PWD:/data redis
70ea150288c3bc382be82fac4b79ebc857c2818064ac66a808a9653a0dbbcd8d
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# redis-cli -h localhost -p 6380
localhost:6380> get foo
"1"
localhost:6380> exit
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70ea150288c3 redis "docker-entrypoint.s…" 26 seconds ago Up 25 seconds 0.0.0.0:6380->6379/tcp optimistic_curran
1b25aef5f610 redis "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6379->6379/tcp kind_curran
直接去获取foo,字符串为1. 由此可以看出,redis的数据被持久化存储在本机当前路径下的dump.rdb文件中。要查询volume可以使用如下命令。
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker volume ls
DRIVER VOLUME NAME
local 10fd2a6d86f8cc433b505f087d75f2e0b31dd2f7385d3a750a33a78f2a0884dd
local 12243b35012e753006e66be39f57b59bfa61965e9ac6337ca3d1696a9dad15cd
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker volume -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
root@iZbp13z6cxj72rb7bxf0smZ:/home/cong/DockerDemo# docker volume inspect fb8e6aa826e59b4c06283ca91e45d202ca57144a0a9e169c5aff5b5349309eac
[
{
"CreatedAt": "2018-06-27T13:31:41+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/fb8e6aa826e59b4c06283ca91e45d202ca57144a0a9e169c5aff5b5349309eac/_data",
"Name": "fb8e6aa826e59b4c06283ca91e45d202ca57144a0a9e169c5aff5b5349309eac",
"Options": null,
"Scope": "local"
}
]
http://www.runoob.com/redis/redis-backup.html