网站第一屏一般做多大,今天昆明刚刚发生的新闻,wordpress调用多个标签,做3D打印样品用什么外贸网站好在Docker中若要想实现容器数据的持久化#xff08;所谓的数据持久化即数据不随着Container的结束而销毁#xff09;#xff0c;需要将数据从宿主机挂载到容器中。目前Docker提供了三种不同的方式将数据从宿主机挂载到容器中。 #xff08;1#xff09;Volumes#xff1a;…在Docker中若要想实现容器数据的持久化所谓的数据持久化即数据不随着Container的结束而销毁需要将数据从宿主机挂载到容器中。目前Docker提供了三种不同的方式将数据从宿主机挂载到容器中。 1VolumesDocker会管理宿主机文件系统的一部分资源默认位于 /var/lib/docker/volumes 目录中最常用的方式 [rootlocalhost ~]# docker run -it -v /opt/ centos /bin/bash [root977816d61f04 /]# touch /opt/test.txt [root977816d61f04 /]# ls /opt/ test.txt [rootlocalhost ~]# ls /var/lib/docker/volumes/38fe1df3b5049eecb6954b65f738a34e8673b2a026a9edeb0c62aaed15c6e99f/_data/ test.txt 目前所有Container的数据都保存在/var/lib/docker/volumes/目录下边由于没有在创建时指定卷所以Docker帮我们默认创建许多匿名就上面这一堆很长ID的名字卷。 2bind mounts意为着可以指定存储在宿主机系统的任意位置比较常用的方式
但是bind mounts在不同的宿主机系统之间是不可移植的比如Windows和Linux的存储结构是不一样的bind mount所指向的host目录也不能一样。这也是为什么bind mount不能出现在Dockerfile中的原因因为会导致Dockerfile无法移植。 3tmpfs挂载存储在宿主机系统的内存中而不会写入宿主机的文件系统一般都不会用的方式 8.2、Volume的基本使用
8.2.1、管理卷
创建一个自定义容器卷 [rootlocalhost ~]# docker volume create nginx-data nginx-data 查看所有容器卷 [rootlocalhost ~]# docker volume ls DRIVER VOLUME NAME local nginx-data 查看指定容器卷详情信息 [rootlocalhost ~]# docker volume inspect nginx-data [ { CreatedAt: 2020-12-02T23:21:4508:00, Driver: local, Labels: {}, Mountpoint: /var/lib/docker/volumes/nginx-data/_data, Name: nginx-data, Options: {}, Scope: local } ] 8.2.2、创建使用指定卷的容器
有了自定义容器卷我们可以创建一个使用这个数据卷的容器 [rootlocalhost ~]# docker run -d -it --namenginx -p 8000:80 -v nginx-data:/usr/share/nginx/html nginx 930bcea1135707a66056b6cdc294847925786a6a32692c8873c630cabd8e3681 [rootlocalhost ~]# docker exec -it nginx /bin/bash root930bcea11357:/# ls /usr/share/nginx/html/ 50x.html index.html 选项-v代表挂载数据卷这里使用自定数据卷nginx-data并且将数据卷挂载到 /usr/share/nginx/html 这个目录是yum安装nginx的默认网页目录。如果没有通过-v指定那么Docker会默认帮我们创建匿名数据卷进行映射和挂载。 注意
数据卷下无文件显示容器对应目录下的文件数据卷下有文件显示数据卷原有文件并将容器对应目录的文件隐藏显示数据卷文件 可以看到网页目录下有两个默认页面这时我们可以查看宿主机文件系统的数据 [rootlocalhost ~]# ls /var/lib/docker/volumes/nginx-data/_data/ 50x.html index.html 可以看到容器里面的两个默认页面由此可知Volume帮我们做了类似于一个软链接的功能。在容器里边的改动我们可以在宿主机里感知而在宿主机里面的改动在容器里边可以感知到。 如果我们手动stop并且remove当前nginx容器我们会发现容器卷里面的文件还在并没有随着容器被删除掉。 [rootlocalhost ~]# docker stop nginx nginx [rootlocalhost ~]# docker rm nginx nginx [rootlocalhost ~]# ls /var/lib/docker/volumes/nginx-data/_data/ 50x.html index.html 所以在数据卷里边的东西是可以持久化的。如果下次还需要创建一个nginx容器那么时候复用当前数据卷里面文件的。 [rootlocalhost ~]# docker run -d -it --namenginx2 -p 8001:80 -v nginx-data:/usr/share/nginx/html nginx f21efa07e66d89004327f6cbb705af9d4464b0f423ffdcec54d9c352b90cbdcb [rootlocalhost ~]# docker exec -it nginx2 /bin/bash rootf21efa07e66d:/# ls /usr/share/nginx/html 50x.html index.html 此外我们还可以启动多个nginx容器实例共享同一个数据卷。数据卷的复用性和扩展性较强的。 8.2.3、清理卷
如果不再使用自定义数据卷了那么可以手动清理掉 [rootlocalhost ~]# docker volume rm nginx-data nginx-data [rootlocalhost ~]# docker volume ls DRIVER VOLUME NA 8.3、Bind Mounts的基本使用
8.3.1 使用卷创建一个容器 [rootlocalhost ~]# docker run -d -it --namenginx -p 800:80 -v /wwwroot:/usr/share/nginx/html nginx d7e201c67bdfbd88ac2aa04590889ac4cc3e2f473f90b1b12dd7c5158f1ec306 这里指定了将宿主机上的 /wwwroot 目录如果没有会自动创建挂载到 /usr/share/nginx/html 这个目录是yum安装nginx的默认网页目录。 docker挂载的默认权限是读写(rw),用户也可以通过ro指定为只读 [rootlocalhost ~]# docker run -d -it --namenginx -p 800:80 -v /wwwroot:/usr/share/nginx/html:ro nginx d7e201c67bdfbd88ac2aa04590889ac4cc3e2f473f90b1b12dd7c5158f1ec306 [rootlocalhost ~]# docker exec -it nginx /bin/bash rootd7e201c67bdf:/# ls /usr/share/nginx/html 可以看到与volumes不同bind mounts的方式会隐藏掉被挂载目录里面的内容如果非空的话这里是/usr/share/nginx/html 目录下的内容被隐藏掉了因此我们看不到。 但是我们可以将宿主机上的文件随时挂载到容器中
新建一个index.html并在容器中查看 [rootlocalhost ~]# echo test html /wwwroot/index.html rootd7e201c67bdf:/# ls /usr/share/nginx/html index.html 8.3.2、验证绑定 [rootlocalhost ~]# docker inspect nginx HostConfig: { Binds: [ /wwwroot:/usr/share/nginx/html ], 8.3.3、清理 [rootlocalhost ~]# docker stop nginx nginx [rootlocalhost ~]# docker rm nginx nginx [rootlocalhost ~]# ls /wwwroot/ index.html 同volumes一样当我们清理掉容器之后挂载目录里面的文件仍然还在不会随着容器的结束而消失从而实现数据持久化。
8.4、数据卷容器
8.4.1、数据卷容器概述 用户需要在容器之间共享一些持续性更新的数据时可以使用数据卷容器。数据容器也是一个普通的容器。里边带有设置好的数据卷专门提供给其他容器挂载使用。 通过--volumes-from 数据卷容器名来实现。 8.4.2、创建数据卷容器 [rootlocalhost ~]# docker run -it -v /dbdata:/dbdata --namedbdata centos /bin/bash [root56c18602fb79 /]# exit exit //创建一个数据卷容器并在其中创建一个数据卷挂载到/dbdata 进入test1容器创建文件测试 [rootlocalhost ~]# docker run -it --volumes-from dbdata --name test1 centos /bin/bash [root13ab94ee6fde /]# ls anaconda-post.log bin dbdata dev etc home lib lib64 lostfound media mnt opt proc root run sbin srv sys tmp usr var [root13ab94ee6fde /]# touch dbdata/crushlinux //在test1容器的/dbdata目录创建测试文件 [root13ab94ee6fde /]# exit exit 进入test2容器验证结果 [rootlocalhost ~]# docker run -it --volumes-from dbdata --name test2 centos:1 /bin/bash [rootfe011c0bb730 /]# ls anaconda-post.log bin dbdata dev etc home lib lib64 lostfound media mnt opt proc root run sbin srv sys tmp usr var [rootfe011c0bb730 /]# ls dbdata/ crushlinux //测试文件还在 说明
可以多次使用--volume-from参数从多个容器挂载多个目录。也可以从其他已经挂载了数据卷的容器来挂载数据卷类似传递。再次强调如果删除了挂载的容器数据卷不会被自动删除。如果要删除容器的时候同时删除数据卷需加上-v参数。