zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Docker 之间如何共享数据?

Docker数据 如何 之间 共享
2023-09-14 09:01:46 时间

容器之间共享数据


第一种方法是将共享数据放在 bind mount 中,然后将其 mount 到多个容器。还是以 httpd 为例,不过这次的场景复杂些,我们要创建由三个nginx容器组成的 web server 集群,它们使用相同的 html 文件,操作如下:

(1)将 /data/nginx/html/ mount 到三个nginx 容器.

[root@localhost ~]# cat /data/nginx/html/index.html 
share directory
[root@localhost ~]# docker run -itd --name=nginx1 -v /data/nginx/html:/usr/share/nginx/html -p 81:80 nginx
60bbbe1fca8e998d70291c03dafb209222315619cc34d129d0cf27c120bc8766
[root@localhost ~]# docker run -itd --name=nginx2 -v /data/nginx/html:/usr/share/nginx/html -p 82:80 nginx
8d0c42ab5e5283b4d5cbecede776d3eface0e26b0a64b67f35135f4255dd8537
[root@localhost ~]# docker run -itd --name=nginx3 -v /data/nginx/html:/usr/share/nginx/html -p 83:80 nginx
db94a6b3086c0adeee17cd66b7e622d38f57890d76b82a228b937fdb43de0e2d


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
db94a6b3086c        nginx               "/docker-entrypoint.…"   16 seconds ago      Up 14 seconds       0.0.0.0:83->80/tcp   nginx3
8d0c42ab5e52        nginx               "/docker-entrypoint.…"   25 seconds ago      Up 24 seconds       0.0.0.0:82->80/tcp   nginx2
60bbbe1fca8e        nginx               "/docker-entrypoint.…"   39 seconds ago      Up 38 seconds       0.0.0.0:81->80/tcp   nginx1

(2)查看当前主页内容。 

[root@localhost ~]# curl 192.168.179.99:81
share directory
[root@localhost ~]# curl 192.168.179.99:82
share directory
[root@localhost ~]# curl 192.168.179.99:83
share directory

(3)修改 volume 中的主页文件,再次查看并确认所有容器都使用了新的主页。

[root@localhost ~]# echo "share directory changed" > /data/nginx/html/index.html 
[root@localhost ~]# curl 192.168.179.99:81
share directory changed
[root@localhost ~]# curl 192.168.179.99:82
share directory changed
[root@localhost ~]# curl 192.168.179.99:83
share directory changed

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

 

用 volume container 共享数据


volume container 是专门为其他容器提供 volume 的容器。它提供的卷可以是 bind mount,也可以是 docker managed volume。下面我们创建一个 volume container: 

[root@localhost ~]# mkdir -p /data/nginx/html
[root@localhost ~]# echo "hello nginx" >> /data/nginx/html/index.html

[root@localhost ~]# docker run -itd --name=nginx1 -v /data/nginx/html:/usr/share/nginx/html -p 80:80 nginx
9466015b8205c7b488085668044a3e9451fa701e40845d4e1d75cb46faf033a3
[root@localhost ~]# docker inspect nginx1 
       "Mounts": [
            {
                "Type": "bind",
                "Source": "/data/nginx/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

bind mount,存放 web server 的静态文件。

其他容器可以通过 --volumes-from 使用 nginx1 这个 volume container:

[root@localhost ~]# docker run -itd --name=nginx2 --volumes-from nginx1  -p 81:80 nginx
2189b30e6ad5eef5bab7be269511df464066eee5e7f74cf39ff21fc0b0902194
[root@localhost ~]# docker run -itd --name=nginx3 --volumes-from nginx1  -p 82:80 nginx
d076c6581d3d621ec99c3915ba359d5e550874915be047f1c3034a129e4f806c
[root@localhost ~]# curl localhost:81
hello nginx
[root@localhost ~]# curl localhost:82
hello nginx


[root@localhost ~]# docker inspect nginx2
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/data/nginx/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

 可见,三个容器已经成功共享了 volume container 中的 volume。

下面我们讨论一下 volume container 的特点:

  1. 与 bind mount 相比,不必为每一个容器指定 host path,所有 path 都在 volume container 中定义好了,容器只需与 volume container 关联,实现了容器与 host 的解耦。

  2. 使用 volume container 的容器其 mount point 是一致的,有利于配置的规范和标准化,但也带来一定的局限,使用时需要综合考虑。