6. Docker - 数据管理

一、数据管理

  1. 挂载本地目录到容器里
bash-3.2# mkdir -p /data/docker/docker_date/docker_01
bash-3.2# docker run -itd -v /data/docker/docker_date/docker_01:/docker_01 centos bash
41dcfa81c7edc0484182558006ac2728cfeb253e99f6d7303f055b0b11f864e2
bash-3.2# echo  "This is docker_01 dir" > /docker_01/1.txt
bash-3.2# docker exec -it 41dc bash
41dcfa81c7ed# ls /docker_01/
1.txt
41dcfa81c7ed# echo "This is Docker" > /docker_01/2.txt
bash-3.2# ls /data/docker/docker_date/docker_01
1.txt  2.txt
### -v: 指定挂载目录
    : : 前面的为本地目录
    : : 后面到为容器里的目录
    即使将删除这个容器,文件也不会丢失.
  1. 挂载数据卷(多个容器挂载宿主机的同一个目录)
bash-3.2# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                    NAMES
41dcfa81c7ed        centos                 "bash"              6 minutes ago       Up 6 minutes                                 trusting_stallman
bash-3.2# docker run -itd --volumes-from trusting_stallman centos bash      #将"trusting_stallman容器"的数据卷(docker_01)挂载到新建的容器中
37f4feba96d34808fd4e5c6972c6062a1f6ccd1c0a4f35dae3dfdd10871fe8ec
bash-3.2# docker exec -it 37f4feba96d3 bash
37f4feba96d3# df -h|grep docker*
Filesystem           Size    Used    Avail   Use%   Mounted on
/dev/sda3            402G     14G     368G    4%    /docker_01
37f4feba96d3# echo "This is Doceker_3" > /docker_01/3.txt
37f4feba96d3# ls /docker_01/
1.txt  2.txt  3.txt
bash-3.2# docker exec -it 41dcfa81c7ed bash
41dcfa81c7ed# ls /docker_01/
1.txt  2.txt  3.txt
### 总结: 将"37f4feba96d3容器"内的/docker_01目录共享到"41dcfa81c7ed容器"内.
  1. 自定义数据卷容器
bash-3.2# docker run -itd -v /data --name 90root_V centos bash
1e4cf0c7b5dc9ba7f862d588a6f4fe7db6939a0a70a2784501cf85b6428040b2
### 这里的/data是容器的/data目录,而不是宿主机的/data目录
bash-3.2# docker exec -it 90root_V bash     #进入容器
1e4cf0c7b5dc# df -h|grep data
Filesystem           Size    Used    Avail   Use%   Mounted on
/dev/sda1             50G     28G     20G     59%     /data
1e4cf0c7b5dc# touch /data/1 /data/2 /data/3
bash-3.2# docker run -itd --volumes-from 90root_V --name=90root_V2 centos bash
a689fcdb06ed08a369f73503a274d029839cddb80f32767103792ef45631d0af
bash-3.2# docker exec -it 90root_V2 bash
a689fcdb06ed# df -h
Filesystem           Size    Used    Avail   Use%   Mounted on
/dev/sda1             50G     28G     20G     59%    /data
a689fcdb06ed# ls /data
1  2  3 
a689fcdb06ed# touch /data/4
1e4cf0c7b5dc# ls /data
1  2  3  4
  1. 数据卷的备份
bash-3.2# mkdir /vol_data_backup        #宿主机创建备份目录
bash-3.2# docker run -itd --volumes-from 90root_V -v /vol_data_backup/:/backup centos bash
4e5fda17c51afb0ba27aa08213c825a756584a9077c936d9bedb0d8185a94f48
bash-3.2# docker exec -it 4e5fda17c51a bash
4e5fda17c51a# df -h
Filesystem           Size    Used    Avail   Use%   Mounted on
/dev/sda1             50G    28G      20G    59%     /data
4e5fda17c51a# ls /backup                #容器是有/backup目录的
4e5fda17c51a# tar cvf /backup/data.tar /data/
bash-3.2# ls /vol_data_backup/
data.tar
### volumes-from: 将"90root_V容器"里的数据卷(/data)挂载到新建容器内.
###  -v: 宿主机的/vol_data_bakcup目录挂载到容器/backup目录
  1. 数据卷的恢复
### 恢复思路: 新建一个数据卷容器, 再新建一个容器并挂载该数据卷容器,然后再解包(tar)
bash-3.2# docker run -itd -v /90root --name 90root_centos_v1 centos bash    #/90root为容器里的目录
8e2547638bb3161f2225b05f29541388c072d4a2f991e9bce62024cf6f3dabcb
bash-3.2# docker run -itd --volumes-from 90root_centos_v1 -v /vol_data_backup/:/backup --name 90root_centos_v2 centos bash
dced5597366d506d5f522606bbcace8afca32f8772f9e54b878c9e7f03b96f0c
bash-3.2# docker exec -it 90root_centos_v2 bash
dced5597366d# ls /backup/
data.tar
dced5597366d# tar xvf /backup/data.tar -C /90root/
dced5597366d# ls /90root/data/
1  2  3  4  5
bash-3.2# docker exec -it 90root_centos_v1 bash
8e2547638bb3# ls /90root/data/
1  2  3  4  5
posted @ 2016-10-16 12:24  幻月0412  阅读(139)  评论(0编辑  收藏  举报