Docker数据管理
Docker提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts 和tmpfs。
olumes
docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)。
管理卷
[root@docker-master ~]# docker volume create nginx-vo1 nginx-vo1 [root@docker-master ~]# docker volume ls DRIVER VOLUME NAME local 144c5839b36278cea8b3bc7bf3d10d051c11e7acc5610d420f25314f240ef574 local a0c117c96d84eeaeebd548eda827577f17926a595c85ea69a9f4ea9f756c1557 local f0e495702096c6845110ae74e0ed24455059e1265831acca1bb303458b4400ab local nginx-vo1 [root@docker-master ~]# docker volume inspect nginx-vo1 [ { "CreatedAt": "2018-12-12T01:05:59+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/nginx-vo1/_data", "Name": "nginx-vo1", "Options": {}, "Scope": "local" } ]
用卷创建一个容器
docker run -itd --name=nginx-test --mount src=nginx-vo1,dst=/usr/share/nginx/html nginx
进入容器内部查看nginx web目录
[root@docker-master ~]# docker exec -it nginx-test bash root@23821377e2f7:/# ls /usr/share/nginx/html/ 50x.html index.html
在宿主机docker volumes目录可以发现有相同的两个html文件,无论是在容器内新创建文件还是在宿主机创建文件,内容都不会同步映射到对应目录
[root@docker-master ~]# ll /var/lib/docker/volumes/nginx-vo1/_data/ total 8 -rw-r--r-- 1 root root 494 Nov 27 20:31 50x.html -rw-r--r-- 1 root root 612 Nov 27 20:31 index.html
清理
docker container stop nginx-test docker container rm nginx-test docker volume rm nginx-vo1
注意:
1.如果没有指定卷,自动创建
2.建议使用–mount,更通用
bind mounts
可以存储在宿主机系统的任意位置。将宿主机的目录映射到容器中做持久卷,删除容器后数据存储在宿主机上不会丢失
用卷创建一个容器
[root@docker-master ~]# mkdir /app/wwwroot -p [root@docker-master ~]# docker run -itd --name=nginx-test --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx 453be5dc46e410c3daa5c34f219ed5d195c134a538b2e5f85d0c1d333fd7e7ff [root@docker-master ~]# docker exec -it nginx-test bash root@453be5dc46e4:/# mount /dev/mapper/docker-8:2-528671-5a0afb1cab58d9e41d9ec2ecfc57b7045452918f77a15ede38aa9acb69d7bdac on / type xfs (rw,relatime,nouuid,attr2,inode64,logbsize=64k,sunit=128,swidth=128,noquota) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666) sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime) tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,relatime,mode=755) cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd) cgroup on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb) cgroup on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset) cgroup on /sys/fs/cgroup/net_cls type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls) cgroup on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio) cgroup on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices) cgroup on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer) cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct,cpu) cgroup on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event) cgroup on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory) mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime) /dev/sda2 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered) /dev/sda2 on /etc/hostname type ext4 (rw,relatime,data=ordered) /dev/sda2 on /etc/hosts type ext4 (rw,relatime,data=ordered) shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k) /dev/sda2 on /usr/share/nginx/html type ext4 (rw,relatime,data=ordered)/dev/sda2 on /usr/share/nginx/html type ext4 (rw,relatime,data=ordered)
进入到容器内使用mount命令可以发现/dev/sda2 on /usr/share/nginx/html type ext4 (rw,relatime,data=ordered)已经挂载了
docker run -itd --name=nginx-test -v /app/wwwroot:/usr/share/nginx/html nginx
验证绑定
docker inspect nginx-test
清理
docker container stop nginx-test docker container rm nginx-test
注意:
1.如果源文件/目录没有存在,不会自动创建,会抛出一个错误。
docker: Error response from daemon: invalid mount config for type “bind”: bind source path does not exist.
See ‘docker run –help’.
2.如果挂载目标在容器中非空目录,则该目录现有内容将被隐藏。
tmpfs
挂载存储在宿主机系统的内存中,而不会写入宿主机的文件系统。