实战案例:数据卷容器
启动一个数据卷容器Server
先启动一个容器,并挂载宿主机的数据目录
范例:使用之前的镜像创建数据卷容器
#数据卷容器一般不需映射端口
[root@ubuntu1804 ~]#docker run -d --name volume-server -v /data/bin/catalina.sh:/apps/tomcat/bin/catalina.sh:ro -v /data/testapp:/data/tomcat/webapps/testapp tomcat-web:app1
109a34c5a83b23bc57b16fc8b1e19104d3d522a8c62e1e98d8e69577b01739b5
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
109a34c5a83b tomcat-web:app1 "/apps/tomcat/bin/ru…" 14 seconds ago Up 13 seconds 8009/tcp, 8080/tcp volume-server
启动多个数据卷容器Client
[root@ubuntu1804 ~]#docker run -d --name client1 --volumes-from volume-server -p 8081:8080 tomcat-web:app1
fe6ce0548dfee924cd39a8d86d5ed0e8ce9ea65323742f1336fa3b002c1b4a8c
[root@ubuntu1804 ~]#docker run -d --name client2 --volumes-from volume-server -p 8082:8080 tomcat-web:app1
10397838df9a489d7af2112850d4285bff5c2c262ea05cc9c5fb265af538f2c8
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10397838df9a tomcat-web:app1 "/apps/tomcat/bin/ru…" 30 seconds ago Up 27 seconds 8009/tcp, 0.0.0.0:8082->8080/tcp client2
fe6ce0548dfe tomcat-web:app1 "/apps/tomcat/bin/ru…" 40 seconds ago Up 38 seconds 8009/tcp, 0.0.0.0:8081->8080/tcp client1
109a34c5a83b tomcat-web:app1 "/apps/tomcat/bin/ru…" 2 minutes ago Up 2 minutes 8009/tcp, 8080/tcp volume-server
[root@ubuntu1804 ~]#
验证访问
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v3
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v3
进入容器测试读写
读写权限依赖于源数据卷Server容器
#进入 Server 容器修改数据
[root@ubuntu1804 ~]#docker exec -it volume-server bash
[root@109a34c5a83b /]# cat /data/tomcat/webapps/testapp/index.html
testapp v3
[root@109a34c5a83b /]# echo testapp v4 > /data/tomcat/webapps/testapp/index.html
[root@109a34c5a83b /]#
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v4
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v4
#进入 Client 容器修改数据
[root@ubuntu1804 ~]#docker exec -it client1 bash
[root@fe6ce0548dfe /]# cat /data/tomcat/webapps/testapp/index.html
testapp v4
[root@fe6ce0548dfe /]# echo testapp v4 > /data/tomcat/webapps/testapp/index.html
[root@fe6ce0548dfe /]# cat /data/tomcat/webapps/testapp/index.html
testapp v5
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v5
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v5
在宿主机直接修改
[root@ubuntu1804 ~]#cat /data/testapp/index.html
testapp v5
[root@ubuntu1804 ~]#echo testapp v6 > /data/testapp/index.html
[root@ubuntu1804 ~]#cat /data/testapp/index.html
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v6
[root@ubuntu1804 ~]#
[root@ubuntu1804 ~]#docker exec -it volume-server bash
[root@109a34c5a83b /]# cat /data/tomcat/webapps/testapp/index.html
testapp v6
[root@109a34c5a83b /]#
关闭卷容器Server测试能否启动新容器
关闭卷容器,仍然可以创建新的client容器及访问旧的client容器
[root@ubuntu1804 ~]#docker stop volume-server
volume-server
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10397838df9a tomcat-web:app1 "/apps/tomcat/bin/ru…" 9 minutes ago Up 9 minutes 8009/tcp, 0.0.0.0:8082->8080/tcp client2
fe6ce0548dfe tomcat-web:app1 "/apps/tomcat/bin/ru…" 10 minutes ago Up 10 minutes 8009/tcp, 0.0.0.0:8081->8080/tcp client1
[root@ubuntu1804 ~]#docker run -d --name client3 --volumes-from volume-server -p 8083:8080 tomcat-web:app1
458df991688c3dbc69d824f889616e0a7534ce18543c8559162f3609fbb26b53
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
458df991688c tomcat-web:app1 "/apps/tomcat/bin/ru…" About a minute ago Up About a minute 8009/tcp, 0.0.0.0:8083->8080/tcp client3
10397838df9a tomcat-web:app1 "/apps/tomcat/bin/ru…" 11 minutes ago Up 11 minutes 8009/tcp, 0.0.0.0:8082->8080/tcp client2
fe6ce0548dfe tomcat-web:app1 "/apps/tomcat/bin/ru…" 11 minutes ago Up 11 minutes 8009/tcp, 0.0.0.0:8081->8080/tcp client1
109a34c5a83b tomcat-web:app1 "/apps/tomcat/bin/ru…" 13 minutes ago Exited (137) About a minute ago volume-server
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8083/testapp/
testapp v6
删除源卷容器Server,访问client和创建新的client容器
删除数据卷容器后,旧的client 容器仍能访问,但无法再创建新的client容器
[root@ubuntu1804 ~]#docker rm -fv volume-server
volume-server
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
458df991688c tomcat-web:app1 "/apps/tomcat/bin/ru…" 4 minutes ago Up 4 minutes 8009/tcp, 0.0.0.0:8083->8080/tcp client3
10397838df9a tomcat-web:app1 "/apps/tomcat/bin/ru…" 14 minutes ago Up 14 minutes 8009/tcp, 0.0.0.0:8082->8080/tcp client2
fe6ce0548dfe tomcat-web:app1 "/apps/tomcat/bin/ru…" 14 minutes ago Up 14 minutes 8009/tcp, 0.0.0.0:8081->8080/tcp client1
[root@ubuntu1804 ~]#docker run -d --name client4 --volumes-from volume-server -p 8084:8080 tomcat-web:app1
Unable to find image 'tomcat-web:app1' locally
docker: Error response from daemon: pull access denied for tomcat-web, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8083/testapp/
testapp v6
重新创建容器卷Server
重新创建容器卷后,还可继续创建新client 容器
[root@ubuntu1804 ~]#docker run -d --name volume-server -v /data/bin/catalina.sh:/apps/tomcat/bin/catalina.sh:ro -v /data/testapp:/data/tomcat/webapps/testapp tomcat-web:app1
ee0dacff6a4531dc8ecc1d416b449e087d8bf27dad2d6a6e515faf10455a1cc0
[root@ubuntu1804 ~]#docker run -d --name client4 --volumes-from volume-server -p 8084:8080 tomcat-web:app1
dcd002fe87887a0c5d1f62b24cd7665e42ee864c975d1a2953c383af0cb65a70
[root@ubuntu1804 ~]#curl 127.0.0.1:8084/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8081/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8082/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8083/testapp/
testapp v6
[root@ubuntu1804 ~]#curl 127.0.0.1:8084/testapp/
testapp v6
数据卷容器总结
将提供卷的容器Server 删除,已经运行的容器Client依然可以使用挂载的卷,因为容器是通过挂载访问数据的,但是无法创建新的卷容器客户端,但是再把卷容器Server创建后即可正常创建卷容器Client,此方式可以用于线上共享数据目录等环境,因为即使数据卷容器被删除了,其他已经运行的容器依然可以挂载使用
数据卷容器可以作为共享的方式为其他容器提供文件共享,类似于NFS共享,可以在生产中启动一个实例挂载本地的目录,然后其他的容器分别挂载此容器的目录,即可保证各容器之间的数据一致性
数据卷容器的 Server 和 Client 可以不使用同一个镜像生成
本文链接:http://www.yunweipai.com/34868.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52674.html