实验环境:192.168.200.100 桥接
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1 231d40e811cd 2 weeks ago 126MB
nginx latest 231d40e811cd 2 weeks ago 126MB
[root@localhost ~]# docker run -it --name test01 -v /html:/tmp nginx:1 /bin/bash #运行并修改名称为test01方便记忆,-v是映射目录。将真机的/html与docker容器中的
/tmp打通
root@ef8f202fface:/# ls /tmp/ #一开始是没有任何文件
再开启一个192.168.200.100的窗口并执行这条操作
[root@localhost ~]# touch /html/1.txt
[root@localhost ~]# echo "111" > /html/1.txt
再查询/tmp下有没有1.txt
root@ef8f202fface:/# ls /tmp/
1.txt
root@ef8f202fface:/# cat /tmp/1.txt
111
重新运行一个容器并设置权限为只读权限
[root@localhost ~]# docker run -it --name test02 -v /html:/tmp:ro nginx:1 /bin/bash
root@2c39a7ca5cc8:/# ls /tmp/
1.txt
root@2c39a7ca5cc8:/# echo "2222" > /tmp/1.txt
bash: /tmp/1.txt: Read-only file system #此时报错,因为不具备写权限
再开一个192.168.200.100的窗口进行真机操作:
[root@localhost ~]# echo "222" >> /html/1.txt
再查询/tmp下有没有2222的数据
root@2c39a7ca5cc8:/# cat /tmp/1.txt
111
222
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c39a7ca5cc8 nginx:1 "/bin/bash" 9 minutes ago Up 9 minutes 80/tcp test02
ef8f202fface nginx:1 "/bin/bash" 22 minutes ago Up 22 minutes 80/tcp test01
b676d7311157 nginx:1 "/bin/bash" 43 minutes ago Exited (0) 30 minutes ago heuristic_ritchie
c4b32964d8d0 nginx:1 "/bin/bash" 44 minutes ago Exited (0) 21 minutes ago naughty_robinson
[root@localhost ~]# docker ps -aq
2c39a7ca5cc8
ef8f202fface
b676d7311157
c4b32964d8d0
[root@localhost ~]# docker ps -aq | while read line #利用循环的方式删除所有容器
> do
> docker rm -f $line
> done
2c39a7ca5cc8
ef8f202fface
b676d7311157
c4b32964d8d0
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1 231d40e811cd 2 weeks ago 126MB
nginx latest 231d40e811cd 2 weeks ago 126MB
[root@localhost ~]# docker run -it -v /dbdata:/dbdata --name=dbdata nginx:1 /bin/bash
root@17f9974820f5:/# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17f9974820f5 nginx:1 "/bin/bash" About a minute ago Exited (0) 14 seconds ago dbdata
[root@localhost ~]# docker run -it --name test01 --volumes-from dbdata nginx:1 /bin/bash
root@4a1a6407ff83:/# ls
bin dbdata etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
再开启一个192.168.200.100的窗口并执行这条操作
[root@localhost ~]# touch /dbdata/1.html
再查询dbdata下有没有1.html的数据
root@4a1a6407ff83:/# ls dbdata/
1.html
相当于把数据卷的功能放到了容器内,谁指定,谁就能使用
备份
[root@localhost ~]# docker run --volumes-from dbdata -v $(pwd):/backup --name worker nginx:1 tar cvf /backup/backup.tar /dbdata
运行结果为:
tar: Removing leading `/' from member names
/dbdata/
/dbdata/1.html
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
backup.tar 公 共 视频 文档 音 乐
恢复:
创建带有数据卷的容器:dadata2
[root@localhost ~]# docker run -v /dbdata --name dbdata2 nginx:1 /bin/bash
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50576a447682 nginx:1 "/bin/bash" 8 seconds ago Exited (0) 7 seconds ago dbdata2
43527dd75b0d nginx:1 "tar cvf /backup/bac…" 4 minutes ago Exited (0) 4 minutes ago worker
4a1a6407ff83 nginx:1 "/bin/bash" 18 minutes ago Up 18 minutes 80/tcp test01
17f9974820f5 nginx:1 "/bin/bash" 21 minutes ago Exited (0) 20 minutes ago dbdata
[root@localhost ~]# docker run --volumes-from dbdata2 -v $(pwd):/backup nginx:1 tar xvf /backup/backup.tar
dbdata/
dbdata/1.html
==========================================================================================
================================================================================================