Linux操作文档——Docker数据管理


一、Data Volume

bind mountdocker managed volume
volume 位置可任意指定/var/lib/docker/volumes/…
有mount point 影响隐藏并替换为 volume原有数据复制到 volume
是否支持单个文件支持不支持,只能是目录
权限控制可设置为只读,默认为读写权限无控制,均为读写权限
移植性移植性弱,与 host path 绑定移植性强,无需指定 host 目录

1、Bind mount

使用nginx:latest镜像创建—个名为testweb的容器,并且将宿主机的/usr/share/nginx/html目录挂载到容器的/root/html目录上

[root@docker01 ~]# mkdir html
[root@docker01 ~]# cd html/
[root@docker01 html]# echo "docker-test1" >index.html
[root@docker01 ~]# docker run -itd --name testweb -p80 --restart=always -v /root/html:/usr/share/nginx/html nginx:latest
[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4f55734e7447        nginx:latest        "/docker-entrypoint.…"   6 seconds ago       Up 4 seconds        0.0.0.0:32770->80/tcp    testweb
916df5b5b4c0        registry:2          "/entrypoint.sh /etc…"   7 days ago          Up 7 days           0.0.0.0:5000->5000/tcp   registry
[root@docker01 ~]# curl 127.0.0.1:32770
docker-test1

限制容器对挂载目录只读权限

[root@docker01 ~]# docker run -itd --name testweb1 -p80 --restart=always -v /root/html:/usr/share/nginx/html:ro nginx:latest

限制容器对挂载文件只读权限

[root@docker01 ~]# docker run -itd --name testweb2 -p80 --restart=always -v /root/html/index.html:/usr/share/nginx/html/index.html:ro nginx:latest

2、Docker Manager Volume

不需要指定 mount 源数据,-v参数可以专门提供数据卷给其他容器挂载使用。

[root@docker01 ~]# docker run -itd --name test1 -P -v /usr/share/nginx/html nginx:latest

查看容器配置信息

[root@docker01 ~]# docker inspect test1
[
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c2c3d39174c2768de8c2af921853d2db4a258223eb30ecddcbfe9854785f4527",
                "Source": "/var/lib/docker/volumes/c2c3d39174c2768de8c2af921853d2db4a258223eb30ecddcbfe9854785f4527/_data",
                "Destination": "/usr/share/nginx/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ]
]

手动创建volume

[root@docker01 ~]# docker volume create web
web
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               c2c3d39174c2768de8c2af921853d2db4a258223eb30ecddcbfe9854785f4527
local               web
[root@docker01 ~]# docker run -itd -P --name web1 -v web:/usr/share/nginx nginx:latest 
[root@docker01 ~]# docker volume inspect web 
[
        "Mounts": [
            {
                "Type": "volume",
                "Name": "web",
                "Source": "/var/lib/docker/volumes/web/_data",
                "Destination": "/usr/share/nginx",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ]
]

二、容器与容器的数据共享

volume container:给其他容器提供volume存储卷的容器。它可以提供bind mount,也可以提供docker manager volume。
创建一个vc_data容器

[root@docker01 ~]# docker create --name vc_data -v ~/html:/usr/share/nginx/html -v /other/useful/tools busybox

使用vc容器

[root@docker01 ~]# docker run -itd --name nginx1 -P --volumes-from vc_data nginx

三、容器的跨主机数据共享

[root@docker03 ~]# yum -y install nfs-utils
[root@docker03 ~]# mkdir /data
[root@docker03 ~]# vim /etc/exports
[root@docker03 ~]# systemctl start rpcbind
[root@docker03 ~]# systemctl start nfs
[root@docker03 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@docker03 ~]# systemctl enable rpcbind
[root@docker03 ~]# echo "nginx-test" > /data/index.html
[root@docker01 ~]# showmount -e 192.168.1.30
Export list for 192.168.1.30:
/data *
[root@docker01 ~]# mkdir /htdocs
[root@docker01 ~]# mount -t nfs 192.168.1.30:/data /htdocs
[root@docker01 ~]# cat /htdocs/index.html 
nginx-test
[root@docker01 ~]# docker run -itd --name web-1 -P -v /htdocs:/usr/local/apache2/htdocs httpd:latest 
[root@docker01 ~]# curl 127.0.0.1:32778
nginx-test
[root@docker02 ~]# mkdir /htdocs
[root@docker02 ~]# mount -t nfs 192.168.1.30:/data /htdocs
[root@docker02 ~]# docker run -itd --name web-1 -P -v /htdocs:/usr/local/apache2/htdocs httpd:latest
[root@docker02 ~]# curl 127.0.0.1:32768
nginx-test
posted @ 2020-09-08 21:51  高中僧  阅读(35)  评论(0)    收藏  举报