Docker 数据管理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


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

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值