日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Docker 數(shù)據(jù)管理

作者:雞湯來(lái)咯!! 更新時(shí)間: 2022-07-13 編程語(yǔ)言

提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔

文章目錄

  • Docker的數(shù)據(jù)庫(kù)管理
    • 數(shù)據(jù)卷
    • 數(shù)據(jù)卷容器
    • 容器互聯(lián) (使用centos鏡像)


Docker的數(shù)據(jù)庫(kù)管理

數(shù)據(jù)卷

數(shù)據(jù)卷是一個(gè)供容器使用的特殊目錄,位于容器中。可將宿主機(jī)的目錄掛載到數(shù)據(jù)卷上,對(duì)數(shù)據(jù)卷的修改操作立刻可見,并且更新數(shù)據(jù)不會(huì)影響鏡像,從而實(shí)現(xiàn)數(shù)據(jù)在宿主機(jī)與容器之間的遷移。數(shù)據(jù)卷的使用類似于Linux下對(duì)目錄進(jìn)行的mount操作。

docker cp 也是一種遷移

[root@zbx-agent01 ~]# docker pull centos:7
[root@localhost ~]# ls /var/www
ls: 無(wú)法訪問(wèn)/var/www: 沒(méi)有那個(gè)文件或目錄
#宿主機(jī)目錄/var/www掛載到容器中的/datal。
#注意:宿主機(jī)本地目錄的路徑必須是使用絕對(duì)路徑。如果路徑不存在,Docker會(huì)自動(dòng)創(chuàng)建相應(yīng)的路徑。
#-v選項(xiàng)可以在容器內(nèi)創(chuàng)建數(shù)據(jù)卷
[root@zbx-agent01 ~]# docker run -v /var/www:/data1 --name c7 -itd centos:7 /bin/bash
12d35de6f83674afe893d17894968dac10b43700044da66f10b45a49ae0cca74
[root@zbx-agent01 ~]# ls /var/www -d
/var/www
#進(jìn)容器
[root@zbx-agent01 ~]# docker exec -it c7 bash
[root@12d35de6f836 /]# ls
anaconda-post.log  bin  data1  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#寫入數(shù)據(jù)
[root@12d35de6f836 /]# echo "i am stevelu">/data1/abc.txt
[root@12d35de6f836 /]# exit
exit
[root@zbx-agent01 ~]# cat /var/www/abc.txt 
i am stevelu

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

數(shù)據(jù)卷容器

如果需要在容器之間共享一些數(shù)據(jù),最簡(jiǎn)單的方法就是使用數(shù)據(jù)卷容器。數(shù)據(jù)卷容器是一個(gè)普通的容器,專門提供數(shù)據(jù)卷給其他容器掛載使用。

[root@zbx-agent01 ~]# docker run -itd --name c8 -v /data1 -v /data2 -it centos:7 /bin/bash
5fce3f28819e4e29c3d4769b1ab2fc6ab2e570bf78563de6d76749ea1b71e426
[root@zbx-agent01 ~]# docker exec -it c8 bash
[root@5fce3f28819e /]# echo "i am stevelu" >/data1/abc.txt
[root@5fce3f28819e /]# echo "i am gyq" >/data2/ABC.txt
#使用--volumes-from 來(lái)掛載c8容器中的數(shù)據(jù)卷到c9容器
[root@5fce3f28819e /]# cat /data1/abc.txt 
i am stevelu
[root@5fce3f28819e /]# cat /data2/ABC.txt 

注意:使用–volumes-from參數(shù)所掛載數(shù)據(jù)卷的容器自身并不需要保持在運(yùn)行狀態(tài)。
在這里插入圖片描述
如果刪除了掛載的容器,數(shù)據(jù)卷并不會(huì)被自動(dòng)刪除。如果要?jiǎng)h除一個(gè)數(shù)據(jù)卷,必須在刪除最后一個(gè)還掛載著它的容器時(shí)顯式使用docker rm-v命令來(lái)指定同時(shí)刪除關(guān)聯(lián)的容器。

掛載了數(shù)據(jù)卷的容器也可以作為數(shù)據(jù)卷容器

