Docker数据卷管理

数据卷和数据卷容器

在Docker的使用过程中往往需要对数据进行持久化,或者需要在多个容器之间进行数据共享,所以这就涉及到Docker容器的数据操作。
容器中数据管理主要有两种方式:数据卷和数据卷容器。

数据卷(Data Volumes) 容器内数据直接映射到本地宿主机。
数据卷容器(Data Volume Containers) 使用特定容器维护数据卷。

数据卷

数据卷是一个特殊的目录,它将主机目录直接映射进容器。可供一个或多个容器使用。
数据卷设计的目的就是为了 数据的持久化,它完全独立与容器的生命周期。因此,容器删除时,不会删除其挂载的数据卷,也不会存在类似的垃圾机制对容器存在的数据卷进行处理。

数据卷的特性

数据卷在容器启动时初始化,如果容器使用的镜像在挂载点包含了数据,这些数据会拷贝到新初始化的数据卷中
数据卷可以在容器之间共享和重用
可以对数据卷里的内容直接修改,修改回马上生效,无论是容器内操作还是本地操作
对数据卷的更新不会影响镜像的更新
数据卷会一直存在,即使挂载数据卷的容器已经被删除

docker run 在创建容器时使用 -v 参数可以挂载一个或多个数据卷到当前运行的容器中
-v的作用是将宿主机上的目录作为容器的数据卷挂载到容器中,使宿主机和容器之间可以共 享一个目录。

[root@foundation8 web]# docker stop vm1
vm1
[root@foundation8 web]# docker rm -f vm1
vm1
[root@foundation8 web]# docker run -it --name vm1 -v /tmp/data1:/data1 -v /tmp/data2:/data2:ro -v /etc/yum.repos.d/dvd.repo:/etc/yum.repos.d/dvd.repo:ro nginx bash
## 创建一个容器
## -v 参数可以重复使用,挂载多个数据卷到容器中,冒号前面的是宿主机的目录(本地目录不存在 docker 会自动创建),冒号后面的是容器中的挂载目录。
## /tmp/data1:/data1  将宿主机的目录/tmp/data1影射到容器中的/data1目录
## /data1 默认可读写 /data2 只读
## 将宿主机/etc/yum.repos.d/dvd.repo文件映射到容器/etc/yum.repos.d/dvd.repo文件,可以使用yum源,但不能进行修改
root@845749407d5c:/# ls
bin   data1  dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  data2  etc  lib   media  opt  root  sbin  sys  usr
root@845749407d5c:/# cd /etc/yum.repos.d/
root@845749407d5c:/etc/yum.repos.d# ls
dvd.repo
root@845749407d5c:/etc/yum.repos.d# cd /data2/
root@845749407d5c:/data2# touch file                # /data2只读
touch: cannot touch 'file': Read-only file system
root@845749407d5c:/data2# cd /data1/                # /data1可读写
root@845749407d5c:/data1# touch file
root@845749407d5c:/data1# ls
file

在宿主机的/tmp目录对目录data1,data2进行操作,在容器中进行测试

[root@foundation8 ~]# cd /tmp/
[root@foundation8 tmp]# cd data1/
[root@foundation8 data1]# touch file1
root@845749407d5c:/data1# ls
file  file1
root@845749407d5c:/data1# rm -f file1
[root@foundation8 data2]# touch file2
root@845749407d5c:/data2# ls
file2
root@845749407d5c:/data2# rm -fr file2
rm: cannot remove 'file2': Read-only file system
[root@foundation8 web]# docker rm -f vm1
vm1

数据卷容器

用户需要在多个容器之间共享一些数据,就可以使用数据卷容器。
命名的容器挂载数据卷,其他容器通过挂载这个容器实现数据共享,挂载数据卷的容器,就叫数据卷容器。

创建数据卷容器

[root@foundation8 web]# docker create --name datavol -v /tmp/data1:/data1 -v /tmp/data2:/data2:ro -v /etc/yum.repos.d/dvd.repo:/etc/yum.repos.d/dvd.repo:ro nginx bash
2862fe11c05aa528746a5190887043111c34c2420ccab76c3c2deb5d7bf2ac34
[root@foundation8 web]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2862fe11c05a        nginx               "bash"              47 seconds ago      Created                                 datavol
[root@foundation8 web]# docker run -it --name vm1 --volumes-from datavol nginx bash
root@52465d9c7fb5:/# ls
bin   data1  dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  data2  etc  lib   media  opt  root  sbin  sys  usr
root@52465d9c7fb5:/# cd /data1/
root@52465d9c7fb5:/data1# ls
file
root@52465d9c7fb5:/data1# cd /data2/
root@52465d9c7fb5:/data2# ls
file2
root@52465d9c7fb5:/data2# cd /etc/yum.repos.d/
root@52465d9c7fb5:/etc/yum.repos.d# ls
dvd.repo
root@52465d9c7fb5:/etc/yum.repos.d# exit
[root@foundation8 web]# docker run -it --name vm2 -v /tmp/backup:/backup nginx bash
root@222e9142b5a5:/# ls
backup  boot  etc   lib    media  opt   root  sbin  sys  usr
bin dev   home  lib64  mnt    proc  run   srv   tmp  var

备份数据卷

