Docker学习笔记(四)Docker数据持久化

一、容器与宿主机共享数据:

上面讲到的两种类型的数据卷(data volume)。他们均可实现在容器和宿主机之间共享数据,但方式有所区别:
对于bind mount,直接在启动容器时将宿主机目录挂载到容器指定的目录。docker managed volume就要麻烦点,由于volume存在于宿主机中,在启动容器时才生成,所以需要将数据复制到volume中。
除此之外,我们可以使用docker cp命令,将宿主机中的数据复制到容器中:
[root@docker ~]# docker cp index.html e19052f33d61:/usr/share/nginx/html/index2.html
[root@docker ~]# docker exec -it nginx bash
root@e19052f33d61:/# ls /usr/share/nginx/html/
50x.html index.html index2.html
root@e19052f33d61:/#
docker cp命令不可以覆盖容器正在使用的文件。

二、容器与容器之间共享数据:

容器之间共享数据,第一种方法是使用bind mount方式,我们把数据放在bind mount的源目录中,挂载到多个容器中。
以nginx容器为例,我们启动三个nginx容器,分别为1,2和3,并且把index.html挂载到三个容器:

[root@docker ~]# docker run --name nginx1 -d -P \
-v ~/index.html:/usr/share/nginx/html/index.html docker.io/nginx
20a9c8db433a842efdc264e689788a049b9e0612709f134157887dbc8b5b0b6c
[root@docker ~]# docker run --name nginx2 -d -P \
-v ~/index.html:/usr/share/nginx/html/index.html docker.io/nginx
97173f107ac65631bb9572bb74000c39560b4d22a476fe07efc0f4fa11699340
[root@docker ~]# docker run --name nginx3 -d -P \
-v ~/index.html:/usr/share/nginx/html/index.html docker.io/nginx
e69772dc9ac23155c869f0d6d0048c11b95535f5adc4d552e8c246d901c03964

测试访问容器:

[root@docker ~]# curl 127.0.0.1:32770
111111111111111111
[root@docker ~]# curl 127.0.0.1:32771
111111111111111111
[root@docker ~]# curl 127.0.0.1:32772
111111111111111111

更新本地的index.html后再访问容器:

[root@docker ~]# echo "update for website" > index.html 
[root@docker ~]# cat index.html 
update for website
[root@docker ~]# curl 127.0.0.1:32772
update for website
[root@docker ~]# curl 127.0.0.1:32771
update for website
[root@docker ~]# curl 127.0.0.1:32770
update for website
[root@docker ~]#

另一种容器之间共享数据的方法是使用volume container。

三、volume container共享数据:

volume container是专门为其他容器提供volume的容器,该容器不需要是运行状态,他提供的卷可以是bind mount,也可以是docker managed volume。下面我们来创建一个volume container:

[root@docker data]#  docker create --name vc_data \
> -v /data/nginx/html:/usr/share/nginx/html \
> -v /other/useful/tools \
> docker.io/nginx
05cf0bbd2560b478e298008c370f9c60f45a7b2e97e2b218e1e3328397c3ba6b
[root@docker data]#

我们使用的是docker create命令,而不是docker run,是因为我们上面提到volume container只提供数据,本身不需要运行。
上面的volume containermount了两个volume:
bind mount,存放web server的静态文件;
docker managed volume,存放工具。
通过docker inspect可以查看这两个volume:

