前言
?前面兩篇文章講述的是構(gòu)建docker鏡像的三種方式以及如何搭建docker私有倉(cāng)庫(kù),而本文將講述有關(guān)docker中的數(shù)據(jù)管理及端口映射的內(nèi)容。
- 揭開(kāi)Docker的面紗 – 基礎(chǔ)理論梳理和安裝流程演示 http://www.sfodin.cn/Linux/2020-04/163003.htm
- Docker基礎(chǔ)命令詳解 – 鏡像及容器操作 http://www.sfodin.cn/Linux/2020-04/163005.htm
- 深入理解Docker的硬件資源控制與驗(yàn)證 http://www.sfodin.cn/Linux/2020-04/163006.htm
- Docker網(wǎng)絡(luò)模式與配置Docker自定義網(wǎng)絡(luò)(bridge模式) http://www.sfodin.cn/Linux/2020-04/163007.htm
- Docker構(gòu)建鏡像的三種方式(Dockerfile初步) http://www.sfodin.cn/Linux/2020-04/163008.htm
- 教你如何搭建Docker私有倉(cāng)庫(kù) http://www.sfodin.cn/Linux/2020-04/163009.htm
docker數(shù)據(jù)管理
什么是docker的數(shù)據(jù)管理?
? 先想一想關(guān)于Linux系統(tǒng)管理中的磁盤管理,其實(shí)本質(zhì)上也都是對(duì)存儲(chǔ)的數(shù)據(jù)進(jìn)行管理,docker中的數(shù)據(jù)管理也是如此。docker中,對(duì)數(shù)據(jù)的管理主要在于兩個(gè)方面,其一是為了方便查看容器內(nèi)的數(shù)據(jù),其二是實(shí)現(xiàn)多個(gè)容器的數(shù)據(jù)共享。
? 那么管理docker的數(shù)據(jù)的方式有兩個(gè):數(shù)據(jù)卷和數(shù)據(jù)卷容器。下面會(huì)逐個(gè)介紹。
docker的數(shù)據(jù)管理方式
數(shù)據(jù)卷
? 數(shù)據(jù)卷是一個(gè)供容器使用的特殊目錄,位于容器中,可將宿主機(jī)的目錄掛載到數(shù)據(jù)卷上,對(duì)數(shù)據(jù)卷的修改操作立刻可見(jiàn)(其實(shí)上篇文章中已經(jīng)接觸到了),并且更新數(shù)據(jù)不會(huì)影響鏡像,從而實(shí)現(xiàn)數(shù)據(jù)在宿主機(jī)與容器之間的遷移。
? 其實(shí),在docker中,數(shù)據(jù)卷的使用可以類比于Linux下對(duì)目錄進(jìn)行的mount操作。
數(shù)據(jù)卷容器
? 顯然,數(shù)據(jù)卷容器和數(shù)據(jù)卷不是同一個(gè)概念,但是作用是一樣的。只不過(guò)數(shù)據(jù)卷容器的目的是在容器間共享一些數(shù)據(jù)。數(shù)據(jù)卷容器就可以認(rèn)為是一個(gè)普通的容器,只不過(guò)是專門用來(lái)提供數(shù)據(jù)卷給其他容器掛載使用的。
數(shù)據(jù)卷和數(shù)據(jù)卷容器的概念和區(qū)別可以通過(guò)下圖來(lái)理解:
數(shù)據(jù)卷就是宿主機(jī)中提供的一塊空間(目錄空間)掛載給容器,使得容器與宿主機(jī)之間可以共享文件數(shù)據(jù),而數(shù)據(jù)卷容器是將一個(gè)容器內(nèi)的一個(gè)空間(目錄)掛載給另一個(gè)容器,使得容器與容器之間共享文件數(shù)據(jù)。
下面來(lái)實(shí)際來(lái)操作一下是如何通過(guò)中兩種方式對(duì)docker進(jìn)行數(shù)據(jù)管理的。
如何進(jìn)行對(duì)docker的數(shù)據(jù)管理?
1、創(chuàng)建數(shù)據(jù)卷(為后面的數(shù)據(jù)卷容器做準(zhǔn)備)
#目前是空鏡像和空容器環(huán)境 [root@localhost ~]# docker run -d -v /data/data1 -v /data/data2 --name web httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 123275d6e508: Pull complete e984dd982a6e: Pull complete 963280e5cf81: Pull complete 6faf90d050b2: Pull complete 962b56984bb0: Pull complete Digest: sha256:d5dc0d279039da76a8b490d89a5c96da83a33842493d4336b42ccdfbd36d7409 Status: Downloaded newer image for httpd:latest 8e21b3e1366970633f01ae4d77b0f55a2d52782997138cc1e8ab70904d5f487b
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e21b3e13669 httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp web [root@localhost ~]# docker exec -it 8e21b3e13669 /bin/bash root@8e21b3e13669:/usr/local/apache2# ls /data/ data1 data2
2、掛載主機(jī)目錄作為數(shù)據(jù)卷:數(shù)據(jù)卷實(shí)際案例
[root@localhost ~]# docker run -d -v /var/www:/data/data1 --name web1 httpd:latest 5ff89e3dbdd25bbb1b105678c50364758fe62cb5c7d10aa5f47e8865ddd5e8d8 #參數(shù)解釋: -d:守護(hù)進(jìn)程 -v:數(shù)據(jù)卷操作 /var/www:宿主機(jī)目錄將被掛載的目錄 /data/data1:容器中數(shù)據(jù)卷 --name:容器名稱
3、測(cè)試
[root@localhost ~]# cd /var/www/ [root@localhost www]# touch file [root@localhost www]# ls file [root@localhost www]# docker exec -it web1 /bin/bash #在新的容器中查看目錄內(nèi)容 root@5ff89e3dbdd2:/usr/local/apache2# ls /data/data1/ file root@5ff89e3dbdd2:/usr/local/apache2#
4、數(shù)據(jù)卷容器實(shí)際案例
#前面已經(jīng)進(jìn)行創(chuàng)建了一個(gè)名為web的容器,其中所創(chuàng)建的數(shù)據(jù)卷分別掛載到了/data/data1與/data/data2目錄上,使用--volumes-from選項(xiàng)來(lái)掛載web容器中的數(shù)據(jù)卷到新的容器 [root@localhost ~]# docker run -it --volumes-from web --name web_test httpd:latest /bin/bash root@afafb9631fc9:/usr/local/apache2# ls / bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@afafb9631fc9:/usr/local/apache2# ls /data/ data1 data2 #兩個(gè)目錄(數(shù)據(jù)卷容器)提供給新的容器web_test使用
我們可以進(jìn)行測(cè)試,在其中一個(gè)目錄中創(chuàng)建文件,如何進(jìn)入原本的數(shù)據(jù)卷容器中查看是否有該文件
root@afafb9631fc9:/usr/local/apache2# cd /data/data1 root@afafb9631fc9:/data/data1# touch newfile root@afafb9631fc9:/data/data1# exit exit [root@localhost ~]# docker exec -it web /bin/bash root@8e21b3e13669:/usr/local/apache2# ls /data/data1 newfile
? 測(cè)試成功了,這樣可以通過(guò)數(shù)據(jù)卷容器實(shí)現(xiàn)容器之間的數(shù)據(jù)共享了。通過(guò)這些機(jī)制,即使容器在運(yùn)行過(guò)程中出現(xiàn)故障,用戶也不必?fù)?dān)心數(shù)據(jù)發(fā)生丟失了,如果發(fā)生意外,只需要快速重新創(chuàng)建容器即可。
下面簡(jiǎn)述一下有關(guān)docker的端口映射
docker網(wǎng)絡(luò)通信之端口映射
? 其實(shí)docker的端口映射應(yīng)該在先前的網(wǎng)絡(luò)部分進(jìn)行講述的,還記得docker0網(wǎng)絡(luò)模式的原理嗎?其實(shí),在docker中,默認(rèn)的選擇是docker網(wǎng)橋模式,而實(shí)現(xiàn)網(wǎng)絡(luò)通信也是依賴于NAT地址轉(zhuǎn)換,簡(jiǎn)單而言就是外部網(wǎng)絡(luò)可以通過(guò)訪問(wèn)宿主機(jī)的ip地址結(jié)合端口號(hào)對(duì)docker內(nèi)部的容器進(jìn)行訪問(wèn)通信獲取數(shù)據(jù)信息。
? 端口映射有兩種命令方式,一種是加入選項(xiàng)-P,另一種是使用-p。-P表示docker自己分配一個(gè)端口,適用于自己未規(guī)定而又避免與其他容器沖突端口的情況,-p則表示指定端口,適用于自定義的情況,節(jié)省資源和優(yōu)化管理的情況。
下面看看實(shí)際案例:
[root@localhost ~]# docker run -d -P httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 123275d6e508: Pull complete e984dd982a6e: Pull complete 963280e5cf81: Pull complete 6faf90d050b2: Pull complete 962b56984bb0: Pull complete Digest: sha256:d5dc0d279039da76a8b490d89a5c96da83a33842493d4336b42ccdfbd36d7409 Status: Downloaded newer image for httpd:latest e7a9cda3fea1904401b274a35b51d93a23b95cd0c8e548bb4982e21e8586d657 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e7a9cda3fea1 httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp charming_ride
我們測(cè)試一下這個(gè)Apache服務(wù)是否成功,從獲取到成功差不多就30s
使用小寫嘗試一下:
[root@localhost ~]# docker run -d -p 54544:80 httpd 26d52f0d0a49a8c3b87aa4f4eb251eee080c9ba776902987e0173d7a290412d0 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 26d52f0d0a49 httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:54544->80/tcp busy_khorana e7a9cda3fea1 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:32768->80/tcp charming_ride
測(cè)試驗(yàn)證:
看了都成功了。接下來(lái)說(shuō)說(shuō)容器的互聯(lián)。
容器互聯(lián)
容器互聯(lián)是指通過(guò)容器的名稱在容器間建立一條專門的網(wǎng)絡(luò)通信隧道從而實(shí)現(xiàn)容器的互聯(lián)。在使用docker run 命令的時(shí)候使用–link選項(xiàng)就可以實(shí)現(xiàn)容器之間的互聯(lián)通信。
格式為:–link name:alias
name——容器名
alias——這個(gè)連接的別名
需要注意的是:容器互聯(lián)是通過(guò)容器的名稱來(lái)執(zhí)行的,–name選項(xiàng)可以給容器創(chuàng)建一個(gè)友好的名稱,這個(gè)名稱是唯一的,即不可重復(fù)。
具體案例演示:
1、創(chuàng)建容器
[root@localhost ~]# docker run -itd -P --name test1 CentOS /bin/bash Unable to find image 'centos:latest' locally latest: Pulling from library/centos 8a29a15cefae: Pull complete Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700 Status: Downloaded newer image for centos:latest 9f2ce34c7867d8b159201f9a3521a28570a3843a8fca08a60f58b12ff7565188
2、創(chuàng)建接收容器
[root@localhost ~]# docker run -itd -P --name test2 --link test1:test1 centos /bin/bash f3c2974dcd19796cd8a48ecaf398370f891887cbfb92a192e8d7e706ccbefd62
3、進(jìn)入一個(gè)容器中進(jìn)行測(cè)試(ping)
[root@localhost ~]# docker exec -it test2 /bin/bash [root@f3c2974dcd19 /]# ping test1 PING test1 (172.17.0.2) 56(84) bytes of data. 64 bytes from test1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.089 ms 64 bytes from test1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.070 ms 64 bytes from test1 (172.17.0.2): icmp_seq=3 ttl=64 time=0.062 ms 64 bytes from test1 (172.17.0.2): icmp_seq=4 ttl=64 time=0.065 ms 64 bytes from test1 (172.17.0.2): icmp_seq=5 ttl=64 time=0.063 ms ^C --- test1 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 13ms rtt min/avg/max/mdev = 0.062/0.069/0.089/0.014 ms
需要注意的是,我們的容器需要是up的狀態(tài)否則會(huì)遇到報(bào)錯(cuò)。