將數據從宿主機到容器的三種方式: 1,volumes:docker管理宿主機文件系統的一部分(/var/lib/docker/volumes)保存數據的最佳方式 2,bind mounts 將宿主機上的任意位置的文件或者目錄掛在到容器 (--mount type=bind,src=源目錄,dst=目標目錄) 3,tmpfs:掛載存儲在主機系統的內存中,而不會寫入主機的文件系統。若是不但願將數據持久存儲在任何位置,能夠使用 tmpfs,同時避免寫入容器可寫層提升性能。 =====================Volume================================ 方式一: $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9ba6a9cecc6 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp nginx01 root@bogon: ~ 14:23:51 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 8 days ago 109MB nginx latest 881bd08c0b08 8 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 14:23:56 $ docker volume ls DRIVER VOLUME NAME root@bogon: ~ 14:24:00 $ docker volume create nginx-vol inx-vol ▽oot@bogon: ~ 14:24:14 $ docker volume ls DRIVER VOLUME NAME local nginx-vol root@bogon: ~ 14:24:22 $ docker run -itd --name nginx02 -p 8081:80 -h nginx01 -v nginx-vol:/usr/share/nginx/html nginx 6e5a0cf4451cb71a78e41d7dcfd2a60181208d7b6ca6f4c05d16434dcc674ba7 root@bogon: ~ 14:25:05 $ docker volume inspect nginx-vol [ { "CreatedAt": "2019-03-13T14:25:04+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", "Name": "nginx-vol", "Options": {}, "Scope": "local" } ] root@bogon: ~ 14:33:43 $ cd /var/lib/docker/volumes/nginx-vol/_data root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:33:48 $ ls 50x.html index.html root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:33:49 $ vim index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:34:11 $ echo "aa" >index.html root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:35:42 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6e5a0cf4451c nginx "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:8081->80/tcp nginx02 c9ba6a9cecc6 nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp nginx01 root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:36:00 $ curl 127.0.0.1:8081 aa root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:36:02 $ 方式二: $ docker run -itd -p 8082:80 --mount src=nginx-vol2,dst=/usr/share/nginx/html --name nginx03 nginx 6254f743f799b52e82516767989e995d08601383188d0e4fbda07cbea3ca858f root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:38:47 $ docker volume inspect nginx-vol2 [ { "CreatedAt": "2019-03-13T14:38:32+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/nginx-vol2/_data", "Name": "nginx-vol2", "Options": null, "Scope": "local" } ] root@bogon: /var/lib/docker/volumes/nginx-vol/_data 14:38:53 $ 注意: 1,若是沒指定卷,自動建立 2,建議使用--mount,比較通用 =====================Bind Mounts================================ root@bogon: /data/www 12:55:34 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@bogon: /data/www 12:55:36 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 8 days ago 109MB nginx latest 881bd08c0b08 8 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: /data/www 12:55:39 $ mkdir /data/www -p root@bogon: /data/www 12:55:49 $ cat /data/www/index.html sadasdad root@bogon: /data/www 12:57:17 $ docker run -itd --name nginx01 -p 8080:80 --mount type=bind,src=/data/www,dst=/usr/share/nginx/html nginx c9ba6a9cecc6bb8b241a15841fe1889f56a9c92e315017eb6c68fb0ef0c5bff4 root@bogon: /data/www 12:57:32 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9ba6a9cecc6 nginx "nginx -g 'daemon of…" 9 seconds ago Up 8 seconds 0.0.0.0:8080->80/tcp nginx01 root@bogon: /data/www 12:57:41 $ docker exec -it c9ba6a9cecc6 bash root@c9ba6a9cecc6:/# cat /usr/share/nginx/html/index.html sadasdad root@c9ba6a9cecc6:/# 注意: 1,若是源文件目錄沒有存在(也就是src),不會自動建立,會拋出一個錯誤 2,若是掛載目標容器中爲非空目錄,則該目錄源文件會被隱藏。 volume 總結: 1,多個運行的容器之間共享數據,由於掛載點若是同樣存儲是同樣的 2,當容器中止或者刪除,該卷依然存在,數據不會丟失 3,多個容器能夠同時掛載相同的卷 4,當明確刪除卷時,卷纔會被刪除 5,將容器數據存儲在遠程主機上或者其餘存儲 6,將數據從一臺docker主機遷移到另外一臺時,先中止容器,而後備份卷的目錄(/var/lib/docker/volumes/) Bind Mounts 總結: 1,從主機共享配置文件到容器,默認狀況下,掛載主機/etc/resolv.conf到每一個容器,提供dns解析 2,在docker主機上開發環境和容器之間共享源代碼,例如,能夠將maven tag目錄掛在到容器中,每次在docker主機上構建maven項目時,容器均可以訪問構建的項目包 3,當docker主機的文件或者目錄結構保證與容器所需的綁定掛在一致時 區別: volume 是docker的宿主機文件系統一部分,只有docker能夠進行更改,其餘進程不能修改 bind mounts 是掛載在宿主機文件系統的任意位置,除了docker全部進程均可以進行修改、