使用容器時會產生一些紀錄檔或其他檔案,或是我們需要把容器內的資料進行備份,甚至多個容器之間進行資料共用,這樣就會涉及容器的資料管理操作。
容器中管理資料主要有兩種方式: **1. 資料卷 ** 2. 資料卷容器
資料卷:掛載宿主系統的儲存空間
資料卷容器:掛載容器的儲存空間
如果使用者需要在容器之間共用一些持續更新的資料,最簡單的方式是使用資料卷容器,資料卷容器其實是一個普通的容器,專門用來提供資料卷供其它容器掛載
掛載宿主機目錄
實現容器和宿主機資料共用
[root@5centos /]# docker run -v /tset1:/doctest1 --name web1 -it 20.0.0.5:5000/httpd /bin/bash
[root@d7ef3f247970 /]# cd doctest1/
[root@d7ef3f247970 doctest1]# touch abc.mp4
宿主機驗證
[root@5centos /]# cd tset1/
[root@5centos tset1]# ls
abc.mp4
容器和容器資料共用
建立資料卷容器test50
[root@5centos /]# docker run --name test50 -v /data1 -v /data2 -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
[root@de27be0d57bb /]# ls
bin data2 etc lib lost+found mnt proc run srv tmp var
data1 dev home lib64 media opt root sbin sys usr
[root@de27be0d57bb /]# cd data1
[root@de27be0d57bb data1]# touch 123.avi
[root@de27be0d57bb data1]# cd /data2
[root@de27be0d57bb data2]# touch 456.avi
建立新容器test60並掛載資料卷容器目錄
[root@5centos ~]# docker run -it --volumes-from test50 --name test60 centos /bin/bash
[root@a40a840da387 /]# ls
bin data2 etc lib lost+found mnt proc run srv tmp var
data1 dev home lib64 media opt root sbin sys usr
[root@a40a840da387 /]# cd data1
[root@a40a840da387 data1]# ls
123.avi
[root@a40a840da387 data1]# cd /data2
[root@a40a840da387 data2]# ls
456.avi
-p 指定埠 2080對映內部80
[root@5centos ~]# docker run -d -p 2080:80 httpd:centos
6df38d1adcbeb677cd66dc2f25df35a1ba924ab0fad6113860a52481fab45097
-P 隨機埠
[root@5centos ~]# docker run -d -P httpd:centos
0b01169861db122032bb512e23977fd8660fd5c7a7485f0df558cac089b9c5fc
建立容器 apa01,埠隨機
[root@5centos /]# docker run -itd -P --name apa01 centos:8 /bin/bash
4c1f79e9d7cea3f747975b79388ae3a6bb41df81d8bbb31a7f8a5f16f7993c1b
建立容器 apa02,埠隨機,a1是別名,隨便取,記得住就行
[root@5centos /]# docker run -itd -P --name apa02 --link apa01:a1 centos:8 /bin/bash
ae9d8131a2689be66393174eb9f6a6e1a5c36abe50400bdffe47198a33b0fdb8
進入容器,ping測試
apa01
[root@5centos /]# docker exec -it apa01 /bin/bash
[root@4c1f79e9d7ce /]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
40: eth0@if41: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
apa02
[root@ae9d8131a268 /]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
42: eth0@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
測試
[root@ae9d8131a268 /]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.111 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.061 ms
^C
--- 172.17.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.059/0.072/0.111/0.023 ms
已經互通啦!