[root@foundation8 web]# docker run --rm -v /tmp/backup:/backup ubuntu tar cf /backup/etc.tar /etc
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
c64513b74145: Pull complete 
01b8b12bad90: Pull complete 
c5d85cf7a05f: Pull complete 
b6b268720157: Pull complete 
e12192999ff1: Pull complete 
Digest: sha256:3f119dc0737f57f704ebecac8a6d8477b0f6ca1ca0332c7ee1395ed2c6a82be7
Status: Downloaded newer image for ubuntu:latest
tar: Removing leading `/' from member names
[root@foundation8 web]# cd /tmp/backup/
[root@foundation8 backup]# ls
etc.tar
[root@foundation8 backup]# docker run -d --name vm1 game2048
[root@foundation8 backup]# docker inspect vm1 | grep Pid
            "Pid": 12886,
            "PidMode": "",
            "PidsLimit": 0,

[root@foundation8 backup]# ps ax

12886 ?        Ss     0:00 /bin/sh -c sed -i "s/ContainerID: /Containe

[root@foundation8 backup]# cd /proc/
[root@foundation8 proc]# cd 12886/
[root@foundation8 12886]# ls
attr             environ    mem            pagemap      stat
autogroup        exe        mountinfo      personality  statm
auxv             fd         mounts         projid_map   status
cgroup           fdinfo     mountstats     root         syscall
clear_refs       gid_map    net            sched        task
cmdline          io         ns             schedstat    timers
comm             limits     numa_maps      sessionid    uid_map
coredump_filter  loginuid   oom_adj        setgroups    wchan
cpuset           map_files  oom_score      smaps
cwd              maps       oom_score_adj  stack
[root@foundation8 12886]# cd ns
[root@foundation8 ns]# ls
ipc  mnt  net  pid  user  uts
[root@foundation8 ns]# ll
total 0
lrwxrwxrwx 1 root root 0 Aug 19 15:11 ipc -> ipc:[4026532399]
lrwxrwxrwx 1 root root 0 Aug 19 15:11 mnt -> mnt:[4026532397]
lrwxrwxrwx 1 root root 0 Aug 19 15:08 net -> net:[4026532402]
lrwxrwxrwx 1 root root 0 Aug 19 15:11 pid -> pid:[4026532400]
lrwxrwxrwx 1 root root 0 Aug 19 15:11 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 Aug 19 15:11 uts -> uts:[4026532398]
[root@foundation8 ns]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.0021cc7099f2   no      enp0s25
                            vnet0
                            vnet1
                            vnet2
                            vnet3
                            vnet4
                            vnet5
docker0     8000.0242ab6064ed   no      vethf8bd308
virbr0      8000.52540030e8c2   yes     virbr0-nic
virbr1      8000.52540091fde0   yes     virbr1-nic
[root@foundation8 ns]# docker stop vm1
vm1
[root@foundation8 ns]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.0021cc7099f2   no      enp0s25
                            vnet0
                            vnet1
                            vnet2
                            vnet3
                            vnet4
                            vnet5
docker0     8000.0242ab6064ed   no      
virbr0      8000.52540030e8c2   yes     virbr0-nic
virbr1      8000.52540091fde0   yes     virbr1-nic

[root@foundation8 ns]# docker run -it --name vm2 ubuntu
root@5909422b42aa:/# exit
[root@foundation8 ns]# docker start vm2
vm2
[root@foundation8 ns]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
5909422b42aa        ubuntu              "/bin/bash"              About a minute ago   Up 2 seconds                            vm2
f43050b049c9        game2048            "/bin/sh -c 'sed -..."   16 minutes ago       Up 5 minutes        80/tcp, 443/tcp     vm1
[root@foundation8 ns]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.0021cc7099f2   no      enp0s25
                            vnet0
                            vnet1
                            vnet2
                            vnet3
                            vnet4
                            vnet5
docker0     8000.0242ab6064ed   no      veth4fdd57c
                            vethd2e8ab0
virbr0      8000.52540030e8c2   yes     virbr0-nic
virbr1      8000.52540091fde0   yes     virbr1-nic

[root@foundation8 ns]# ip addr
9: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ab:60:64:ed brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:abff:fe60:64ed/64 scope link 
       valid_lft forever preferred_lft forever
24: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:41:ec:58 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe41:ec58/64 scope link 
       valid_lft forever preferred_lft forever
25: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:1f:a6:72 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe1f:a672/64 scope link 
       valid_lft forever preferred_lft forever
26: vnet2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:77:fe:3c brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe77:fe3c/64 scope link 
       valid_lft forever preferred_lft forever
27: vnet3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:fa:82:61 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fefa:8261/64 scope link 
       valid_lft forever preferred_lft forever
28: vnet4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:25:d2:4b brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe25:d24b/64 scope link 
       valid_lft forever preferred_lft forever
29: vnet5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN qlen 1000
    link/ether fe:54:00:43:72:b3 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe43:72b3/64 scope link 
       valid_lft forever preferred_lft forever
37: veth4fdd57c@if36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether 9e:ce:e3:e2:b7:90 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::9cce:e3ff:fee2:b790/64 scope link 
       valid_lft forever preferred_lft forever
43: vethd2e8ab0@if42: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether a2:7c:0d:29:f5:5b brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::a07c:dff:fe29:f55b/64 scope link 
       valid_lft forever preferred_lft forever
[root@foundation8 Desktop]# docker load -i ubuntu.tar 
454970bd163b: Loading layer 196.8 MB/196.8 MB
38112156678d: Loading layer 208.9 kB/208.9 kB
4e1f7c524148: Loading layer 4.608 kB/4.608 kB
56063ad57855: Loading layer 1.024 kB/1.024 kB
The image ubuntu:latest already exists, renaming the old one with ID sha256:735f80812f90aca43213934fd321a75ef20b2e30948dbbdd2c240e8abaab8a28 to empty string
[root@foundation8 Desktop]# docker run -it --name vm3 ubuntu bash
root@5e27913a4089:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
48: eth0@if49: <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
    inet 172.17.0.4/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:4/64 scope link 
       valid_lft forever preferred_lft forever
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值