以前咱們知道,咱們再容器中的改動默認是不會被保存的,須要docker commit
提交更新image
而volume提供方便持久化的存儲,而且volume可用於容器與容器間的數據共享html
docker run -d --name nginx -v /usr/share/nginx/html nginx
docker inspect nginx // 檢查,列表容器的全部信息
複製代碼
這裏咱們關心得是Mounts
部分node
{"Mounts": [
{
"Type": "volume",
"Name": "e9484a423cea196a0646fd3570caa3c029724c80b5861479a2429e1854be1225",
"Source": "/var/lib/docker/volumes/e9484a423cea196a0646fd3570caa3c029724c80b5861479a2429e1854be1225/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]}
複製代碼
表示將機器路徑/var/lib/docker/volumes/e9484a423cea196a0646fd3570caa3c029724c80b5861479a2429e1854be1225/_data
掛載到容器/usr/share/nginx/html
進機器看看:nginx
sudo ls /var/lib/docker/volumes/e9484a423cea196a0646fd3570caa3c029724c80b5861479a2429e1854be1225/_data
複製代碼
進入容器內看看:docker
docker exec -it nginx bash
ls /usr/share/nginx/html
複製代碼
上下對應
docker run -p 8080:80 -d -v $PWD/html:/usr/share/nginx/html nginx // 當前目錄下html文件夾指向 容器文件夾/var/www/html
cd $PWD/html
複製代碼
新建文件 index.htmlshell
vim index.html
this is new path: /Users/XXX/Dev/workplace/docker/test/dockerfile2/html
// 測試 curl http://localhost:8080/
複製代碼
docker create -v $PWD/data:/var/mydata --name data_container ubuntu
docker run -it --volumes-from data_container ubuntu bash // --volumes-from 數據區來源於; ubuntu 基礎鏡像
複製代碼
進入容器命令行輸入mount
,你會找到/var/mydata
json
overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/HV7J3XHTSUR2VIX5FAICK3GTNP:/var/lib/docker/overlay2/l/FNZVNT3SVEJA6OA6AUF4HS7BAF:/var/lib/docker/overlay2/l/HLYOPOXL7XLBVZ75AEBUXBDSI6:/var/lib/docker/overlay2/l/VLLH4C5NZ7VWCHKU2FE5WQIF43:/var/lib/docker/overlay2/l/IZU6B7HVYHQIVVS5HF4QNFZSWN,upperdir=/var/lib/docker/overlay2/7e5a362489866581c4c8bc1979782cda8f85d06f48855cdc0faf0cda1c9498de/diff,workdir=/var/lib/docker/overlay2/7e5a362489866581c4c8bc1979782cda8f85d06f48855cdc0faf0cda1c9498de/work)
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)
cpuset on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)
cpu on /sys/fs/cgroup/cpu type cgroup (ro,nosuid,nodev,noexec,relatime,cpu)
cpuacct on /sys/fs/cgroup/cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct)
blkio on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)
memory on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)
devices on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)
freezer on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)
net_cls on /sys/fs/cgroup/net_cls type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls)
perf_event on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)
net_prio on /sys/fs/cgroup/net_prio type cgroup (ro,nosuid,nodev,noexec,relatime,net_prio)
hugetlb on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb)
pids on /sys/fs/cgroup/pids type cgroup (ro,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,name=systemd)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
osxfs on /var/mydata type fuse.osxfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other,max_read=1048576)
/dev/sda1 on /etc/resolv.conf type ext4 (rw,relatime,data=ordered)
/dev/sda1 on /etc/hostname type ext4 (rw,relatime,data=ordered)
/dev/sda1 on /etc/hosts type ext4 (rw,relatime,data=ordered)
devpts on /dev/console type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
proc on /proc/bus type proc (ro,relatime)
proc on /proc/fs type proc (ro,relatime)
proc on /proc/irq type proc (ro,relatime)
proc on /proc/sys type proc (ro,relatime)
proc on /proc/sysrq-trigger type proc (ro,relatime)
tmpfs on /proc/acpi type tmpfs (ro,relatime)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/keys type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/timer_list type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/sched_debug type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /sys/firmware type tmpfs (ro,relatime)
複製代碼
驗證一下:ubuntu
touch index.html
複製代碼
exit
退出,如$PWD/data
下出現index.html
證實掛載成功
以上方式,能夠實現多容器數據同步vim