欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker 之间如何共享数据?

程序员文章站 2024-03-15 11:22:47
...

容器之间共享数据


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

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

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


[[email protected] ~]# 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)查看当前主页内容。 

[[email protected] ~]# curl 192.168.179.99:81
share directory
[[email protected] ~]# curl 192.168.179.99:82
share directory
[[email protected] ~]# curl 192.168.179.99:83
share directory

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

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

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

相关标签: Docker docker