持久化数据指的是数据的生命周期和容器的不一致,容器删除后数据还在。
非持久化数据指的是它的生命周期和容器一致,容器删除后数据也没了。
docker容器中持久化数据一般采用两种存储方式
volume
bind mount

一、volume进行容器挂载数据卷
 1、创建容器
 root@herrychen:~# docker run -d -p 8080:80 -v /usr/local/apache2/htdocs httpd
 Unable to find image ‘httpd:latest’ locally
 latest: Pulling from library/httpd
 54fec2fa59d0: Already exists
 8219e18ac429: Pull complete
 3ae1b816f5e1: Pull complete
 a5aa59ad8b5e: Pull complete
 4f6febfae8db: Pull complete
 Digest: sha256:c9e4386ebcdf0583204e7a54d7a827577b5ff98b932c498e9ee603f7050db1c1
 Status: Downloaded newer image for httpd:latest
 b961ae49dacd8d4a0eac47d82def35fe584a6bdeb52499ef65edcc4fc69b0a102、docker volume用法
 Commands:
 create Create a volume 创建一个数据卷
 inspect Display detailed information on one or more volumes 打印一个或多个数据卷的详细信息
 ls List volumes 列出所有数据卷
 prune Remove all unused local volumes 删除所有未使用的数据卷
 rm Remove one or more volumes 删除一个或多个数据卷root@herrychen:~# docker volume ls
 DRIVER VOLUME NAME
 local 2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee55393、查看volume的详细信息,volume把本地的一个自动生成的目录挂载给容器的目录。
 root@herrychen:~# docker inspect b961a
4、查看本地的那个自动生成的目录文件内容
root@herrychen:~# docker ps -a
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 b961ae49dacd httpd “httpd-foreground” 19 minutes ago Up 19 minutes 0.0.0.0:8080->80/tcp laughing_panini
 acbc44e0b486 registry “/entrypoint.sh /etc…” 13 hours ago Up 13 hours 0.0.0.0:1000->5000/tcp affectionate_burnell5、进入容器内修改主页内容
 root@herrychen:~#
 root@herrychen:~# docker exec -it b961a bash
 root@b961ae49dacd:/usr/local/apache2# cd htdocs/
 root@b961ae49dacd:/usr/local/apache2/htdocs# ls
 index.html
 root@b961ae49dacd:/usr/local/apache2/htdocs# echo “update the index” > index.html
 root@b961ae49dacd:/usr/local/apache2/htdocs# exit
 exitroot@herrychen:~# curl 127.0.0.1:8080
 update the index进入容器修改主页已经成功了。
6、本地目录里的index.html也被修改了。进行了同步更新。
 root@herrychen:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
 update the index7、强制删除容器
 root@herrychen:~# docker rm -f b961ae49dacd
 b961ae49dacd8、本地自动生成的目录文件还在。
 root@herrychen:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
 update the index二、bind mount进行容器挂载数据卷
 1、创建文件
 root@herrychen:~# mkdir htdocs
 root@herrychen:~# ls
 dockerfile htdocs2、创建容器,:ro参数表示只读
 root@herrychen:~# docker run --name httpd01 -d -p 8081:80 -v /root/htdocs/:/usr/local/apache2/htdocs:ro httpd
 4b61ce6450e3a5efa2a2760e29ed0b99d2a01d5b223a8248c5345fc5d34c3479

3、查看主页内容,是空的,

docker desktop volumes 怎么引用 docker --volumes-from_docker

4、容器里主页内容也是空的,说明没有文件,文件被/root/htdocs/给覆盖了。
 同时进入容器修改文件内容也报错,文件是只读的。
 root@herrychen:~# docker exec -it httpd01 bash
 root@4b61ce6450e3:/usr/local/apache2# ls
 bin build cgi-bin conf error htdocs icons include logs modules
 root@4b61ce6450e3:/usr/local/apache2# cd htdocs
 root@4b61ce6450e3:/usr/local/apache2/htdocs# ls
 root@4b61ce6450e3:/usr/local/apache2/htdocs# echo “bind mount” > index.html
 bash: index.html: Read-only file system3、只能通过对宿主机目录文件的修改进行同步。
 root@herrychen:~# cd htdocs/
 root@herrychen:~/htdocs# ls
 root@herrychen:~/htdocs# vim index.html
 bind mount
 root@herrychen:~/htdocs# cat index.html
 bind mountroot@herrychen:~/htdocs# curl 127.0.0.1:8081
 bind mount4、不带:ro表示数据卷可读写,进入容器修改文件内容也可以了。
 说明bind mount不但可以把宿主机的目录挂载给容器,同时可以限制对容器内的目录的读写权限。root@herrychen:~#docker run --name httpd02 -d -p 8082:80 -v /root/htdocs/:/usr/local/apache2/htdocs httpd
 3ews1ce6450e3a5efa2a2760e291d0b99d2a01d5b223a8248c5345fc5d34c3567root@herrychen:~#docker exec -it httpd2 bash
 root@3ews1ce6450e:/usr/local/apache2#cd htdocs/
 #ls
 index.html
 #cat index.html
 bind mount
 #echo “i can change” > index.html
 i can change三、volume container的使用
1、创建数据卷
 root@herrychen:~# docker volume create mynewvolume
 mynewvolume
 root@herrychen:~# docker volume ls
 DRIVER VOLUME NAME
 local mynewvolume2、创建一个不需要运行的容器,因为只需要对其他容器提供一个挂载点。
 root@herrychen:~# docker create --name vc -v mynewvolume:/usr/local/apache2/htdocs httpd
 ac4d6026e0317902faa65e4d31ba697371622075eb5feea7ad8d42503d5661213、创建两个业务容器,
 –volumes-from是挂载数据卷,将httpd3容器、httpd4容器的数据卷挂载到vc容器中
 root@herrychen:~# docker run --name http3 -d -p 1003:80 --volumes-from vc httpd
 74db47fb9876571ba8b483e13eec21eb59d9e2a9dc330a23178b2433a6cb61b9root@herrychen:~# docker run --name http4 -d -p 1004:80 --volumes-from vc httpd
 60ab284ad4705900e358456b17ee571deb998733e43c7f7953673d756ae85b78root@herrychen:~# docker ps -a
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 60ab284ad470 httpd “httpd-foreground” 23 seconds ago Up 21 seconds 0.0.0.0:1004->80/tcp http4
 74db47fb9876 httpd “httpd-foreground” 54 seconds ago Up 52 seconds 0.0.0.0:1003->80/tcp http3
 ac4d6026e031 httpd “httpd-foreground” 5 minutes ago Created vc4、进入httpd3容器修改主页文件
 root@herrychen:~#docker exec -it http3 bash
 #cd htdocs/
 #ls
 index.html
 #echo “new data” > index.html
 #exitroot@herrychen:~#curl 127.0.0.1:1003
 new data5、看到httpd4容器也更新了,
 root@herrychen:~#curl 127.0.0.1:1004
 new data

可见volume container是做容器共享数据存储非常好的一种模式。