1.绑定方式:bind的方式共享目录
--注意:
如果源文件/目录没有存在,不会自动创建,会抛出错误。
如果挂载目标在容器中非空目录,则该目录现有内容将被隐藏。
方式使用bind:
1)-v /mnt:/mnt
2)type=,可以是 bind、volume 或 tmpfs。
[root@es1 nginx]# docker run -itd --name myweb_8007 -p 8007:80 --restart always \
> --mount type=bind,src=/mnt,dst=/mnt 192.168.1.11:443/myharbor/httpd:latest
0cb0c69e5d9a13c1bed2582045cb260f039bb186d9ce67e94a9638c43a604b91
--访问:
http://192.168.1.7:8007
It works!
2.登陆容器,查看共享目录
[root@es1 nginx]# docker exec -it 0cb0c69e5d9a bash
root@myweb_8007:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@myweb_8007:/usr/local/apache2# ls /mnt
EFI LiveOS RPM-GPG-KEY-redhat-release extra_files.json media.repo
EULA Packages TRANS.TBL images repodata
GPL RPM-GPG-KEY-redhat-beta addons isolinux
由此可见,操作系统的/mnt镜像目录已经挂载到容器了。
3.本地目录挂载到容器中。
mkdir -p /data/httpd/sspuweb_8006
echo 'sspuweb_8006'> /data/httpd/sspuweb_8006/sspu.html
docker run -itd --name sspuweb_8006 -h sspuweb_8006 -p 8006:80 \
--mount type=bind,dst=/usr/local/apache2/htdocs \
192.168.1.11:443/myharbor/httpd:latest
--访问8006下的文件。
[root@es1 nginx]# curl http://192.168.1.7:8006/sspu.html
sspuweb_8006
[root@es1 nginx]#
4.总结
volume 的方式适合于数据库应用。
bind mounts 的方式适合网站访问。