網站首頁 編程語言 正文
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- Docker的數據庫管理
- 數據卷
- 數據卷容器
- 容器互聯 (使用centos鏡像)
Docker的數據庫管理
數據卷
數據卷是一個供容器使用的特殊目錄,位于容器中。可將宿主機的目錄掛載到數據卷上,對數據卷的修改操作立刻可見,并且更新數據不會影響鏡像,從而實現數據在宿主機與容器之間的遷移。數據卷的使用類似于Linux下對目錄進行的mount操作。
docker cp 也是一種遷移
[root@zbx-agent01 ~]# docker pull centos:7
[root@localhost ~]# ls /var/www
ls: 無法訪問/var/www: 沒有那個文件或目錄
#宿主機目錄/var/www掛載到容器中的/datal。
#注意:宿主機本地目錄的路徑必須是使用絕對路徑。如果路徑不存在,Docker會自動創建相應的路徑。
#-v選項可以在容器內創建數據卷
[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
#進容器
[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
#寫入數據
[root@12d35de6f836 /]# echo "i am stevelu">/data1/abc.txt
[root@12d35de6f836 /]# exit
exit
[root@zbx-agent01 ~]# cat /var/www/abc.txt
i am stevelu
數據卷容器
如果需要在容器之間共享一些數據,最簡單的方法就是使用數據卷容器。數據卷容器是一個普通的容器,專門提供數據卷給其他容器掛載使用。
[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 來掛載c8容器中的數據卷到c9容器
[root@5fce3f28819e /]# cat /data1/abc.txt
i am stevelu
[root@5fce3f28819e /]# cat /data2/ABC.txt
注意:使用–volumes-from參數所掛載數據卷的容器自身并不需要保持在運行狀態。
如果刪除了掛載的容器,數據卷并不會被自動刪除。如果要刪除一個數據卷,必須在刪除最后一個還掛載著它的容器時顯式使用docker rm-v命令來指定同時刪除關聯的容器。
掛載了數據卷的容器也可以作為數據卷容器
[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
容器互聯 (使用centos鏡像)
容器互聯是通過容器的名稱在容器間建立一條專門的網絡通信隧道。簡單點說,就是會在源容器和接收容器之間建立一條隧道,接收容器可以看到源容器指定的信息
[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
- 上一篇:Docker資源限制Cgroup
- 下一篇:nginx-1.20*安裝check模塊
相關推薦
- 2024-03-09 【Redis】Redis 實現分布式Session
- 2022-04-14 C#可變參數params示例詳解_C#教程
- 2022-10-14 Ubuntu在GitHub中配置SSH Key
- 2022-11-12 C語言內存操作函數使用示例梳理講解_C 語言
- 2022-07-27 Python?pyecharts?Line折線圖的具體實現_python
- 2023-03-11 Rust如何進行模塊化開發技巧分享_Rust語言
- 2022-05-31 C?語言的弱符號與弱引用你了解嗎_C 語言
- 2023-01-14 GoLang并發機制探究goroutine原理詳細講解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支