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: 指定掛載目錄
: : 前面的爲本地目錄
: : 後面到爲容器裏的目錄
即便將刪除這個容器,文件也不會丟失.
- 掛載數據卷(多個容器掛載宿主機的同一個目錄)
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容器"內.
- 自定義數據卷容器
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
- 數據卷的備份
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目錄
- 數據卷的恢復
### 恢復思路: 新建一個數據卷容器, 再新建一個容器並掛載該數據卷容器,而後再解包(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