挂载作用
目录的挂载是为了解决容器内部的文件和外部的文件的互通,通过目录的挂载,可以同步相关的代码一级相关的配置修改,还有如日志双向的数据同步等。
实现
单目录的挂载
[root@localhost ~]# docker run -it -v /宿主机目录:/容器主机的目录 镜像名(或镜像ID)
多目录的挂载
[root@localhost ~]# docker run -it -v /宿主机目录:/容器主机的目录 -v /宿主机目录:/容器主机的目录 镜像名(或镜像ID)
示例步骤
1):查询本地所有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 891fcd9c5b3a 2 weeks ago 647MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost ~]#
2:使用ubuntu镜像为例,进入ubuntu容器查看具体的信息,新建测试目录
[root@localhost ~]# docker run -it 9b9cb95443b5 /bin/bash
root@6199fa3fd9d1:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@6199fa3fd9d1:/# ^C
root@6199fa3fd9d1:/# mkdir ceshitongbu001
root@6199fa3fd9d1:/# ls
bin boot ceshitongbu001 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@6199fa3fd9d1:/#
3:挂载宿主目录到容器内部的ceshitongbu001目录下
[root@localhost ~]# cd ..
[root@localhost /]# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]# mkdir suzhuceshi0001
root@localhost /]# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv suzhuceshi0001 sys tmp usr var
[root@localhost /]#
执行挂载
[root@localhost /]# docker run -it -v suzhuceshi0001/:/ceshitongbu001 9b9cb95443b5
docker: Error response from daemon: create suzhuceshi0001/: "suzhuceshi0001/" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
[root@localhost /]#
错误2:
[root@localhost /]# docker run -it -v suzhuceshi0001/:ceshitongbu001/ 9b9cb95443b5
docker: Error response from daemon: invalid volume specification: 'suzhuceshi0001/:ceshitongbu001/': invalid mount config for type "volume": invalid mount path: 'ceshitongbu001/' mount path must be absolute.
See 'docker run --help'.
处理
显示文件路径必须完整(即:以“/”开头)
[root@localhost /]# docker run -it -v /suzhuceshi0001/:/ceshitongbu001/ 9b9cb95443b5
root@509bd82ac9b4:/#
4:外部编辑修改文件,然后查看容器内部的文件信息
image.png
5:容器同步到外部
image.png
其他补充挂载容器目录可读写性
如:容器内的ceshitongbu001下的内容都不运行修改
[root@localhost ~]# docker run -it -v /宿主机目录:/容器主机的目录:ro 镜像名(或镜像ID)
实践:
[root@localhost /]# docker run -it -v /suzhuceshi0001/:/ceshitongbu001/:ro 9b9cb95443b5
root@c6aa103b9b79:/# cd ceshitongbu001/
root@c6aa103b9b79:/ceshitongbu001# touch sdsds.tex
touch: cannot touch 'sdsds.tex': Read-only file system
root@c6aa103b9b79:/ceshitongbu001# touch sdsds.tet
touch: cannot touch 'sdsds.tet': Read-only file system
root@c6aa103b9b79:/ceshitongbu001#
文件拷贝——容器和宿主主机的之间相互拷贝
来源:https://www.runoob.com/docker/docker-cp-command.html
将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。
docker cp /www/runoob 96f7f14e99ab:/www/
将主机/www/runoob目录拷贝到容器96f7f14e99ab中,目录重命名为www。
docker cp /www/runoob 96f7f14e99ab:/www
将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。
docker cp 96f7f14e99ab:/www /tmp/
网友评论