"Mounts": [
    {
        "Type": "bind",
        "Source": "/data/nginx/html",
        "Destination": "/usr/share/nginx/html",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    },
    {
        "Type": "volume",
        "Name": "db7000f42661413d5b4a151d99252475c3ab3a9271177d38f338e22dec040f28",
        "Source": "/var/lib/docker/volumes/db7000f42661413d5b4a151d99252475c3ab3a9271177d38f338e22dec040f28/_data",
        "Destination": "/other/useful/tools",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

其他容器可以使用 --volumes-from参数使用vc_data这个volume container:

[root@docker data]# docker run --name nginx1 -d -P --volumes-from vc_data docker.io/nginx
bf180a9b01f5e31ac2e87d92a44aeb156c84fa125b51521ff62b34e24d699f6b
[root@docker data]# docker run --name nginx2 -d -P --volumes-from vc_data docker.io/nginx
400ba04aa783322f7b06540000c6545fa0510751325171546842a78367eec865
[root@docker data]# docker run --name nginx3 -d -P --volumes-from vc_data docker.io/nginx
06898aad50d0e51f448feb32a0fb6517a06c34acf682d35a7b37ad060d59005f

测试访问容器:

[root@docker data]# curl 127.0.0.1:32773
website in host
[root@docker data]# curl 127.0.0.1:32774
website in host
[root@docker data]# curl 127.0.0.1:32775
website in host

使用docker inspect查看容器有哪些volume:

"Mounts": [
    {
        "Source": "/data/nginx/html",
        "Destination": "/usr/share/nginx/html",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    },
    {
        "Name": "db7000f42661413d5b4a151d99252475c3ab3a9271177d38f338e22dec040f28",
        "Source": "/var/lib/docker/volumes/db7000f42661413d5b4a151d99252475c3ab3a9271177d38f338e22dec040f28/_data",
        "Destination": "/other/useful/tools",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

可以看出,mount的volume和vc_data配置的完全一致。
总结一下volume container的特点:
与bind mount相比,不必为每一个容器指定源volume,所有path都爱volume container中定义好了,容器只需与volume container关联。
使用 volume container的容器,其mount point是一样的,有利于配置的规范化和标准化,但也有一定的局限性,使用时需要综合考虑。

四、data-packed volume container:

在上面的例子中,volume container的数据归根结底还是在宿主机上,我们现在使用一种方法,将数据存放在容器中,这种方法叫data-packed volume container,原理是创建新的镜像,把数据打包到新的镜像中,然后通过docker managed volume 共享:
使用Dockerfile创建镜像:

FROM busybox:latest
ADD htdocs /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocs

ADD 是将宿主机的htdocs的数据复制到镜像中;
VOLUME的作用与-v一样,用来创建docker managed volume,mount point是/usr/local/apache2/htdocs,因为这个目录就是ADD的目录,所以数据会直接复制到volume中。
构建镜像:

[root@docker ~]# docker build -t datapacked .
Sending build context to Docker daemon 28.16 kB
Step 1/3 : FROM busybox:latest
 ---> db8ee88ad75f
Step 2/3 : ADD htdocs /usr/local/apache2/htdocs
 ---> 42c812fe1f9c
Removing intermediate container ab3ff59c6969
Step 3/3 : VOLUME /usr/local/apache2/htdocs
 ---> Running in 15ecb872d6ce
 ---> 0ae7b4f2bf51
Removing intermediate container 15ecb872d6ce
Successfully built 0ae7b4f2bf51

用新的镜像创建data-packed volume container:

[root@docker ~]# docker create --name vc_data2 datapacked
7ba20b97445f65525b685fee5e5359e9670fe6936379656475e2dd71869068b4

启动httpd容器,并使用data-packed volume container:

[root@docker ~]# docker run -d -P --volumes-from vc_data2 httpd
0318446eb961776b083831ce0241c9347b751a53f0eb863cf8be16531f932241

启动后测试访问httpd容器:

[root@docker ~]# curl 127.0.0.1:32776
datapacked

刚好是我们自定义的index.html。
使用docker inspect 命令查看mount point:

"Mounts": [
    {
        "Name": "3aa7883b8e786564e6cf797cdebcb2078ee830281c6c9910486d0e7c7d6b547d",
        "Source": "/var/lib/docker/volumes/3aa7883b8e786564e6cf797cdebcb2078ee830281c6c9910486d0e7c7d6b547d/_data",
        "Destination": "/usr/local/apache2/htdocs",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
发布了21 篇原创文章 · 获赞 6 · 访问量 2847

猜你喜欢

转载自blog.csdn.net/weixin_43334786/article/details/105407762