日韩精品成人无码专区免费-国产99久久久久久免费看-国产精品丝袜久久久久久不卡-国产精品无码一区二区三区

Docker數(shù)據(jù)持久化和容器與容器的數(shù)據(jù)共享

發(fā)布時間:2025-04-05 點擊:41
云計算
一、前言
當(dāng)我們使用docker創(chuàng)建一個mysql的container, 數(shù)據(jù)是存儲在container內(nèi)的.
如果有一天不小心執(zhí)行了docker rm $(docker ps -aq)刪除所有container. 那么mysql里的數(shù)據(jù)也會被刪掉, 這是不安全的.
我們需要將數(shù)據(jù)持久化, 存儲在container外部. 即使刪除container也不會刪除原有的數(shù)據(jù).
二、容器的缺陷
容器中的數(shù)據(jù)可以存儲在容器層。但是將數(shù)據(jù)存放在容器層存在以下問題:
1.數(shù)據(jù)不是持久化。意思是如果容器刪除了,這些數(shù)據(jù)也就沒了
2.主機(jī)上的其它進(jìn)程不方便訪問這些數(shù)據(jù)
3.對這些數(shù)據(jù)的i/o會經(jīng)過存儲驅(qū)動,然后到達(dá)主機(jī),引入了一層間接層,因此性能會有所下降
三、data volume有兩種掛載方式:
1)bind mount(用戶管理):將宿主機(jī)上的某個目錄或文件(不可以是沒有格式化的磁盤文件),掛載到容器中,默認(rèn)在容器內(nèi)對此目錄是有讀寫權(quán)限的,如果只需要向容器內(nèi)添加文件,不希望覆蓋目錄,需要注意源文件必須存在,否則會被當(dāng)做一個目錄bind mount給容器。
2)docker manager volume(docker自動管理):不需要指定源文件,只需要指定mount point(掛載點)。把容器里面的目錄映射到了本地。
這種方式相比bind mount 缺點是無法限制對容器里邊目錄或文件的權(quán)限。
使用第二種掛載方式,-v 掛載時,不指定源文件位置,則默認(rèn)掛載的路徑是:
[root@sqm-docker01 _data]# pwd/var/lib/docker/volumes/dd173640edd5b0205bb02f3c4139647be12528b38289b9f93f18123a6b1266a8/_data#當(dāng)有目錄掛載時,默認(rèn)在/var/lib/docker/volumes/下會生成一串hash值,hash值下有一個_data的目錄,容器內(nèi)映射的文件就在此路徑下。四、storage driver
數(shù)據(jù)存儲方式
centos7版本的docker,storage driver(數(shù)據(jù)存儲方式)為:overlay2 ,backing filesystem(文件系統(tǒng)類型): xfs
可使用 “docker inspect 容器名稱” 來查看數(shù)據(jù)存儲方式
五、data volume
(bind mount)
持久化存儲:本質(zhì)上是dockerhost文件系統(tǒng)中的目錄或文件,能夠直接被mount到容器的文件系統(tǒng)中。在運行容器時,可通過-v實現(xiàn)。
特點:
1. data volume是目錄或文件,不能是沒有格式化的磁盤(塊設(shè)備)。
容器可以讀寫volume中的數(shù)據(jù)。
volume數(shù)據(jù)可以永久保存,即使使用它的容器已經(jīng)被銷毀。
小實驗:
運行一個nginx服務(wù),做數(shù)據(jù)持久化
(1)data volume是目錄或文件,不能是沒有格式化的磁盤(塊設(shè)備)。
[root@docker01 ~]# mkdir html//創(chuàng)建測試目錄[root@docker01 ~]# cd html/[root@docker01 html]# echo this is a testfile in dockerhost. > index.html//創(chuàng)建測試網(wǎng)頁[root@docker01 ~]# docker run -itd --name testweb -v /root/html/:/usr/share/nginx/html nginx:latest//運行一個nginx容器,并掛載目錄[root@docker01 ~]# docker inspect testweb
[root@docker01 ~]# curl 172.17.0.3
注意:dockerhost上需要被掛載的源文件或目錄,必須是已經(jīng)存在,否則,會被當(dāng)作一個目錄掛載到容器中。
(2)容器可以讀寫volume中的數(shù)據(jù)。
[root@docker01 ~]# docker exec -it testweb /bin/bashroot@ef12d312a94e:/# cd /usr/share/nginx/html/root@ef12d312a94e:/usr/share/nginx/html# echo update > index.html//容器中更西部數(shù)碼頁root@ef12d312a94e:/usr/share/nginx/html# exit[root@docker01 ~]# cat html/index.html//可以看到宿主目錄的掛載目錄也更新了
(3)volume數(shù)據(jù)可以永久保存,即使,使用它的容器已經(jīng)被銷毀,也可以通過宿主機(jī)的掛在目錄重新啟動一個容器掛載這個目錄進(jìn)行訪問。
[root@docker01 ~]# docker ps -a -q |xargs docker rm -f
//刪除所有容器
[root@docker01 ~]# cat html/index.html//容器刪除之后,宿主機(jī)的測試網(wǎng)頁也在
[root@docker01 ~]# docker run -itd --name t1 -p -v /root/html/:/usr/share/nginx/html nginx:latest//基于測試網(wǎng)頁創(chuàng)建一個容器[root@docker01 ~]# docker ps
[root@docker01 ~]# curl 127.0.0.1:32768//訪問一下
[root@docker01 ~]# echo update-new > html/index.html//再次更新測試網(wǎng)頁[root@docker01 ~]# curl 127.0.0.1:32768//在宿主機(jī)更新測試網(wǎng)頁,剛剛創(chuàng)建的容器的測試網(wǎng)頁也會更新
(5)默認(rèn)掛載到容器內(nèi)的文件,容器是有讀寫權(quán)限。可以在運行容器是-v 后邊加“:ro”限制容器的寫入權(quán)限
[root@docker01 ~]# docker run -itd --name t2 -p -v /root/html/:/usr/share/nginx/html:ro nginx:latest//創(chuàng)建容器設(shè)置指讀權(quán)限[root@docker01 ~]# docker exec -it t2 /bin/bash//進(jìn)入容器root@4739c0f5d970:/# cd /usr/share/nginx/htmlroot@4739c0f5d970:/usr/share/nginx/html# echo 1234 > index.html//修改測試網(wǎng)頁(失敗,因為是只讀的)
[root@docker01 ~]# echo 654321 > html/index.html //宿主機(jī)可以更改[root@docker01 ~]# curl 127.0.0.1:32768
(6)并且還可以掛載單獨的文件到容器內(nèi)部,一般他的使用場景是:如果不想對整個目錄進(jìn)行覆蓋,而只希望添加某個文件,就可以使用掛載單個文件。
<1>測試1
[root@docker01 ~]# docker run -itd --name v6 -p -v /root/html/index.html:/usr/share/nginx/html/index.html nginx:latest[root@docker01 ~]# docker ps
[root@docker01 ~]# curl 127.0.0.1:32770
<1>測試2
[root@docker01 ~]# echo test > test.html[root@docker01 ~]# docker run -itd --name t8 -p -v /root/test.html:/usr/share/nginx/html/test.html nginx:latest
[root@docker01 ~]# curl 127.0.0.1:32772/test.html
六,docker manager volume
會自動在宿主機(jī)生成目錄,所以在掛載目錄的時候只用寫容器中的目錄。
特性和上邊的bind mount基本一樣
[root@docker01 ~]# docker run -itd --name t1 -p -v /usr/share/nginx/html nginx:latest[root@docker01 ~]# docker ps
[root@docker01 ~]# docker inspect t1
[root@docker01 _data]# cd /var/lib/docker/volumes/17c50a065a6b10ccd01ca1ce8091fdf6282dc9dcb77a0f6695906257ecc03a63/_data[root@docker01 _data]# echo this is a testfile > index.html[root@

綁定泛域名-域名及賬戶問題
免備案和備案的云服務(wù)器有何區(qū)別
DN:短域名承包前三 域名20.com以175萬美金震撼全場
取消云加速-云服務(wù)器問題
租阿里云服務(wù)器能不辦sp證嗎
微博當(dāng)?shù)溃珺BS已死?
網(wǎng)站建設(shè)哪些誤區(qū)禁止踏入
在VScode中怎么設(shè)置背景色?