[root@zbx-agent01 ~]# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS                        PORTS                                     NAMES
46188e2f028a   centos:7           "/bin/bash"              15 minutes ago   Exited (0) 14 minutes ago                                               c9
5fce3f28819e   centos:7           "/bin/bash"              21 minutes ago   Up 21 minutes                                                           c8
12d35de6f836   centos:7           "/bin/bash"              28 minutes ago   Up 28 minutes                                                           c7
08d9d0a77dab   httpd              "httpd-foreground"       30 minutes ago   Exited (0) 30 minutes ago                                               dreamy_chebyshev
0a14ae5189c1   centos:7           "/bin/bash"              17 hours ago     Exited (255) 30 minutes ago                                             test1
edb1704cf372   stevelugyq/nginx   "/docker-entrypoint.?-"   17 hours ago     Exited (255) 30 minutes ago   0.0.0.0:42399->80/tcp, :::42399->80/tcp   web2
9c4612185d64   stevelugyq/nginx   "/docker-entrypoint.?-"   17 hours ago     Exited (255) 30 minutes ago   0.0.0.0:49153->80/tcp, :::49153->80/tcp   web1
2f80db9d227c   httpd              "httpd-foreground"       18 hours ago     Exited (0) 17 hours ago                                                 wonderful_mccarthy
[root@zbx-agent01 ~]# docker run -itd --name c10 --volumes-from c7 centos:7 bash
7dc4c280b0fc8298390692cdaf25701c628bde7e2869ad195d6b366eb9d62a78
[root@zbx-agent01 ~]# docker exec -it c10 bash
[root@7dc4c280b0fc /]# ls
anaconda-post.log  bin  data1  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@7dc4c280b0fc /]# ls /data1/
abc.txt


在這里插入圖片描述
在這里插入圖片描述

容器互聯(lián) (使用centos鏡像)

容器互聯(lián)是通過(guò)容器的名稱在容器間建立一條專門的網(wǎng)絡(luò)通信隧道。簡(jiǎn)單點(diǎn)說(shuō),就是會(huì)在源容器和接收容器之間建立一條隧道,接收容器可以看到源容器指定的信息

[root@zbx-agent01 ~]# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS                        PORTS                                     NAMES
7dc4c280b0fc   centos:7           "bash"                   4 minutes ago    Up 4 minutes                                                            c10
46188e2f028a   centos:7           "/bin/bash"              21 minutes ago   Exited (0) 20 minutes ago                                               c9
5fce3f28819e   centos:7           "/bin/bash"              26 minutes ago   Up 26 minutes                                                           c8
12d35de6f836   centos:7           "/bin/bash"              33 minutes ago   Up 33 minutes                                                           c7
08d9d0a77dab   httpd              "httpd-foreground"       35 minutes ago   Exited (0) 35 minutes ago                                               dreamy_chebyshev
0a14ae5189c1   centos:7           "/bin/bash"              17 hours ago     Exited (255) 36 minutes ago                                             test1
edb1704cf372   stevelugyq/nginx   "/docker-entrypoint.…"   17 hours ago     Exited (255) 36 minutes ago   0.0.0.0:42399->80/tcp, :::42399->80/tcp   web2
9c4612185d64   stevelugyq/nginx   "/docker-entrypoint.…"   18 hours ago     Exited (255) 36 minutes ago   0.0.0.0:49153->80/tcp, :::49153->80/tcp   web1
2f80db9d227c   httpd              "httpd-foreground"       18 hours ago     Exited (0) 18 hours ago                                                 wonderful_mccarthy
[root@zbx-agent01 ~]# docker exec -it c10 bash
[root@7dc4c280b0fc /]# ping c8
ping: c8: Name or service not known
[root@7dc4c280b0fc /]# exit 
exit
[root@zbx-agent01 ~]# docker run -itd -P --name c11 --link c10:c10 centos:7 /bin/bash
22500512a0baca4bf67fd79bca6a620df7ce427a84228315bd912f67f9f1eb94
[root@zbx-agent01 ~]# docker exec -it c11 bash
[root@22500512a0ba /]# pi
pinentry         pinentry-curses  ping             ping6            pinky            pivot_root       
[root@22500512a0ba /]# ping c10
PING c10 (172.17.0.4) 56(84) bytes of data.
64 bytes from c10 (172.17.0.4): icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from c10 (172.17.0.4): icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from c10 (172.17.0.4): icmp_seq=3 ttl=64 time=0.061 ms
^C
--- c10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.061/0.070/0.090/0.016 ms
[root@22500512a0ba /]# docker exec -it c10 bash
bash: docker: command not found
[root@22500512a0ba /]# exit 
exit
[root@zbx-agent01 ~]# docker exec -it c10 bash
[root@7dc4c280b0fc /]# yum install -y net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
base                                                                                                                    | 3.6 kB  00:00:00     
extras                                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                                 | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                           | 153 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                       | 247 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                      |  16 MB  00:00:01     
(4/4): base/7/x86_64/primary_db                                                                                         | 6.1 MB  00:00:15     
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================
 Package                         Arch                         Version                                         Repository                  Size
===============================================================================================================================================
Installing:
 net-tools                       x86_64                       2.0-0.25.20131004git.el7                        base                       306 k

Transaction Summary
===============================================================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.25.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                           | 306 kB  00:00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                   1/1 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                   1/1 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                                                                                  

Complete!
[root@7dc4c280b0fc /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 5459  bytes 24592386 (23.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5331  bytes 291415 (284.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在這里插入圖片描述

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_66946305/article/details/125721113

欄目分類
最近更新