容器中管理數據主要有兩種方式:linux
數據卷是一個可供容器使用的特殊目錄,它繞過文件系統,能夠提供不少有用的特性:docker
數據卷的使用,相似於Linux下對目錄或文件進行mount操做。centos
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry latest 5c929a8b587a 29 hours ago 33.27 MB
genesis_centos latest 85bc3a58f134 5 days ago 277.6 MB
192.168.1.179:5000/busybox latest 9967c5ad88de 12 days ago 1.093 MB
busybox latest 9967c5ad88de 12 days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 13 days ago 341.3 MB
centos_with_net latest 3e8ea8607f08 4 weeks ago 294.9 MB
centos latest 9baab0af79c4 6 weeks ago 196.7 MB
[root@localhost ~]# ls /data/
ls: 沒法訪問/data/: 沒有那個文件或目錄
[root@localhost ~]# mkdir /data/
[root@localhost ~]# docker run -itd -v /data/:/data1 centos bash
096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5
[root@localhost ~]# touch /data/1.txt
[root@localhost ~]# echo "test" > /data/1.txt
[root@localhost ~]# docker exec -it 09646 bash
[root@096460f831bf /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5 9.8G 231M 9.0G 3% / tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data1 [root@096460f831bf /]# ls /data1/
1.txt
[root@096460f831bf /]# cat /data1/1.txt
test
[root@096460f831bf /]# touch /data1/2.txt
[root@096460f831bf /]# exit
exit
[root@localhost ~]# ls /data/
1.txt 2.txt
無論是把容器停掉、仍是刪除,數據仍是存在的ruby
[root@localhost ~]# docker stop 09646
09646
[root@localhost ~]# ls /data/
1.txt 2.txt
[root@localhost ~]# docker rm 09646
09646
[root@localhost ~]# ls /data/
1.txt 2.txt
[root@localhost ~]# docker run -itd -v /data/:/data1 centos bash
e136b27a8e177d878e76c60aafade32df947a60f77b3f95dcaf0680b7ffbc6e8
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e136b27a8e17 centos "bash" 14 seconds ago Up 13 seconds tender_euclid
其實掛載目錄的時候能夠指定容器name,若是不指定就隨機定義了。好比上面咱們沒有指定它,就生成了一個名字爲tender_euclid,這個名字可使用命令 docker ps
看最右側一列。bash
[root@localhost ~]# docker run -itd --volumes-from tender_euclid centos bash
3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164
這樣咱們使用centos鏡像建立了新的容器,而且使用了tender_euclid容器的數據卷。app
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3222c7c5c456 centos "bash" 26 seconds ago Up 25 seconds sick_albattani
e136b27a8e17 centos "bash" 6 minutes ago Up 6 minutes tender_euclid
[root@localhost ~]# docker exec -it 3222 bash
[root@3222c7c5c456 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-3222c7c5c45687e0650b699a9291bc50ecc85030acf8f388c1c6a50b0dc67164 9.8G 231M 9.0G 3% / tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data1 [root@3222c7c5c456 /]# ls /data1/
1.txt 2.txt
[root@3222c7c5c456 /]# touch /data1/3.txt
[root@3222c7c5c456 /]# ls -l /data1/
total 4
-rw-r--r--. 1 root root 5 Oct 20 05:53 1.txt
-rw-r--r--. 1 root root 0 Oct 20 05:59 2.txt
-rw-r--r--. 1 root root 0 Oct 20 06:31 3.txt
[root@3222c7c5c456 /]# exit
exit
[root@localhost ~]# ls /data/
1.txt 2.txt 3.txt
有時候,咱們須要多個容器之間相互共享數據,相似於linux裏面的NFS。因此就能夠搭建一個專門的數據卷容器,而後其餘容器直接掛載該數據卷。post
首先創建數據卷容器spa
[root@localhost ~]# docker run -itd -v /data/ --name cent_testv centos bash
fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb45150dbc21 centos "bash" 8 minutes ago Up 8 minutes cent_testv
3222c7c5c456 centos "bash" 52 minutes ago Up 52 minutes sick_albattani
e136b27a8e17 centos "bash" 58 minutes ago Up 58 minutes tender_euclid
[root@localhost ~]# docker exec -it cent_testv bash
[root@fb45150dbc21 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-fb45150dbc218e71ff07eca44be3603e004e01b94effcca14c2bd8b3a998f096 9.8G 231M 9.0G 3% /
tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data
[root@fb45150dbc21 /]# ls /data/
[root@fb45150dbc21 /]# exit
exit
[root@localhost ~]# ls /data/
1.txt 2.txt 3.txt
其餘容器掛載該數據卷3d
[root@localhost ~]# docker run -itd --volumes-from cent_testv centos bash
0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a80861145c9 centos "bash" 3 seconds ago Up 2 seconds mad_carson
fb45150dbc21 centos "bash" 14 minutes ago Up 14 minutes cent_testv
3222c7c5c456 centos "bash" 58 minutes ago Up 58 minutes sick_albattani
e136b27a8e17 centos "bash" About an hour ago Up About an hour tender_euclid
[root@localhost ~]# docker exec -it 0a8086 bash
[root@0a80861145c9 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-0a80861145c9a2627618a78db2b7225eba64137d4664d3706e02c1c623cde5e3 9.8G 231M 9.0G 3% /
tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data
[root@0a80861145c9 /]# touch /data/fight.txt
[root@0a80861145c9 /]# exit
exit
[root@localhost ~]# docker exec -it cent_testv bash
[root@fb45150dbc21 /]# ls /data/
fight.txt
[root@fb45150dbc21 /]# exit
exit
[root@localhost ~]# docker run -itd --volumes-from cent_testv -v /vol_data_backup/:/backup centos bash
4f5bf6f33f2c78197e54e5145824e98bf89d802376e83019c2913b336fbd9d20
首先咱們須要使用cent_testv數據卷新開一個容器,同時咱們還須要把本地的/vol_data_backup/目錄掛載到該容器的/backup下,這樣在容器中/backup目錄裏面新建的文件咱們就能夠直接在/vol_data_backup/目錄中看到了。 而後再把/data/目錄下面的文件打包到成data.tar文件放到/backup目錄下面。code
[root@localhost ~]# docker exec -it 4f5bf bash
[root@4f5bf6f33f2c /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-4f5bf6f33f2c78197e54e5145824e98bf89d802376e83019c2913b336fbd9d20 9.8G 231M 9.0G 3% /
tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data
[root@4f5bf6f33f2c /]# ls /backup/
[root@4f5bf6f33f2c /]# ls /data/
fight.txt
[root@4f5bf6f33f2c /]# tar cvf /backup/data.tar /data/
tar: Removing leading `/' from member names
/data/
/data/fight.txt
[root@4f5bf6f33f2c /]# exit
exit
[root@localhost ~]# ls /vol_data_backup/
data.tar
先新建一個數據卷容器,再建一個新的容器並掛載該數據卷容器,而後再把tar包解包。
[root@localhost ~]# docker run -itd -v /data --name cent_testv2 centos bash
4cd696928bbe6e0aec9bf8b6856323d7228eb65006b21849eff9f0d41dcea90f
[root@localhost ~]# docker run -itd --volumes-from cent_testv2 -v /vol_data_backup/:/backup centos
7169e8be6d3e5836b626806696046195ed600a1f95b308495e90e6c7b15170d5
[root@localhost ~]# docker exec -it 7169 bash
[root@7169e8be6d3e /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-253:0-1447735-7169e8be6d3e5836b626806696046195ed600a1f95b308495e90e6c7b15170d5 9.8G 231M 9.0G 3% / tmpfs 936M 0 936M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/mapper/VolGroup-lv_root 35G 6.0G 28G 18% /data [root@7169e8be6d3e /]# ls /backup/
data.tar
[root@7169e8be6d3e /]# mv /backup/data.tar .
[root@7169e8be6d3e /]# ls
anaconda-post.log bin data.tar etc lib lost+found mnt proc run srv tmp var
backup data dev home lib64 media opt root sbin sys usr
[root@7169e8be6d3e /]# tar xvf data.tar
data/
data/fight.txt
[root@7169e8be6d3e /]# ls /data/
fight.txt
[root@7169e8be6d3e /]# exit
exit
[root@localhost ~]# ls /vol_data_backup/
[root@localhost ~]#